gpt4 book ai didi

PHP 的 mysql 迁移到 mysqli 性能

转载 作者:行者123 更新时间:2023-11-29 18:19:56 25 4
gpt4 key购买 nike

目前我正在将遗留代码库迁移到 PHP 7.1。代码库使用单个数据库对象作为 PHP 已删除的 mysql 函数的包装器。因此,简单的方法似乎是将所有这些 mysql 函数替换为新的 mysqli 函数。

没有问题...

然而,当我将重构后的代码注册到生产服务器时,mysql服务器负载飙升。所以就在那时我发现 mysql_connect 试图重用 mysql 连接(或类似的东西):

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

mysqli_connect 没有这样的行为,所以我们确实看到大量的 mysql 连接被打开。

然后我们通过自己实现 mysqli 对象缓存来解决这个问题。因此,当一个请求尝试打开与同一用户/数据库组合的另一个连接时,我们会重新使用已建立的连接。

这确实有很大的不同,但是我们仍然看到更高的数据库负载(大约高 5 倍)。

我还缺少什么吗?有什么好主意吗?当然,这些问题可能仍然是我们的代码库所特有的,但我希望只是有一些我忽略的 mysqli 扩展。

旁注:我们现在不想使用持久连接,因为这可能会带来其他复杂性。

最佳答案

很可能每个脚本中仍然偶尔有一个或两个孤立连接。确保您的代码每个实例仅连接一次。

we fixed that by implementing mysqli object caching ourself.

在 PHP 中,对象缓存已经实现。您只需确保仅建立一次连接,并且在整个脚本执行过程中仅使用保持该连接的单个对象。

we do not want to use persistent connections just now, because that may give other complications.

正是如此。在一个脚本中拥有无数个连接,持久连接将立即杀死您的数据库。

关于PHP 的 mysql 迁移到 mysqli 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46670930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com