Skip to content

Commit 6804efc

Browse files
committed
New query store project
1 parent ed6e81b commit 6804efc

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

query_store.pbix

101 KB
Binary file not shown.

query_store.sql

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
WITH MainQuery AS (
2+
SELECT
3+
DB_NAME() AS [database_name]
4+
,DB_NAME() + N':' + CAST(qry.query_id AS nvarchar(20)) AS query_id
5+
,obj.[type] AS [object_type]
6+
,SCHEMA_NAME(obj.schema_id) + N'.' + obj.[name] AS [object_name]
7+
,qrytxt.query_sql_text
8+
,qryexec.plan_count
9+
,qryexec.execution_count
10+
,qryexec.total_duration_in_minutes
11+
,qryexec.total_logical_io_reads
12+
,qryexec.total_physical_io_reads
13+
,qryexec.total_query_max_used_memory_mb
14+
,qryexec.total_rowcount
15+
,qryexec.total_tempdb_space_used_mb
16+
FROM
17+
sys.query_store_query AS qry
18+
INNER JOIN sys.query_store_query_text AS qrytxt
19+
ON qry.query_text_id = qrytxt.query_text_id
20+
LEFT JOIN sys.objects AS obj
21+
ON qry.[object_id] = obj.[object_id]
22+
INNER JOIN (
23+
SELECT
24+
qrypln.query_id
25+
,count(DISTINCT qrypln.plan_id) AS plan_count
26+
,SUM(qrystat.count_executions) AS execution_count
27+
,SUM(qrystat.count_executions * qrystat.avg_duration / 6000000) AS total_duration_in_minutes
28+
,SUM(qrystat.count_executions * qrystat.avg_logical_io_reads) AS total_logical_io_reads
29+
,SUM(qrystat.count_executions * qrystat.avg_physical_io_reads) AS total_physical_io_reads
30+
,SUM(qrystat.count_executions * qrystat.avg_query_max_used_memory * 8 / 1024) AS total_query_max_used_memory_mb
31+
,SUM(qrystat.count_executions * qrystat.avg_rowcount) AS total_rowcount
32+
,SUM(qrystat.count_executions * qrystat.avg_tempdb_space_used * 8 / 1024) AS total_tempdb_space_used_mb
33+
FROM
34+
sys.query_store_plan AS qrypln
35+
INNER JOIN sys.query_store_runtime_stats AS qrystat
36+
ON qrypln.plan_id = qrystat.plan_id
37+
INNER JOIN sys.query_store_runtime_stats_interval AS intrvl
38+
ON qrystat.runtime_stats_interval_id = intrvl.runtime_stats_interval_id
39+
WHERE
40+
intrvl.start_time >= '2020-02-20'
41+
GROUP BY qrypln.query_id
42+
) AS qryexec
43+
ON qry.query_id = qryexec.query_id
44+
),
45+
TopDuration AS (
46+
SELECT TOP (25) N'Duration' AS top_type, *
47+
FROM MainQuery
48+
ORDER BY total_duration_in_minutes DESC
49+
),
50+
TopLogicalReads AS (
51+
SELECT TOP (25) N'Logical Reads' AS top_type, *
52+
FROM MainQuery
53+
ORDER BY total_logical_io_reads DESC
54+
),
55+
TopPhysicalReads AS (
56+
SELECT TOP (25) N'Physical Reads' AS top_type, *
57+
FROM MainQuery
58+
ORDER BY total_physical_io_reads DESC
59+
),
60+
TopMemoryConsumption AS (
61+
SELECT TOP (25) N'Memory Consumption' AS top_type, *
62+
FROM MainQuery
63+
ORDER BY total_query_max_used_memory_mb DESC
64+
),
65+
TopRowCount AS (
66+
SELECT TOP (25) N'Row Count' AS top_type, *
67+
FROM MainQuery
68+
ORDER BY total_rowcount DESC
69+
),
70+
TopTempdDBUsage AS (
71+
SELECT TOP (25) N'TempDB Usage' AS top_type, *
72+
FROM MainQuery
73+
ORDER BY total_tempdb_space_used_mb DESC
74+
)
75+
SELECT *
76+
FROM TopDuration
77+
UNION ALL
78+
SELECT *
79+
FROM TopLogicalReads
80+
UNION ALL
81+
SELECT *
82+
FROM TopPhysicalReads
83+
UNION ALL
84+
SELECT *
85+
FROM TopMemoryConsumption
86+
UNION ALL
87+
SELECT *
88+
FROM TopRowCount
89+
UNION ALL
90+
SELECT *
91+
FROM TopTempdDBUsage;
92+
93+
/*
94+
SELECT * FROM sys.query_store_runtime_stats;
95+
96+
SELECT * FROM sys.database_query_store_options;
97+
SELECT * FROM sys.query_context_settings;
98+
*/
99+
100+
101+
102+
103+
104+

0 commit comments

Comments
 (0)