Query all table data and index compression

前端 未结 4 1674
星月不相逢
星月不相逢 2021-02-04 03:26

Does anyone happen to have a generic SQL statement that\'ll list all of the tables and indexes in a database, along with their current compression setting, for each partition?

4条回答
  •  心在旅途
    2021-02-04 04:20

    While I think while the final queries posted by Barguast may work, there is still a problem with them/something not explained well enough.

    Basically an index_id of 0 is a heap, 1 is a clustered index and 2 is everything else (non-clustered indexes).

    The problem with the above queries is that the query for the data will not work if the table is a heap (even though there is data in the table). Also the query for the indexes works because you specify the index_Id = 2 and there is dupes due to not joining the index_id between sys.indexes and sys.partitions. If you join on those then there will not be duplicates in the result set and you can do the much more understandable index_id not in (0,1).

    Anyways fixed queries are below. I also added index name to the first query (note this field will be null if the table is a heap). Also note that you don't have to specify the join for index_id on the first query, because the where specifies (0,1) and there can only be one of those (in other words you could add it if you like but it doesn't make a difference).

    -- 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)
    

提交回复
热议问题