mysqli_multi_query 是否一次性将整个 SQL 发送到数据库?
我严重怀疑 mysqli 的多查询是否是真正的多查询,因为从 Web 服务器到数据库的总行程仅为 1。
如果我们调用 5 语句多查询,我们必须执行 multi_query()一次和 next_result() 4 次。
那么,这不是仍然是从 Web 服务器到数据库的 5 次访问吗?
此外,mysqli_use_result()
、mysqli_more_results()
和 mysqli_store_result()
每次调用都需要访问一次数据库?
I am seriously doubting mysqli's multi-queries are truly multi-queries in the sense that the total trip made to the database from the web server is only 1.
If we call a 5-statement multi-query, we have to do multi_query() once and next_result() 4 times.
So isn't that still 5 trips to the database from the web server?
And besides, mysqli_use_result()
, mysqli_more_results()
and mysqli_store_result()
all requires one trip to the database per call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,
mysqli:multi_query()
确实将 SQL 一次性发送到服务器。该函数还将发出一个next_result()
调用,这意味着它将等待返回第一个结果集。 SQL 的处理将在 MySQL 服务器上继续进行,而 PHP 可以自由地执行其他操作。然而,要获得结果,您需要阻止 PHP 脚本并要求 MySQL 提供结果。这就是为什么如果您希望收到 5 个结果,则需要调用
next_result()
4 次。每个调用都会对服务器进行一次新的访问以请求结果,这些结果可能已经准备好并等待获取或尚未获取。实际上,您最终可能会访问 MySQL 服务器超过 5 次。 值得指出的是,这本身并不会提高 PHP 脚本的性能。除非您有充分的理由在 MySQL 服务器上批量执行 SQL,否则不要使用
mysqli::multi_query()
。坚持使用准备好的语句。Yes,
mysqli:multi_query()
does send the SQL in one go to the server. The function will also issue a singlenext_result()
call, which means that it will wait for the first result set to be returned. The processing of the SQL will continue on the MySQL server while PHP is free to do something else.However, to get the results you need to block PHP script and ask MySQL to provide the result. This is why you need to call
next_result()
4 times if you expect to receive 5 results. Each call does make a new trip to the server to ask for the results, which might already be ready and waiting to be fetched or not.In practice, you might end up making more than 5 trips to the MySQL server. It's worth pointing out that this is not going to improve the performance of your PHP script on its own. Unless you have some very good reason to execute SQL in bulk on the MySQL server do not use
mysqli::multi_query()
. Stick to using prepared statements.是的。
mysqli::multi_query
确实在同一个 MySQL 数据包中发送多个查询。Yes.
mysqli::multi_query
does send multiple queries in the same MySQL packet.