user975383
user975383

Reputation: 51

Getting "set statistics io on" results in t-sql for tuning

I want to add monitoring capabilities to a complex process involving many stored procedures. In some cases I want to capture the number of logical reads produced by a single statement.

In other words, I would like to turn on the set statistics io on, access (and save the results to a log table) what is usually displayed in the SSMS in the "messages" tab.

I saw that it can be done in .Net with SqlInfoMessageEventHandler. I'm sure that it can also be done in T-SQL but i didn't find it yet.

Thanks!


Logical_reads in sys.dm_exec_requests is not increasing as well...

The perfect solution for me would be a way of somehow capturing the "set statistics io on" information :

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.

Upvotes: 4

Views: 1811

Answers (1)

Andomar
Andomar

Reputation: 238096

One way is to use dynamic management views, available in 2008 and up. For example, to determine the number of reads done by your query, you could:

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

There's basically two types of counters:

  • The _session_ views have counters that are incremented after your current batch completes.
  • The _exec_ counters start at 0 and increment while your batch is running.

Upvotes: 2

Related Questions