HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Trace database query

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
databasequerytrace

Problem

I need an advice.
I would need to trace one particular procedure in SQL Server. It happens to me that one procedure ends up with a timeout and I need to find out which select, insert or update this happens. I want to ask, I don't know much about SQL profiler, so my question is, is it really the best tool for my problem? Can I look at a certain procedure in a different way? Is there a better tool?

Solution

You can use an Extended Event for this.

CREATE EVENT SESSION [TrackStoredProc] ON SERVER 
ADD EVENT sqlserver.rpc_completed(SET collect_statement=(1)
    WHERE ([object_name]=N'NameOfStoredProc')),
ADD EVENT sqlserver.sp_statement_completed(SET collect_object_name=(1)
    WHERE ([object_name]=N'NameOfStoredProc'))
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=ON,STARTUP_STATE=ON)
GO


When you use the one above, you can see the statements within a Stored Procedure and the duration of each.
The Track Causality will make sure that you can relate the statements to a call.

See this article for more information

Code Snippets

CREATE EVENT SESSION [TrackStoredProc] ON SERVER 
ADD EVENT sqlserver.rpc_completed(SET collect_statement=(1)
    WHERE ([object_name]=N'NameOfStoredProc')),
ADD EVENT sqlserver.sp_statement_completed(SET collect_object_name=(1)
    WHERE ([object_name]=N'NameOfStoredProc'))
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=ON,STARTUP_STATE=ON)
GO

Context

StackExchange Database Administrators Q#325890, answer score: 4

Revisions (0)

No revisions yet.