虽然我认为虽然 Barguast 发布的最终查询可能有效,但它们仍然存在问题/解释得不够好。
基本上0index_id
是堆,1是聚集索引,2是其他所有索引(非聚集索引)。
上述查询的问题是,如果表是堆(即使表中有数据),对数据的查询将不起作用。此外,索引查询也有效,因为您指定了index_Id = 2
and 由于没有加入index_id
betweensys.indexes
和而存在欺骗sys.partitions
。如果你加入这些,那么结果集中就不会出现重复,你可以做的更容易理解index_id not in (0,1)
。
无论如何,固定查询如下。我还在第一个查询中添加了索引名称(请注意,如果表是堆,则此字段将为空)。另请注意,您不必index_id
在第一个查询中指定连接,因为where
指定(0,1)
并且只能是其中一个(换句话说,如果您愿意,您可以添加它,但它没有任何区别)。
-- Data (table) compression (heap or clustered index)
SELECT [t].[name] AS [Table],
[i].[name] AS [Index],
[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]
INNER JOIN sys.indexes AS [i]
ON [i].[object_id] = [p].[object_id]
WHERE [p].[index_id] in (0,1)
-- Index compression (non-clustered index)
SELECT [t].[name] AS [Table],
[i].[name] AS [Index],
[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]
INNER JOIN sys.indexes AS [i]
ON [i].[object_id] = [p].[object_id] AND i.index_id = p.index_id
WHERE [p].[index_id] not in (0,1)