Skip to content

Commit 17e5f05

Browse files
committed
#3085 sp_BlitzIndex too many missing indexes
Don't query missing index plans if there are over 1000. Closes #3085.
1 parent 1c5488c commit 17e5f05

1 file changed

Lines changed: 47 additions & 31 deletions

File tree

sp_BlitzIndex.sql

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ DECLARE @ColumnList NVARCHAR(MAX);
130130
DECLARE @ColumnListWithApostrophes NVARCHAR(MAX);
131131
DECLARE @PartitionCount INT;
132132
DECLARE @OptimizeForSequentialKey BIT = 0;
133+
DECLARE @StringToExecute NVARCHAR(MAX);
134+
133135

134136
/* Let's get @SortOrder set to lower case here for comparisons later */
135137
SET @SortOrder = REPLACE(LOWER(@SortOrder), N' ', N'_');
@@ -256,7 +258,8 @@ IF OBJECT_ID('tempdb..#Ignore_Databases') IS NOT NULL
256258
index_usage_summary NVARCHAR(MAX) NULL,
257259
index_size_summary NVARCHAR(MAX) NULL,
258260
create_tsql NVARCHAR(MAX) NULL,
259-
more_info NVARCHAR(MAX)NULL
261+
more_info NVARCHAR(MAX) NULL,
262+
sample_query_plan XML NULL
260263
);
261264

262265
CREATE TABLE #IndexSanity
@@ -1695,39 +1698,50 @@ BEGIN TRY
16951698
AND ColumnNamesWithDataTypes.IndexColumnType = ''Included''
16961699
) AS included_columns_with_data_type '
16971700

1698-
/* Github #2780 BGO Removing SQL Server 2019's new missing index sample plan because it doesn't work yet:*/
1701+
/* Get the sample query plan if it's available, and if there are less than 1,000 rows in the DMV: */
16991702
IF NOT EXISTS
17001703
(
17011704
SELECT
17021705
1/0
17031706
FROM sys.all_objects AS o
17041707
WHERE o.name = 'dm_db_missing_index_group_stats_query'
17051708
)
1706-
SELECT
1707-
@dsql += N' , NULL AS sample_query_plan '
1708-
/* Github #2780 BGO Removing SQL Server 2019's new missing index sample plan because it doesn't work yet:*/
1709+
SELECT
1710+
@dsql += N' , NULL AS sample_query_plan '
17091711
ELSE
17101712
BEGIN
1711-
SELECT
1712-
@dsql += N'
1713-
, sample_query_plan =
1714-
(
1715-
SELECT TOP (1)
1716-
p.query_plan
1717-
FROM sys.dm_db_missing_index_group_stats gs
1718-
CROSS APPLY
1719-
(
1720-
SELECT TOP (1)
1721-
s.plan_handle
1722-
FROM sys.dm_db_missing_index_group_stats_query q
1723-
INNER JOIN sys.dm_exec_query_stats s
1724-
ON q.query_plan_hash = s.query_plan_hash
1725-
WHERE gs.group_handle = q.group_handle
1726-
ORDER BY (q.user_seeks + q.user_scans) DESC, s.total_logical_reads DESC
1727-
) q2
1728-
CROSS APPLY sys.dm_exec_query_plan(q2.plan_handle) p
1729-
WHERE ig.index_group_handle = gs.group_handle
1730-
) '
1713+
/* The DMV is only supposed to have 600 rows in it. If it's got more,
1714+
they could see performance slowdowns - see Github #3085. */
1715+
DECLARE @MissingIndexPlans BIGINT;
1716+
SET @StringToExecute = N'SELECT @MissingIndexPlans = COUNT(*) FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_missing_index_group_stats_query;'
1717+
EXEC sp_executesql @StringToExecute, N'@MissingIndexPlans BIGINT OUT', @MissingIndexPlans;
1718+
1719+
IF @MissingIndexPlans > 1000
1720+
BEGIN
1721+
SELECT @dsql += N' , NULL AS sample_query_plan /* Over 1000 plans found, skipping */ ';
1722+
RAISERROR (N'Over 1000 plans found in sys.dm_db_missing_index_group_stats_query - your SQL Server is hitting a bug: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/3085',0,1) WITH NOWAIT;
1723+
END
1724+
ELSE
1725+
SELECT
1726+
@dsql += N'
1727+
, sample_query_plan =
1728+
(
1729+
SELECT TOP (1)
1730+
p.query_plan
1731+
FROM sys.dm_db_missing_index_group_stats gs
1732+
CROSS APPLY
1733+
(
1734+
SELECT TOP (1)
1735+
s.plan_handle
1736+
FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_missing_index_group_stats_query q
1737+
INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.dm_exec_query_stats s
1738+
ON q.query_plan_hash = s.query_plan_hash
1739+
WHERE gs.group_handle = q.group_handle
1740+
ORDER BY (q.user_seeks + q.user_scans) DESC, s.total_logical_reads DESC
1741+
) q2
1742+
CROSS APPLY sys.dm_exec_query_plan(q2.plan_handle) p
1743+
WHERE ig.index_group_handle = gs.group_handle
1744+
) '
17311745
END
17321746

17331747

@@ -3596,10 +3610,10 @@ BEGIN;
35963610
AND i.is_disabled = 0
35973611
GROUP BY i.database_id, i.schema_name, i.[object_id])
35983612
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
3599-
index_usage_summary, index_size_summary, create_tsql, more_info )
3613+
index_usage_summary, index_size_summary, create_tsql, more_info, sample_query_plan )
36003614

36013615
SELECT check_id, t.index_sanity_id, t.check_id, t.findings_group, t.finding, t.[Database Name], t.URL, t.details, t.[definition],
3602-
index_estimated_impact, t.index_size_summary, create_tsql, more_info
3616+
index_estimated_impact, t.index_size_summary, create_tsql, more_info, sample_query_plan
36033617
FROM
36043618
(
36053619
SELECT ROW_NUMBER() OVER (ORDER BY magic_benefit_number DESC) AS rownum,
@@ -3623,7 +3637,8 @@ BEGIN;
36233637
mi.create_tsql,
36243638
mi.more_info,
36253639
magic_benefit_number,
3626-
mi.is_low
3640+
mi.is_low,
3641+
mi.sample_query_plan
36273642
FROM #MissingIndexes mi
36283643
LEFT JOIN index_size_cte sz ON mi.[object_id] = sz.object_id
36293644
AND mi.database_id = sz.database_id
@@ -5002,7 +5017,8 @@ BEGIN;
50025017
br.index_size_summary AS [Size],
50035018
COALESCE(br.more_info,sn.more_info,'') AS [More Info],
50045019
br.URL,
5005-
COALESCE(br.create_tsql,ts.create_tsql,'') AS [Create TSQL]
5020+
COALESCE(br.create_tsql,ts.create_tsql,'') AS [Create TSQL],
5021+
br.sample_query_plan AS [Sample Query Plan]
50065022
FROM #BlitzIndexResults br
50075023
LEFT JOIN #IndexSanity sn ON
50085024
br.index_sanity_id=sn.index_sanity_id
@@ -5027,7 +5043,8 @@ BEGIN;
50275043
br.index_size_summary AS [Size],
50285044
COALESCE(br.more_info,sn.more_info,'') AS [More Info],
50295045
br.URL,
5030-
COALESCE(br.create_tsql,ts.create_tsql,'') AS [Create TSQL]
5046+
COALESCE(br.create_tsql,ts.create_tsql,'') AS [Create TSQL],
5047+
br.sample_query_plan AS [Sample Query Plan]
50315048
FROM #BlitzIndexResults br
50325049
LEFT JOIN #IndexSanity sn ON
50335050
br.index_sanity_id=sn.index_sanity_id
@@ -5119,7 +5136,6 @@ ELSE IF (@Mode=1) /*Summarize*/
51195136
DECLARE @LinkedServerDBCheck NVARCHAR(2000);
51205137
DECLARE @ValidLinkedServerDB INT;
51215138
DECLARE @tmpdbchk TABLE (cnt INT);
5122-
DECLARE @StringToExecute NVARCHAR(MAX);
51235139

51245140
IF @OutputServerName IS NOT NULL
51255141
BEGIN

0 commit comments

Comments
 (0)