Skip to content

Commit fc2f3a9

Browse files
Merge pull request #715 from erikdarlingdata/fix/hi-split-step1-aggregate
sp_QuickieStore: split Step 1 aggregate to avoid 4-way DMV join
2 parents e882bbd + f657699 commit fc2f3a9

1 file changed

Lines changed: 165 additions & 38 deletions

File tree

sp_QuickieStore/sp_QuickieStore.sql

Lines changed: 165 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3945,6 +3945,29 @@ then shows share-of-total for the 80/20 context.
39453945
IF @find_high_impact = 1
39463946
BEGIN
39473947
/*Create temp tables for high impact analysis*/
3948+
CREATE TABLE
3949+
#hi_plan_stats
3950+
(
3951+
plan_id bigint NOT NULL,
3952+
total_executions bigint NOT NULL,
3953+
total_cpu_ms decimal(38, 6) NOT NULL,
3954+
min_cpu_ms decimal(38, 6) NULL,
3955+
max_cpu_ms decimal(38, 6) NULL,
3956+
total_duration_ms decimal(38, 6) NOT NULL,
3957+
min_duration_ms decimal(38, 6) NULL,
3958+
max_duration_ms decimal(38, 6) NULL,
3959+
total_physical_reads_mb decimal(38, 6) NOT NULL,
3960+
min_physical_reads_mb decimal(38, 6) NULL,
3961+
max_physical_reads_mb decimal(38, 6) NULL,
3962+
total_writes_mb decimal(38, 6) NOT NULL,
3963+
min_writes_mb decimal(38, 6) NULL,
3964+
max_writes_mb decimal(38, 6) NULL,
3965+
total_memory_mb decimal(38, 6) NOT NULL,
3966+
min_memory_mb decimal(38, 6) NULL,
3967+
max_memory_mb decimal(38, 6) NULL,
3968+
max_dop bigint NULL
3969+
);
3970+
39483971
CREATE TABLE
39493972
#hi_query_stats
39503973
(
@@ -4075,9 +4098,9 @@ BEGIN
40754098
volatile_metrics nvarchar(4000) NULL
40764099
);
40774100

4078-
/*Step 1: Aggregate runtime stats to query_hash level*/
4101+
/*Step 1a: Aggregate runtime stats to plan_id level (qsrs + qsrsi only)*/
40794102
SELECT
4080-
@current_table = 'inserting #hi_query_stats',
4103+
@current_table = 'inserting #hi_plan_stats',
40814104
@sql = @isolation_level;
40824105

40834106
IF @troubleshoot_performance = 1
@@ -4093,75 +4116,183 @@ BEGIN
40934116
SELECT
40944117
@sql += N'
40954118
SELECT
4096-
qsq.query_hash,
4097-
query_count =
4098-
COUNT(DISTINCT qsq.query_id),
4099-
plan_count =
4100-
COUNT(DISTINCT qsp.plan_id),
4119+
qsrs.plan_id,
41014120
total_executions =
41024121
SUM(qsrs.count_executions),
41034122
total_cpu_ms =
41044123
SUM(qsrs.avg_cpu_time / 1000.0 * qsrs.count_executions),
4105-
avg_cpu_ms =
4106-
SUM(qsrs.avg_cpu_time / 1000.0 * qsrs.count_executions) /
4107-
NULLIF(SUM(qsrs.count_executions), 0),
41084124
min_cpu_ms =
41094125
MIN(qsrs.min_cpu_time / 1000.0),
41104126
max_cpu_ms =
41114127
MAX(qsrs.max_cpu_time / 1000.0),
41124128
total_duration_ms =
41134129
SUM(qsrs.avg_duration / 1000.0 * qsrs.count_executions),
4114-
avg_duration_ms =
4115-
SUM(qsrs.avg_duration / 1000.0 * qsrs.count_executions) /
4116-
NULLIF(SUM(qsrs.count_executions), 0),
41174130
min_duration_ms =
41184131
MIN(qsrs.min_duration / 1000.0),
41194132
max_duration_ms =
41204133
MAX(qsrs.max_duration / 1000.0),
41214134
total_physical_reads_mb =
41224135
SUM(qsrs.avg_physical_io_reads * 8.0 / 1024.0 * qsrs.count_executions),
4123-
avg_physical_reads_mb =
4124-
SUM(qsrs.avg_physical_io_reads * 8.0 / 1024.0 * qsrs.count_executions) /
4125-
NULLIF(SUM(qsrs.count_executions), 0),
41264136
min_physical_reads_mb =
41274137
MIN(qsrs.min_physical_io_reads * 8.0 / 1024.0),
41284138
max_physical_reads_mb =
41294139
MAX(qsrs.max_physical_io_reads * 8.0 / 1024.0),
41304140
total_writes_mb =
41314141
SUM(qsrs.avg_logical_io_writes * 8.0 / 1024.0 * qsrs.count_executions),
4132-
avg_writes_mb =
4133-
SUM(qsrs.avg_logical_io_writes * 8.0 / 1024.0 * qsrs.count_executions) /
4134-
NULLIF(SUM(qsrs.count_executions), 0),
41354142
min_writes_mb =
41364143
MIN(qsrs.min_logical_io_writes * 8.0 / 1024.0),
41374144
max_writes_mb =
41384145
MAX(qsrs.max_logical_io_writes * 8.0 / 1024.0),
41394146
total_memory_mb =
41404147
SUM(qsrs.avg_query_max_used_memory * 8.0 / 1024.0 * qsrs.count_executions),
4141-
avg_memory_mb =
4142-
SUM(qsrs.avg_query_max_used_memory * 8.0 / 1024.0 * qsrs.count_executions) /
4143-
NULLIF(SUM(qsrs.count_executions), 0),
41444148
min_memory_mb =
41454149
MIN(qsrs.min_query_max_used_memory * 8.0 / 1024.0),
41464150
max_memory_mb =
41474151
MAX(qsrs.max_query_max_used_memory * 8.0 / 1024.0),
41484152
max_dop =
41494153
MAX(qsrs.max_dop)
4150-
FROM ' + @database_name_quoted + N'.sys.query_store_query AS qsq
4151-
JOIN ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
4152-
ON qsq.query_id = qsp.query_id
4153-
JOIN ' + @database_name_quoted + N'.sys.query_store_runtime_stats AS qsrs
4154-
ON qsp.plan_id = qsrs.plan_id
4154+
FROM ' + @database_name_quoted + N'.sys.query_store_runtime_stats AS qsrs
41554155
JOIN ' + @database_name_quoted + N'.sys.query_store_runtime_stats_interval AS qsrsi
4156-
ON qsrs.runtime_stats_interval_id = qsrsi.runtime_stats_interval_id
4156+
ON qsrsi.runtime_stats_interval_id = qsrs.runtime_stats_interval_id
41574157
WHERE qsrsi.start_time >= @start_date
4158-
AND qsrsi.start_time < @end_date' + @nc10;
4158+
AND qsrsi.start_time < @end_date
4159+
GROUP BY
4160+
qsrs.plan_id
4161+
HAVING
4162+
SUM(qsrs.count_executions) > 0
4163+
OPTION(RECOMPILE);' + @nc10;
4164+
4165+
IF @debug = 1
4166+
BEGIN
4167+
PRINT LEN(@sql);
4168+
PRINT @sql;
4169+
END;
4170+
4171+
INSERT
4172+
#hi_plan_stats WITH (TABLOCK)
4173+
(
4174+
plan_id,
4175+
total_executions,
4176+
total_cpu_ms,
4177+
min_cpu_ms,
4178+
max_cpu_ms,
4179+
total_duration_ms,
4180+
min_duration_ms,
4181+
max_duration_ms,
4182+
total_physical_reads_mb,
4183+
min_physical_reads_mb,
4184+
max_physical_reads_mb,
4185+
total_writes_mb,
4186+
min_writes_mb,
4187+
max_writes_mb,
4188+
total_memory_mb,
4189+
min_memory_mb,
4190+
max_memory_mb,
4191+
max_dop
4192+
)
4193+
EXECUTE sys.sp_executesql
4194+
@sql,
4195+
N'@start_date datetimeoffset(7),
4196+
@end_date datetimeoffset(7)',
4197+
@start_date,
4198+
@end_date;
4199+
4200+
IF @troubleshoot_performance = 1
4201+
BEGIN
4202+
SET STATISTICS XML OFF;
4203+
4204+
EXECUTE sys.sp_executesql
4205+
@troubleshoot_update,
4206+
N'@current_table nvarchar(100)',
4207+
@current_table;
4208+
4209+
EXECUTE sys.sp_executesql
4210+
@troubleshoot_info,
4211+
N'@sql nvarchar(max),
4212+
@current_table nvarchar(100)',
4213+
@sql,
4214+
@current_table;
4215+
END;
4216+
4217+
/*Step 1b: Roll up to query_hash level (qsp + qsq against pre-aggregated temp table)*/
4218+
SELECT
4219+
@current_table = 'inserting #hi_query_stats',
4220+
@sql = @isolation_level;
4221+
4222+
IF @troubleshoot_performance = 1
4223+
BEGIN
4224+
EXECUTE sys.sp_executesql
4225+
@troubleshoot_insert,
4226+
N'@current_table nvarchar(100)',
4227+
@current_table;
4228+
4229+
SET STATISTICS XML ON;
4230+
END;
41594231

41604232
SELECT
4161-
@sql += N'GROUP BY
4233+
@sql += N'
4234+
SELECT
4235+
qsq.query_hash,
4236+
query_count =
4237+
COUNT(DISTINCT qsp.query_id),
4238+
plan_count =
4239+
COUNT(DISTINCT ps.plan_id),
4240+
total_executions =
4241+
SUM(ps.total_executions),
4242+
total_cpu_ms =
4243+
SUM(ps.total_cpu_ms),
4244+
avg_cpu_ms =
4245+
SUM(ps.total_cpu_ms) /
4246+
NULLIF(SUM(ps.total_executions), 0),
4247+
min_cpu_ms =
4248+
MIN(ps.min_cpu_ms),
4249+
max_cpu_ms =
4250+
MAX(ps.max_cpu_ms),
4251+
total_duration_ms =
4252+
SUM(ps.total_duration_ms),
4253+
avg_duration_ms =
4254+
SUM(ps.total_duration_ms) /
4255+
NULLIF(SUM(ps.total_executions), 0),
4256+
min_duration_ms =
4257+
MIN(ps.min_duration_ms),
4258+
max_duration_ms =
4259+
MAX(ps.max_duration_ms),
4260+
total_physical_reads_mb =
4261+
SUM(ps.total_physical_reads_mb),
4262+
avg_physical_reads_mb =
4263+
SUM(ps.total_physical_reads_mb) /
4264+
NULLIF(SUM(ps.total_executions), 0),
4265+
min_physical_reads_mb =
4266+
MIN(ps.min_physical_reads_mb),
4267+
max_physical_reads_mb =
4268+
MAX(ps.max_physical_reads_mb),
4269+
total_writes_mb =
4270+
SUM(ps.total_writes_mb),
4271+
avg_writes_mb =
4272+
SUM(ps.total_writes_mb) /
4273+
NULLIF(SUM(ps.total_executions), 0),
4274+
min_writes_mb =
4275+
MIN(ps.min_writes_mb),
4276+
max_writes_mb =
4277+
MAX(ps.max_writes_mb),
4278+
total_memory_mb =
4279+
SUM(ps.total_memory_mb),
4280+
avg_memory_mb =
4281+
SUM(ps.total_memory_mb) /
4282+
NULLIF(SUM(ps.total_executions), 0),
4283+
min_memory_mb =
4284+
MIN(ps.min_memory_mb),
4285+
max_memory_mb =
4286+
MAX(ps.max_memory_mb),
4287+
max_dop =
4288+
MAX(ps.max_dop)
4289+
FROM #hi_plan_stats AS ps
4290+
JOIN ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
4291+
ON qsp.plan_id = ps.plan_id
4292+
JOIN ' + @database_name_quoted + N'.sys.query_store_query AS qsq
4293+
ON qsq.query_id = qsp.query_id
4294+
GROUP BY
41624295
qsq.query_hash
4163-
HAVING
4164-
SUM(qsrs.count_executions) > 0
41654296
OPTION(RECOMPILE);' + @nc10;
41664297

41674298
IF @debug = 1
@@ -4200,11 +4331,7 @@ OPTION(RECOMPILE);' + @nc10;
42004331
max_dop
42014332
)
42024333
EXECUTE sys.sp_executesql
4203-
@sql,
4204-
N'@start_date datetimeoffset(7),
4205-
@end_date datetimeoffset(7)',
4206-
@start_date,
4207-
@end_date;
4334+
@sql;
42084335

42094336
IF @troubleshoot_performance = 1
42104337
BEGIN
@@ -5613,7 +5740,7 @@ OUTER APPLY
56135740
1/0
56145741
FROM #hi_id_staging_plans AS sp
56155742
WHERE qsp.plan_id = sp.plan_id
5616-
AND sp.query_hash = o.query_hash
5743+
AND o.query_hash = sp.query_hash
56175744
)
56185745
) AS qp0
56195746
WHERE qp0.n = 1

0 commit comments

Comments
 (0)