Skip to content

Commit a1f4c03

Browse files
authored
Merge pull request #3044 from rsocol/dev
Improvement for issue #3043
2 parents e5a589e + 9d24e32 commit a1f4c03

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@ test-results/*
131131
x64/
132132
~$*
133133
~*.*
134+
/.vs

sp_BlitzIndex.sql

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ DECLARE @LineFeed NVARCHAR(5);
127127
DECLARE @DaysUptimeInsertValue NVARCHAR(256);
128128
DECLARE @DatabaseToIgnore NVARCHAR(MAX);
129129
DECLARE @ColumnList NVARCHAR(MAX);
130+
DECLARE @ColumnListWithApostrophes NVARCHAR(MAX);
130131
DECLARE @PartitionCount INT;
131132
DECLARE @OptimizeForSequentialKey BIT = 0;
132133

@@ -2876,9 +2877,9 @@ BEGIN
28762877
SET @dsql = N'USE ' + QUOTENAME(@DatabaseName) + N';
28772878
IF EXISTS(SELECT * FROM ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_row_groups WHERE object_id = @ObjectID)
28782879
BEGIN
2879-
SET @ColumnList = N'''';
2880+
SELECT @ColumnList = N'''', @ColumnListWithApostrophes = N'''';
28802881
WITH DistinctColumns AS (
2881-
SELECT DISTINCT QUOTENAME(c.name) AS column_name, c.column_id
2882+
SELECT DISTINCT QUOTENAME(c.name) AS column_name, QUOTENAME(c.name,'''''''') AS ColumnNameWithApostrophes, c.column_id
28822883
FROM ' + QUOTENAME(@DatabaseName) + N'.sys.partitions p
28832884
INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns c ON p.object_id = c.object_id
28842885
INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id AND ic.index_id = p.index_id
@@ -2887,9 +2888,10 @@ BEGIN
28872888
AND EXISTS (SELECT * FROM ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_segments seg WHERE p.partition_id = seg.partition_id AND seg.column_id = ic.index_column_id)
28882889
AND p.data_compression IN (3,4)
28892890
)
2890-
SELECT @ColumnList = @ColumnList + column_name + N'', ''
2891-
FROM DistinctColumns
2892-
ORDER BY column_id;
2891+
SELECT @ColumnList = @ColumnList + column_name + N'', '',
2892+
@ColumnListWithApostrophes = @ColumnListWithApostrophes + ColumnNameWithApostrophes + N'', ''
2893+
FROM DistinctColumns
2894+
ORDER BY column_id;
28932895
SELECT @PartitionCount = COUNT(1) FROM ' + QUOTENAME(@DatabaseName) + N'.sys.partitions WHERE object_id = @ObjectID AND data_compression IN (3,4);
28942896
END';
28952897

@@ -2907,18 +2909,19 @@ BEGIN
29072909
PRINT SUBSTRING(@dsql, 36000, 40000);
29082910
END;
29092911

2910-
EXEC sp_executesql @dsql, N'@ObjectID INT, @ColumnList NVARCHAR(MAX) OUTPUT, @PartitionCount INT OUTPUT', @ObjectID, @ColumnList OUTPUT, @PartitionCount OUTPUT;
2912+
EXEC sp_executesql @dsql, N'@ObjectID INT, @ColumnList NVARCHAR(MAX) OUTPUT, @ColumnListWithApostrophes NVARCHAR(MAX) OUTPUT, @PartitionCount INT OUTPUT', @ObjectID, @ColumnList OUTPUT, @ColumnListWithApostrophes OUTPUT, @PartitionCount OUTPUT;
29112913

29122914
IF @PartitionCount < 2
29132915
SET @ShowPartitionRanges = 0;
29142916

29152917
IF @Debug = 1
2916-
SELECT @ColumnList AS ColumnstoreColumnList, @PartitionCount AS PartitionCount, @ShowPartitionRanges AS ShowPartitionRanges;
2918+
SELECT @ColumnList AS ColumnstoreColumnList, @ColumnListWithApostrophes AS ColumnstoreColumnListWithApostrophes, @PartitionCount AS PartitionCount, @ShowPartitionRanges AS ShowPartitionRanges;
29172919

29182920
IF @ColumnList <> ''
29192921
BEGIN
29202922
/* Remove the trailing comma */
29212923
SET @ColumnList = LEFT(@ColumnList, LEN(@ColumnList) - 1);
2924+
SET @ColumnListWithApostrophes = LEFT(@ColumnListWithApostrophes, LEN(@ColumnListWithApostrophes) - 1);
29222925

29232926
SET @dsql = N'USE ' + QUOTENAME(@DatabaseName) + N';
29242927
SELECT partition_number, '
@@ -2960,7 +2963,8 @@ BEGIN
29602963
LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partition_range_values prvs ON prvs.function_id = pf.function_id AND prvs.boundary_id = p.partition_number - 1
29612964
LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partition_range_values prve ON prve.function_id = pf.function_id AND prve.boundary_id = p.partition_number ' ELSE N' ' END
29622965
+ N' LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_segments seg ON p.partition_id = seg.partition_id AND ic.index_column_id = seg.column_id AND rg.row_group_id = seg.segment_id
2963-
WHERE rg.object_id = @ObjectID'
2966+
WHERE rg.object_id = @ObjectID
2967+
AND c.name IN ( ' + @ColumnListWithApostrophes + N')'
29642968
+ CASE WHEN @ShowPartitionRanges = 1 THEN N'
29652969
) AS y ' ELSE N' ' END + N'
29662970
) AS x

0 commit comments

Comments
 (0)