查询所有表数据和索引压缩

Bar*_*ast 17 sql-server sql-server-2012

有没有人碰巧有一个通用的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]
Run Code Online (Sandbox Code Playgroud)

Bar*_*ast 30

我以为我会分享我的最终查询.这将给出两个结果集,第一个表数据压缩,第二个表索引压缩.

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
Run Code Online (Sandbox Code Playgroud)


小智 11

虽然我认为虽然Barguast发布的最终查询可能会有效,但仍然存在问题/某些问题尚未得到充分解释.

基本上是index_id0是一个,1是一个簇索引2是一切(非聚集索引).

上述查询的问题是,如果表是堆(即使表中有数据),对数据的查询将不起作用.此外,对索引的查询也起作用,因为您指定了index_Id = 2并且由于没有连接index_id之间sys.indexes而存在dupes 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)
Run Code Online (Sandbox Code Playgroud)


小智 9

这些答案都很体面,也很有效.由于我为我的工作点缀了一点,我想是时候回馈一下了.这个查询添加了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
Run Code Online (Sandbox Code Playgroud)

我使用它作为"工作列表"来生成压缩所有内容的脚本,因为我只是将数据库提升到Azure VM并希望降低IOPS以提高性能.希望这有助于那里的人.


Jas*_*son 5

这应该可以完成工作,对一小部分进行测试,以确保它能够满足您的需求

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
Run Code Online (Sandbox Code Playgroud)

您可能受到欺骗的原因是因为每个表有多个分区记录,例如多个index_id,请参阅这篇MSDN文章以澄清index_id的含义。添加DISTINCT应​​该可以解决欺骗问题