In an attempt to determine the cause of occasional slow running queries, I added an extended event session that logged all events of the type sp_statement_completed on a specific catalog, as well as some standard waits (definition of session is at the end). The results I am getting do not seem to add up and I was wondering if I missed something.
When looking at the captures for a particular execution of the stored procedure in question, I am able to find the event that corresponds to the completion each statement within the SP as well as an even for the completion of the SP itself. However, the duration of the overall SP is grossly higher than the sum of the individual statements and I am baffled as to why.
The SP has three statements in it, none of which take over 20000 microseconds to complete, but the SP itself is taking almost 4 seconds. There were no waits logged for the session at all (I log all waits longer than 100 milliseconds).
In the general case, the SP and the sum of each statement are within a few hundred microseconds of each other.
I can't figure out how to account for the additional time it's taking the SP to complete if each statement finished in sub-second time. Is there a wait_type missing from my logging? Is there some overhead that's not being accounted for as a wait? Any guidance would be appreciated.
Here is my XEvent session definition:
CREATE EVENT SESSION [all_for_2000188] ON SERVER ADD EVENT sqlos.wait_info( ACTION(sqlserver.client_hostname,sqlserver.database_id,sqlserver.plan_handle,sqlserver.session_id,sqlserver.sql_text) WHERE ([opcode]=(1) AND [duration]>(100) AND ([sqlserver].[database_id]=(343) OR [sqlserver].[database_id]=(344)))), ADD EVENT sqlserver.sp_statement_completed( ACTION(sqlserver.client_hostname,sqlserver.database_id,sqlserver.plan_handle,sqlserver.session_id,sqlserver.sql_text) WHERE ([sqlserver].[database_id]=(343) OR [sqlserver].[database_id]=(344))), ADD EVENT sqlserver.sql_statement_completed( ACTION(sqlserver.client_hostname,sqlserver.database_id,sqlserver.plan_handle,sqlserver.session_id,sqlserver.sql_text) WHERE ([sqlserver].[database_id]=(343) OR [sqlserver].[database_id]=(344))) ADD TARGET package0.event_file(SET filename=N'Z:\XELogs\waits_for_2000188.xel',max_file_size=(50),max_rollover_files=(5)), ADD TARGET package0.ring_buffer WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=1 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=OFF) GO