info@techdevops.com | 437-991-3573 | Data Engineering Services
TechDevOps.com
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance



Get The TEQ (Top Expensive Queries)
by BF (Principal Consultant; Architecture; Engineering)
2022-03-10








T-SQL:


SELECT TOP 10 SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(qt.TEXT)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2)+1) as 'Statement',
qs.Execution_Count as 'Total_Calls',
last_execution_time as 'Last_Call',
qs.last_elapsed_time/1000 'LastElapsedTime_ms',
qs.last_worker_time/1000 as 'LastCPUTime_ms',
qs.last_logical_reads as 'LastLogicalReads',
qs.last_logical_writes as 'LastLogicalWrites',
qs.total_worker_time as 'TotalWorkerTime',
qs.total_logical_reads as 'TotalLogicalReads',
qs.total_logical_writes as 'TotalLogicalWrites',
qs.total_elapsed_time/1000000 as 'TotalElapsedTime_in_sec',
qp.Query_Plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
-- WHERE qs.last_execution_time > DateAdd(HH,-1,GetDate())
-- ORDER BY qs.total_logical_reads DESC -- Logical Reads
-- ORDER BY qs.total_logical_writes DESC -- Logical Writes
ORDER BY qs.total_worker_time DESC -- CPU time