我想在涉及许多存储过程的复杂过程中添加监视功能。 在某些情况下,我想捕获单个语句产生的逻辑读取次数。
换句话说,我想打开set statistics io on
,访问(并将结果保存到日志表中)通常在“消息”选项卡中显示在SSMS中。
我看到它可以在.Net中使用SqlInfoMessageEventHandler完成。我确信它也可以在T-SQL中完成,但我还没有找到它。
谢谢!
sys.dm_exec_requests中的Logical_reads也没有增加......
对我来说,完美的解决方案是以某种方式捕获“set statistics io on”信息:
select name, id
from sysobjects
union all
select name,id
from sysobjects ;
(120 row(s) affected)
Table 'sysschobjs'. Scan count 2, logical reads 6, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
答案 0 :(得分:2)
一种方法是使用2008年及以后可用的dynamic management views。例如,要确定查询所执行的读取次数,您可以:
declare @start_reads bigint
select @start_reads = reads from sys.dm_exec_requests where session_id = @@spid
-- Your query here
select reads - @start_reads from sys.dm_exec_requests where session_id = @@spid
基本上有两种类型的计数器:
_session_
视图具有在当前批次完成后递增的计数器。 _exec_
计数器从0开始,并在批处理运行时递增。