Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am tracking long running queries with below Xevent , but our requirement is also to pull execution plan and wait type and duration.

and show report in single column as
db_names
    ,r.STATUS
    ,r.blocking_session_id AS 'blocked_by' 
    , Blocked session
    ,r.wait_type
    ,r.wait_resource
    ,wait_time
    ,r.cpu_time
    ,r.logical_reads
    ,r.reads
    ,r.writes
,query_text
,Query_plan (User can download execution plan)
,User_name
,program_name,
User_Name



Long Query Xevent :
CREATE EVENT SESSION [long_queries_trace] ON SERVER 
ADD EVENT sqlserver.rpc_completed(SET collect_statement=(1)
    ACTION(package0.collect_system_time,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.plan_handle,sqlserver.session_id,sqlserver.session_nt_username,sqlserver.sql_text,sqlserver.transaction_id,sqlserver.transaction_sequence,sqlserver.tsql_stack,sqlserver.username)
    WHERE ([package0].[greater_than_uint64]([duration],(1000000)) AND ([result]=(2) AND [package0].[greater_than_uint64]([logical_reads],(0)) AND [package0].[greater_than_uint64]([cpu_time],(100))) AND [package0].[equal_boolean]([sqlserver].[is_system],(0)))),
ADD EVENT sqlserver.sql_batch_completed(
    ACTION(package0.collect_system_time,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.plan_handle,sqlserver.session_id,sqlserver.session_nt_username,sqlserver.sql_text,sqlserver.transaction_id,sqlserver.transaction_sequence,sqlserver.tsql_stack,sqlserver.username)
    WHERE ([duration]>(1000000) AND [cpu_time]>(100) AND [package0].[equal_boolean]([sqlserver].[is_system],(0)))),
ADD EVENT sqlserver.sql_statement_completed(
    ACTION(package0.collect_system_time,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.plan_handle,sqlserver.session_id,sqlserver.session_nt_username,sqlserver.sql_text,sqlserver.transaction_id,sqlserver.transaction_sequence,sqlserver.tsql_stack,sqlserver.username)
    WHERE ([package0].[greater_than_int64]([duration],(1000000)) AND ([package0].[greater_than_uint64]([cpu_time],(100)) AND [package0].[greater_than_uint64]([logical_reads],(0))) AND [sqlserver].[is_system]=(0))) 
ADD TARGET package0.event_file(SET filename=N'C:\SQLTrace\long_queries_4ee.xel',max_file_size=(100),max_rollover_files=(5))
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=10 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=ON)
GO


So I thought to get waits , I used another xevent for waits related data
, but this is missing CPU, information and plan and not able to link wait trace data with above long running query x event data

CREATE EVENT SESSION [wait_analysis] ON SERVER 
ADD EVENT sqlos.wait_info(
    ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_id,sqlserver.database_name,sqlserver.nt_username,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.request_id,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)
    WHERE ([package0].[divides_by_uint64]([sqlserver].[session_id],(5)) AND [package0].[equal_uint64]([opcode],(1)) AND [package0].[equal_boolean]([sqlserver].[is_system],(0)) AND [sqlserver].[sql_text]<>N'' AND [sqlserver].[database_name]<>'SQLPlanner' AND [package0].[greater_than_uint64]([duration],(200)))),
ADD EVENT sqlos.wait_info_external(
    ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_id,sqlserver.database_name,sqlserver.nt_username,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.request_id,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)
    WHERE ([package0].[divides_by_uint64]([sqlserver].[session_id],(5)) AND [package0].[equal_uint64]([opcode],(1)) AND [package0].[equal_boolean]([sqlserver].[is_system],(0)) AND [sqlserver].[sql_text]<>N'' AND [sqlserver].[database_name]<>'SQLPlanner' AND [package0].[greater_than_uint64]([duration],(200)))) 
ADD TARGET package0.event_file(SET filename=N'C:\query_wait.xel',max_file_size=(150),max_rollover_files=(4))
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=OFF,STARTUP_STATE=ON)
GO




The monitoring should also be light weight and support from sql 2014 version.

Kindly guide here how to proceed . Many thanks for your time.

What I have tried:

x events and few DMV but dont get complete columns and data
Posted
Updated 7-May-23 8:30am
v2
Comments
[no name] 8-May-23 10:25am    
"Functions".

https://www.mssqltips.com/sqlservertip/6195/sql-server-function-to-measure-cpu-usage-per-database/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900