打开一个 MySQL 连接与打开和关闭大量连接?

发布于 2024-09-24 02:11:19 字数 152 浏览 1 评论 0原文

我有一个> php 中的 x20000 循环检查 MySQL 数据库中是否存在条目,我打开 &关闭循环中每个条目的连接,但一位朋友告诉我这太疯狂了,我应该打开一个连接,运行循环,然后关闭它,但他没有告诉我原因。有人可以向我解释一下重用相同连接的好处吗?是CPU使用率还是什么?

I have a > x20000 loop in php that checks if an entry exists in a MySQL database, I open & close the connection for every entry in the loop, but a friend told me that's crazy and I should open one connection, run the loop and then close it, but he didn't tell me why. Could someone explain me the benefits of reusing the same connection? is it CPU usage or what?

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

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

发布评论

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

评论(2

转瞬即逝 2024-10-01 02:11:19

它不仅成本高昂,而且如果您有多个请求同时执行该脚本,则最终可能会遇到服务器上的连接限制,从而导致进一步的请求必须等待或被拒绝。

但最重要的是,正如 @zerkms 所说,这是一项昂贵的操作。大约六个月前,我使用了一个在循环中进行重复连接的脚本,并将连接移到循环之外,脚本的执行时间从 10-12 秒下降到远低于 1 秒。

Not only is it expensive, but if you have multiple requests coming in for the script that are doing it simultaneously, you could end up bumping up against the connection limit on the server, such that further requests must wait or are denied.

But the main thing, as @zerkms said, is that it's an expensive operation. About six months ago, I took a script that made repeated connections in a loop and moved the connection outside the loop, and the script's execution time dropped from 10-12 seconds to well under 1 second.

∞觅青森が 2024-10-01 02:11:19

就是网络连接+mysql连接开销。连接需要一些时间,并且要明确这是“昂贵”的操作。

It is the network connection + mysql connection overheads. It takes a time to connect and to be clear it is "expensive" operation.

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