执行计划重用
考虑以下“代码”,
define stmt1 = 'insert into T(a, b) values(1, 1);
define stmt2 = 'select * from T';
MSSqlCommand.Execute( stmt1;stmt2 );
MSSqlCommand.Execute( stmt2 );
使用以下方法调查缓存的查询计划:
SELECT [cp].[refcounts]
, [cp].[usecounts]
, [cp].[objtype]
, [st].[dbid]
, [st].[objectid]
, [st].[text]
, [qp].[query_plan]
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text ( cp.plan_handle ) st
CROSS APPLY sys.dm_exec_query_plan ( cp.plan_handle ) qp ;
我的印象是,第一个“执行”生成一个复合执行计划,而不是两个单一的执行计划,从而禁用第二个“执行”重用第一个“执行”中生成的任何执行计划执行。
我说得对吗?
Consider the following "code"
define stmt1 = 'insert into T(a, b) values(1, 1);
define stmt2 = 'select * from T';
MSSqlCommand.Execute( stmt1;stmt2 );
MSSqlCommand.Execute( stmt2 );
Investigating the cached query-plans using:
SELECT [cp].[refcounts]
, [cp].[usecounts]
, [cp].[objtype]
, [st].[dbid]
, [st].[objectid]
, [st].[text]
, [qp].[query_plan]
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text ( cp.plan_handle ) st
CROSS APPLY sys.dm_exec_query_plan ( cp.plan_handle ) qp ;
My impression is that the first "Execute" generates a composite execution plan instead of two singular execution plans, thereby disabling the second "Execute" reusing any execution plan generated in the first Execute.
Am I right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你说得对。要重用执行计划的第二部分,您需要将第一个语句拆分为 2 个单独的执行计划。您可以通过使用单独的
MSSqlCommand.Execute
调用来执行它们,或者在一个查询中使用对sp_executesql
的两次调用来执行此操作(这会增加一级间接)。它看起来像这样(伪代码):Yes, you're right. To reuse the the second part of the execution plan you need to split the first statement into 2 separate execution plans. You can do this either by executing them with separate
MSSqlCommand.Execute
calls or by using two calls tosp_executesql
in one query (this adds one level of indirection). It would look something like this (in pseudocode):