为什么在通过 sp_executeSQL 过程执行时,完全相同的 SQL 查询会产生不同的执行计划?
正如标题所述,我不明白为什么 sp_executeSQL 会生成与从 Sql Management Studio 运行查询完全不同的执行计划。
从 SQL Management Studio 运行时,我的相关查询将花费 3 秒,而通过 sp_executeSQL 在 Management Studio 中运行的查询将花费 5 分钟。
我已经更新了统计数据,并审查了索引,但事实仍然在我的脑海中,sp_executeSQL 的执行计划比直接从我的管理工作室运行 sql 差得多。
所以这是我的问题:为什么以这两种不同的方式运行查询之间的执行计划差异如此之大?
谢谢
As the title states, I don't understand why the sp_executeSQL would generate a completely different execution plan than running the query from Sql Management Studio.
My query in question will take 3 seconds when run from SQL management Studio, where as the query run in management studio via sp_executeSQL will take 5 minutes.
I've updated statistics, and reviewed indexes, but the fact remained in my head that the execution plan from sp_executeSQL was FAR worse than running the sql directly from my management studio.
So here is my question: Why would the execution plans differ SO much between running the query in these two different ways?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上参见这个
,
同一个过程可以有多个[执行]计划
see this
basically,
there can be multiple [execution] plans for the same procedure
考虑一下这一点。当您执行一个存储过程时,该过程将有自己的执行计划。当您执行查询语句时,它也会有自己的执行计划。现在,当使用 sp_executeSQL 时,您正在运行此存储过程来动态执行查询。因此本质上它的执行计划是 sp_executeSQL 和您的查询的组合。
Consider this. When you execute a stored procedure, this procedure will have its own execution plan. When you execute a query statement, again it will have its own execution plan. Now when using sp_executeSQL you are running this stored procedure to execute a query dynamically. So in essence its execution plan is the combination of sp_executeSQL and your query.