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?
Thanks.
EDIT: This is as far as I got in my attempt to query tables, but I'm not sure the join is correct (I'm getting duplicates, which seems to be caused by the presence of indexes)
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]
5条答案
按热度按时间lh80um4z1#
I thought I'd share my final query. This'll give two result sets, the first of data compression for heaps and clustered indexes, and the second of index compression for non-clustered indexes.
5sxhfpxr2#
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 theindex_id
betweensys.indexes
andsys.partitions
. If you join on those then there will not be duplicates in the result set and you can do the much more understandableindex_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 thewhere
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).mbjcgjjk3#
These answers are all decent and work. Since I embellished it a bit for my work, I figured is was about time for to contribute back a bit. This query adds the schema from Jason's answer (which I needed). and it also sorts out some of the join issues and combines the results into a pretty simple summary.
I used this as a "work list" to generate scripts to compress everything since I just lift-shift the db into an Azure VM and wanting to reduce IOPS to improve perf. Hope this helps somebody out there.
x8diyxa74#
This should do the job, test it for a small subset to be sure it gives you what you need
The reason you are probably getting dupes is because you have multiple partition records per table, e.g. multiple index_id, see this MSDN article for clarification on what the index_id's mean. Adding a DISTINCT should solve the problem of the dupes
disho6za5#
I prepared below SQL for myself. Also it can be useful for you. Here you are.