PHP、MySQL和大量简单查询

发布于 2024-09-16 05:16:14 字数 274 浏览 1 评论 0原文

我正在实现一个应用程序,该应用程序将有很多客户端从我的网络服务器查询大量小数据包。现在我不确定是否使用与数据库的持久数据连接。数据库当前与网络服务器位于同一系统上,并且可以通过套接字连接,但这可能在不久的将来发生变化。

据我所知,PHP 的几个版本之前 mysqli_pconnect 已被删除,因为它的表现不佳。与此同时,它似乎又回来了。

根据我的场景,我想我不会有其他机会每分钟处理数千个查询,但有大量的持久连接和 MySQL 配置,每个连接只保留很少的资源,对吗?

感谢您的意见!

I'm implementing an application that will have a lot of clients querying lots of small data packages from my webserver. Now I'm unsure whether to use persistent data connections to the database or not. The database is currently on the same system as the webserver and could connect via the socket, but this may change in the near future.

As I know a few releases of PHP ago mysqli_pconnect was removed because it behaved suboptimally. In the meantime it seems to be back again.

Based on my scenario I suppose I won't have an other chance to handle thousands of queries per minute but with loads of persistent connections and a MySQL configuration that reserves only little resources per connection, right?

Thanks for your input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

書生途 2024-09-23 05:16:14

当你测试它时发生了什么?

有了世界上的巢穴,没有任何实用的方法可以传达人们在 SO 响应中提供明确答案所需的所有信息。然而,通常建立 mysql 连接的开销非常小,特别是当它与数据库客户端(在本例中是 Web 服务器)驻留在同一系统上时。如果您使用文件系统而不是网络套接字,开销甚至会更少。

因此,我建议抽象所有数据库调用,以便您可以轻松地在连接类型之间切换,但编写系统以使用按需连接,并确保您的代码尽快显式释放连接 - 然后看看它的行为如何。

C.

What happened when you tested it?

With the nest will in the world, there's no practical way you can convey all the informaton required for people to provide a definitive answer in a SO response. However usually there is very little overhead in establishing a mysql connection, particularly if it resides on the same system as the database client (in this case the webserver). There's even less overhead if you use a filesystem rather than a network socket.

So I'd suggest abstracting all your database calls so you can easily switch between connection types, but write your system to use on-demand connections, and ensure you code explicitly releases the connection as soon as practical - then see how it behaves.

C.

泅人 2024-09-23 05:16:14

PHP 持久连接是邪恶的吗?

问题是只能这样
主机之间有许多活跃的连接
“Apache”和主机“MySQL”

持久连接通常会出现问题,因为您达到了最大连接数。另外,在您的情况下,它并没有带来很大的好处,因为您的数据库服务器位于同一主机上。暂时保持正常连接。

Are PHP persistant connections evil?

The problem is there can be only so
many connections active between Host
“Apache” and Host “MySQL”

Persistant connections usually give problems in that you hit the maximum number of connections. Also, in your case it does not give a great benefit since your database server is on the same host. Keep it to normal connections for now.

千柳 2024-09-23 05:16:14

正如他们所说,您的情况可能会有所不同,但我从未有过使用 PHP 持久连接的良好经验,包括 MySQL 和 Oracle(ODBC 和 OCI8)。每次我测试它时,系统都无法重用连接。在高负载下,我最终达到了最高限制,同时我还有数百个空闲连接。

所以我的建议是你实际尝试一下,看看你的设置是否正确地重用了连接。如果它没有按预期工作,无论如何也不会造成太大损失:与其他 DBMS 相比,打开 MySQL 连接的成本并不是特别高。

另外,不要忘记在适当的时候重置所有相关设置(无论您更改什么会话值,它都会等待您下次建立连接并碰巧重用该连接)。

As they say, your mileage may vary, but I've never had good experiences using persistent connections from PHP, including MySQL and Oracle (both ODBC and OCI8). Every time I've tested it, the system fails to reuse connections. With high load, I end up hitting the top limit while I have hundreds of idle connections.

So my advice is that you actually try it and find out whether your set-up is reusing connections properly. If it isn't working as expected, it won't be a big lose anyway: opening a MySQL connection is not particularly costly compared to other DBMS.

Also, don't forget to reset all relevant settings when appropriate (whatever session value you change, it'll be waiting for you next time to stablish a connection and happen to reuse that one).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文