准备好的 SQL 查询时间与常规查询时间
我知道,从我所读到的内容来看,准备好的语句速度更快,因为预编译的缓存版本用于重复查询。我的疑问是:时间到底节省在哪里?我明白了,只能节省准备查询所花费的时间。即使准备好的语句也必须进行数据库搜索,因此不会节省时间。我错了吗?
I know, from whatever I've read, prepared statements are faster since pre-compiled cached version is used for recurring queries. My doubt is : Exactly where time is saved? I see, only the time taken in preparing a query could be saved. Even prepared statements have to do database search and so no time is saved there. Am I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正确的。使用准备好的语句节省的时间通常是在数据库引擎规划/编译查询中。
That's correct. The time saved by using prepared statements is generally in the database engine planning/compiling the query.
“准备好的查询”中准备好的部分是执行计划。计划告诉数据库如何执行查询;使用哪些索引,按什么顺序。执行计划还解决任何访问权限。
通过构建一次执行计划而不是为每个查询构建一次,可以节省时间。
The part of a "prepared query" that is prepared is the execution plan. The plan tells the database how to execute the query; which indexes to use, in which order. The execution plan also resolves any access rights.
Time is saved by building the execution plan once instead of for every query.