sql - Execution Plan reuse -


consider following "code"

define stmt1 = 'insert t(a, b) values(1, 1); define stmt2 = 'select * t'; mssqlcommand.execute( stmt1;stmt2 ); mssqlcommand.execute( stmt2 ); 

investigating cached query-plans using:

select [cp].[refcounts]  , [cp].[usecounts]  , [cp].[objtype]  , [st].[dbid]  , [st].[objectid]  , [st].[text]  , [qp].[query_plan]  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 first "execute" generates composite execution plan instead of 2 singular execution plans, thereby disabling second "execute" reusing execution plan generated in first execute.

am right?

yes, you're right. reuse the second part of execution plan need split first statement 2 separate execution plans. can either executing them separate mssqlcommand.execute calls or using 2 calls sp_executesql in 1 query (this adds 1 level of indirection). (in pseudocode):

mssqlcommand.execute('exec sp_executesql stmt1; exec sp_executesql stmt2"); 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -