是否有人碰巧有一个通用的SQL语句,它会列出数据库中的所有表和索引,以及每个分区的当前压缩设置?
感谢。
编辑:这是我尝试查询表时所得到的,但我不确定连接是否正确(我得到重复,这似乎是由索引的存在引起的)
SELECT [t].[name], [p].[partition_number], [p].[data_compression_desc]
FROM [sys].[partitions] AS [p]
INNER JOIN sys.tables AS [t] ON [t].[object_id] = [p].[object_id]
答案 0 :(得分:28)
我以为我会分享我的最终查询。这将给出两个结果集,第一个是堆和聚簇索引的数据压缩,第二个是非聚簇索引的索引压缩。
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 [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] > 1
答案 1 :(得分:9)
虽然我认为虽然Barguast发布的最终查询可能会有效,但仍然存在问题/未解释的问题。
基本上index_id
0 是堆, 1 是聚集索引, 2 是其他所有内容(非聚集索引)。
上述查询的问题是,如果表是堆(即使表中有数据),对数据的查询将不起作用。此外,对索引的查询也起作用,因为您指定了index_Id = 2
,并且由于未加入index_id
和sys.indexes
之间的sys.partitions
而存在欺骗行为。如果你加入那些,那么结果集中就不会有重复项,你可以做更容易理解的index_id not in (0,1)
。
无论如何固定查询如下。我还在第一个查询中添加了索引名称(请注意,如果表是堆,则此字段将为null)。另请注意,您不必在第一个查询中指定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)
答案 2 :(得分:6)
这些答案都很体面,也很有效。由于我为我的工作点缀了一点,我觉得是时候回馈一下了。这个查询添加了Jason的答案(我需要的)。它还解决了一些连接问题,并将结果组合成一个非常简单的摘要。
-- Returns user tables and indexes in a DB and their Compression state
select s.name [Schema], t.name [Table], i.name [Index], p.data_compression_desc Compression
, case when p.index_id in (0, 1) then 'Table' else 'Index' end CompressionObject
from sys.tables t
join sys.schemas s on t.schema_id = s.schema_id
join sys.indexes i on t.object_id = i.object_id
join sys.partitions p on (i.object_id = p.object_id and i.index_id = p.index_id)
where t.type = 'U'
order by 1, 2, p.index_id, 3
我使用它作为“工作列表”来生成压缩所有内容的脚本,因为我只是将数据库提升到Azure VM并希望降低IOPS以提高性能。希望这有助于那里的人。
答案 3 :(得分:2)
这应该完成这项工作,测试一小部分以确保它能满足您的需求
SELECT DISTINCT s.name [Schema], t.name [Table], i.name [Index Name], p.partition_number, p.data_compression_desc
-- uncommenting the below line will give you dupes
--, p.index_id
FROM sys.schemas s
INNER JOIN sys.tables t
ON s.schema_id = t.schema_id
INNER JOIN sys.indexes i
ON t.object_id = i.object_id
INNER JOIN sys.partitions p
ON t.object_id = p.object_id
ORDER BY s.name, t.name
您可能获得欺骗的原因是因为每个表有多个分区记录,例如多个index_id,请参阅此MSDN文章,以了解index_id的含义。添加 DISTINCT 应该可以解决欺骗问题