|
| 1 | +--Observe the size of the table without the clustered columnstore index |
| 2 | +SELECT t.NAME AS TableName |
| 3 | + ,s.NAME AS SchemaName |
| 4 | + ,p.rows AS RowCounts |
| 5 | + ,SUM(a.total_pages) * 8 AS TotalSpaceKB |
| 6 | + ,CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB |
| 7 | + ,SUM(a.used_pages) * 8 AS UsedSpaceKB |
| 8 | + ,CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB |
| 9 | + ,(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB |
| 10 | + ,CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB |
| 11 | +FROM sys.tables t |
| 12 | +INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id |
| 13 | +INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID |
| 14 | + AND i.index_id = p.index_id |
| 15 | +INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id |
| 16 | +LEFT JOIN sys.schemas s ON t.schema_id = s.schema_id |
| 17 | +WHERE t.NAME = 'Order_Big_CCI' |
| 18 | +GROUP BY t.NAME |
| 19 | + ,s.NAME |
| 20 | + ,p.Rows |
| 21 | +ORDER BY t.NAME TotalSpaceMB 15710.48 |
| 22 | + |
| 23 | +--Run the query and observe the IO statistics and the execution time |
| 24 | +SET STATISTICS TIME |
| 25 | + ,IO ON |
| 26 | + |
| 27 | +SELECT dc.city |
| 28 | + ,avg(f.[Total Including Tax]) AS AvgOrderCost |
| 29 | +FROM fact.Order_Big_CCI f |
| 30 | +INNER JOIN Dimension.City dc ON dc.[City Key] = f.[City Key] |
| 31 | +GROUP BY dc.city |
| 32 | +HAVING avg(f.[Total Including Tax]) > 700 |
| 33 | +ORDER BY AvgOrderCost DESC |
| 34 | + |
| 35 | +--Create the Columnstore Index (Note: This will take 5-10 minutes) |
| 36 | + |
| 37 | +CREATE CLUSTERED COLUMNSTORE INDEX CCI_Orders ON Fact.Order_Big_CCI |
| 38 | + |
| 39 | + |
| 40 | +--Observe the size of the table wit the clustered columnstore index |
| 41 | +SELECT t.NAME AS TableName |
| 42 | + ,s.NAME AS SchemaName |
| 43 | + ,p.rows AS RowCounts |
| 44 | + ,SUM(a.total_pages) * 8 AS TotalSpaceKB |
| 45 | + ,CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB |
| 46 | + ,SUM(a.used_pages) * 8 AS UsedSpaceKB |
| 47 | + ,CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB |
| 48 | + ,(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB |
| 49 | + ,CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB |
| 50 | +FROM sys.tables t |
| 51 | +INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id |
| 52 | +INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID |
| 53 | + AND i.index_id = p.index_id |
| 54 | +INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id |
| 55 | +LEFT JOIN sys.schemas s ON t.schema_id = s.schema_id |
| 56 | +WHERE t.NAME = 'Order_Big_CCI' |
| 57 | +GROUP BY t.NAME |
| 58 | + ,s.NAME |
| 59 | + ,p.Rows |
| 60 | +ORDER BY t.NAME |
| 61 | + |
| 62 | +--Rerun the Query to Observe CS Performance Improvements |
| 63 | + |
| 64 | +SET STATISTICS TIME |
| 65 | + ,IO ON |
| 66 | + |
| 67 | +SELECT dc.city |
| 68 | + ,avg(f.[Total Including Tax]) AS AvgOrderCost |
| 69 | +FROM fact.Order_Big_CCI f |
| 70 | +INNER JOIN Dimension.City dc ON dc.[City Key] = f.[City Key] |
| 71 | +GROUP BY dc.city |
| 72 | +HAVING avg(f.[Total Including Tax]) > 700 |
| 73 | +ORDER BY AvgOrderCost DESC |
0 commit comments