slow queries

Solutions on MaxInterview for slow queries by the best coders in the world

showing results for - "slow queries"
Sara Sofía
21 Jan 2021
1declare @dayportion float;
2declare @substringlength int;
3set @dayportion=-1.0;
4set @substringlength=200;
5
6SELECT TOP 15 
7    sum(total_logical_reads) 'Total Logical Reads', 
8    sum(total_logical_writes) 'Total Logical Writes',
9    sum(total_worker_time) 'Total CPU',
10    sum(execution_count) 'Count',
11    sum(total_logical_reads) * 100 / (select sum(total_logical_reads) from sys.dm_exec_query_stats where last_execution_time > dateadd(day, @dayportion, getdate())) 'Pct of Reads',
12    sum(total_logical_writes) * 100 / (select sum(total_logical_writes) from sys.dm_exec_query_stats where last_execution_time > dateadd(day, @dayportion, getdate())) 'Pct of Writes',
13    sum(total_worker_time) * 100 / (select sum(total_worker_time) from sys.dm_exec_query_stats where last_execution_time > dateadd(day, @dayportion, getdate())) 'Pct of CPU',
14    sum(total_logical_reads) / sum(execution_count) 'Avg Reads',
15    sum(total_logical_writes) / sum(execution_count) 'Avg Writes',
16    sum(total_worker_time) / sum(execution_count) 'Avg CPU',
17    statement_text
18FROM (
19        select 
20        total_logical_reads, 
21        total_logical_writes,
22        total_worker_time,
23        execution_count,
24        substring(SUBSTRING(st.text, (qs.statement_start_offset/2)+1, 
25            ((CASE qs.statement_end_offset
26              WHEN -1 THEN DATALENGTH(st.text)
27             ELSE qs.statement_end_offset
28             END - qs.statement_start_offset)/2) + 1), 0, @substringlength) as statement_text
29        FROM sys.dm_exec_query_stats qs 
30        CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
31        WHERE last_execution_time > dateadd(day, @dayportion, getdate())
32) x
33group by statement_text
34ORDER BY 'Pct of CPU' DESC
35
similar questions
queries leading to this page
slow queries