Skip to content

Commit

Permalink
added index-level details
Browse files Browse the repository at this point in the history
  • Loading branch information
EitanBlumin committed Jan 8, 2024
1 parent 4d3face commit fcc230a
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions Utility Scripts/Find what could slow down SHRINK operations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ DECLARE
@TableSizeThresholdMB INT = 500


DECLARE @RebuildOptions nvarchar(max) = N'';

IF ISNULL(CONVERT(int, SERVERPROPERTY('EngineEdition')),0) IN (3,5,8)
BEGIN
SET @RebuildOptions = N' WITH (ONLINE=ON)'
END
ELSE
BEGIN
SET @RebuildOptions = N''
END

;WITH TabsCTE
AS
(
SELECT DISTINCT
'Table with LOB or ROW-OVERFLOW data' AS Issue,
p.object_id
,p.index_id
FROM sys.system_internals_allocation_units au
JOIN sys.partitions p ON au.container_id = p.partition_id
WHERE type_desc <> 'IN_ROW_DATA' AND total_pages > 8
Expand All @@ -30,6 +42,7 @@ UNION ALL
SELECT
'Heap with Non-clustered indexes',
p.object_id
,p.index_id
FROM sys.partitions AS p
WHERE p.index_id = 0
AND p.rows > 0
Expand All @@ -40,6 +53,7 @@ UNION ALL
SELECT DISTINCT
'Partitioned Heap',
p.object_id
,p.index_id
FROM sys.partitions AS p
WHERE p.index_id = 0
AND p.rows > 0
Expand All @@ -48,13 +62,17 @@ AND p.partition_number > 1
SELECT t.*,
OBJECT_SCHEMA_NAME(t.object_id) table_schema,
OBJECT_NAME(t.object_id) table_name,
ix.name AS index_name,
SUM(p.rows) AS RowCounts,
CAST(ROUND((SUM(a.used_pages) / 128.00), 2) AS NUMERIC(36, 2)) AS Used_MB,
CAST(ROUND((SUM(a.total_pages) - SUM(a.used_pages)) / 128.00, 2) AS NUMERIC(36, 2)) AS Unused_MB,
CAST(ROUND((SUM(a.total_pages) / 128.00), 2) AS NUMERIC(36, 2)) AS Total_MB
CAST(ROUND((SUM(a.total_pages) / 128.00), 2) AS NUMERIC(36, 2)) AS Total_MB,
RebuildCmd = N'ALTER ' + ISNULL(N'INDEX ' + QUOTENAME(ix.name) + N' ON ', N'TABLE ') + QUOTENAME(OBJECT_SCHEMA_NAME(t.object_id)) + N'.' + QUOTENAME(OBJECT_NAME(t.object_id)) + N' REBUILD' + @RebuildOptions + N';
GO'
FROM TabsCTE AS t
INNER JOIN sys.partitions p ON t.object_id = p.object_id
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
GROUP BY t.Issue, t.object_id
LEFT JOIN sys.indexes AS ix ON t.index_id = ix.index_id AND t.object_id = ix.object_id
GROUP BY t.Issue, t.object_id, t.index_id, ix.name
HAVING SUM(a.used_pages) / 128.00 >= @TableSizeThresholdMB
ORDER BY Used_MB DESC
ORDER BY Used_MB ASC

0 comments on commit fcc230a

Please sign in to comment.