MySQL 准备好的查询是否为每次会话一次的查询提供性能优势?
根据文档,如果您多次运行查询,准备好的查询会提供显着的性能优势,因为 MySQL 服务器解析查询的开销仅发生一次。 我想知道他们所说的“多次”到底是什么意思。
即,假设您有一个网页运行一次查询。 现在假设该页面每秒被调用 50 次。 从性能角度来看,prepare() 查询(从而需要到数据库服务器的两次往返;一次准备查询,一次运行查询)或仅正常发送查询(只需要一次往返)是否更有意义? ? MySQL 和/或 PHP mysqli 驱动程序是否足够智能,能够意识到在之前的调用中何时准备了查询?
According to the documentation, a prepared query provides a significant performance benefit if you're running a query multiple times because the overhead of the MySQL server parsing the query only happens once. I'm wondering what exactly they mean by "multiple times" there.
I.e., say you have a web page that runs a query one time. Now say that page gets called 50 times per second. Does it make more sense from a performance standpoint to prepare() the query (thereby requiring two roundtrips to the DB server; one to prepare the query, one to run it) or to just send the query normally (which only requires one roundtrip)? Is MySQL and/or the PHP mysqli driver smart enough to realize when a query was prepare()'d in a previous invocation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不会。PHP 是一种“无共享”架构,因此与一个请求(一个页面视图)关联的每个资源都会在该请求结束时被丢弃。 准备好的查询不可用于后续数据库连接。
您将从准备好的查询中受益的情况是,您准备好查询并在同一个 PHP 请求期间多次执行它。
No. PHP is a "shared nothing" architecture, so every resource associated with one request (one page view) is discarded at the end of that request. Prepared queries are not available to a subsequent database connections.
The scenario in which you would get benefit from a prepared query is when you prepare it and execute it many times during the same PHP request.