Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking wait statistics per T-SQL statement

Tags:

sql

sql-server

I am creating an application that tracks some specific performance-related statistics from SQL Server for every query executed on the server. I can get most of the information through SQL Trace or more specifically, using the StatementCompleted and BatchCompleted event classes.

Using SQL Trace with the above events gives me everything except wait stats. I know I can get the wait statistics separately from the two Dynamic Management Views and Functions sys.dm_os_wait_stats and sys.dm_os_waiting_tasks.

I need to find a way to somehow club these two information together so that I can determine the wait stats on a per query basis. For example, I need to be able to answer a question like: How long did query X had to wait for an IO or CPU resource before it could begin execution?

I cannot answer this question accurately because SQL Trace does not give me this information. Is there anyway to obtain wait stats from SQL Server for every statement that is being executed on the server?

like image 294
Rahul Ravindran Avatar asked Sep 01 '26 14:09

Rahul Ravindran


1 Answers

If you are on SQL Server 2008 you can use extended events to capture details of the sqlos.wait_info event filtered on the spid of interest.

See SQLSkills: Capturing wait stats for a single operation for example code.

like image 110
Martin Smith Avatar answered Sep 03 '26 03:09

Martin Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!