SELECT OBJECT_SCHEMA_NAME(ps.[object_id]) AS schemaName,
OBJECT_NAME(ps.[object_id]) AS tableName,
ps.partition_number,
p.data_compression_desc,
FORMAT(SUM(ps.row_group_id), 'N0') AS row_group_count,
FORMAT(SUM(ps.total_rows), 'N0') AS row_count,
FORMAT(SUM(ps.deleted_rows), 'N0') AS deleted_rows,
FORMAT(SUM(ps.size_in_bytes) / 1024 / 1024, 'N0') AS size_in_mbytes,
FORMAT(SUM(ps.size_in_bytes) / sum(total_rows), 'N0') AS size_per_row,
CONCAT('ALTER INDEX [', i.[name], '] ON ', OBJECT_SCHEMA_NAME(ps.[object_id]), '.', OBJECT_NAME(ps.[object_id]),
' REBUILD PARTITION = ', ps.partition_number, ' WITH (DATA_COMPRESSION = COLUMNSTORE, ONLINE = ON, SORT_IN_TEMPDB = ON /*, MAXDOP = 8 */);
GO') AS rebuiltCommand
FROM sys.dm_db_column_store_row_group_physical_stats AS ps
INNER JOIN sys.indexes AS i ON ps.[object_id] = i.[object_id] AND ps.index_id = i.index_id
INNER JOIN sys.partitions p ON ps.partition_number = p.partition_number AND ps.[object_id] = p.[object_id] AND ps.index_id = p.index_id
GROUP BY ps.[object_id], ps.partition_number, p.data_compression_desc, i.[name]
ORDER BY schemaName, tableName, ps.partition_number;