执行下面这个语句
SELECT SUBSTRING(text, (statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1
THEN DATALENGTH(text)
ELSE statement_end_offset
END - statement_start_offset)/2) + 1) AS query_text, execution_count,creation_time,last_execution_time,plan_handle
FROM sys.dm_exec_query_stats
CROSS APPLY sys.dm_exec_sql_text(sql_handle)
CROSS APPLY sys.dm_exec_query_plan(plan_handle) p
where p.objectid=OBJECT_ID('storeProcedureName')
((CASE statement_end_offset
WHEN -1
THEN DATALENGTH(text)
ELSE statement_end_offset
END - statement_start_offset)/2) + 1) AS query_text, execution_count,creation_time,last_execution_time,plan_handle
FROM sys.dm_exec_query_stats
CROSS APPLY sys.dm_exec_sql_text(sql_handle)
CROSS APPLY sys.dm_exec_query_plan(plan_handle) p
where p.objectid=OBJECT_ID('storeProcedureName')
得到下面这个结果
虽然各个语句之间为顺序执行的关系,并且没有'if else'导致的语句跳过,但发现三条语句的执行次数(execution_count)不同,创建时间和最后执行时间也不同.
后来发现第2条和第3条语句所涉及的表的数据量变化很大,导致语句级的重编译,使得产生差异
存储过程大致的逻辑如下:
create proc storeProcedureName
@n int
as
select * from table1 where id=@n
select * from table2 where id=@n
select * from table3 where id=@n
@n int
as
select * from table1 where id=@n
select * from table2 where id=@n
select * from table3 where id=@n