-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndexCompressionType.sql
More file actions
42 lines (41 loc) · 1.43 KB
/
IndexCompressionType.sql
File metadata and controls
42 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
--SELECT [t].[name] AS [Table], [p].[partition_number] AS [Partition],
-- [p].[data_compression_desc] AS [Compression]
--FROM [sys].[partitions] AS [p]
--INNER JOIN sys.tables AS [t] ON [t].[object_id] = [p].[object_id]
--WHERE [p].[index_id] in (0,1)
SELECT
s.name AS [Schema],
[t].[name] AS [Table],
[i].[name] AS [Index],
[p].[partition_number] AS [Partition],
[p].[data_compression_desc] AS [Compression],
i.type_desc AS [Index Type],
si.rows AS [Index Rows],
'ALTER INDEX [' + i.name + '] ON [' + s.name + '].[' + t.name + '] REBUILD WITH (DATA_COMPRESSION=ROW)' AS RebuildSQL
FROM [sys].[partitions] AS [p] WITH (NOLOCK)
INNER JOIN sys.tables AS [t] WITH (NOLOCK)
ON [t].[object_id] = [p].[object_id]
INNER JOIN sys.schemas AS s WITH (NOLOCK)
ON
t.schema_id = s.schema_id
LEFT JOIN sys.indexes AS [i] WITH (NOLOCK)
INNER JOIN sys.sysindexes si WITH (NOLOCK)
ON
i.object_id = si.id
AND
i.index_id = si.indid
--INNER JOIN (VALUES (0, 'Heap'),
-- (1, 'Clustered rowstore (B-tree)'),
-- (2, 'Nonclustered rowstore (B-tree)'),
-- (3, 'XML'),
-- (4, 'Spatial'),
-- (5, 'Clustered columnstore index.'),
-- (6, 'Nonclustered columnstore index'),
-- (7, 'Nonclustered hash index') t(id,name)
--ON
-- i.type = t.id
ON [i].[object_id] = [p].[object_id] AND [i].[index_id] = [p].[index_id]
WHERE p.data_compression_desc <> 'ROW'
AND i.type_desc <> 'HEAP'
AND si.rows <= 10000
ORDER BY si.rows DESC