-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressionDemo.sql
More file actions
293 lines (220 loc) · 7.88 KB
/
compressionDemo.sql
File metadata and controls
293 lines (220 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
--Estimate Space for Savings
/*SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FactResellerSalesXL_Heap](
[ProductKey] [int] NOT NULL,
[OrderDateKey] [int] NOT NULL,
[DueDateKey] [int] NOT NULL,
[ShipDateKey] [int] NOT NULL,
[ResellerKey] [int] NOT NULL,
[EmployeeKey] [int] NOT NULL,
[PromotionKey] [int] NOT NULL,
[CurrencyKey] [int] NOT NULL,
[SalesTerritoryKey] [int] NOT NULL,
[SalesOrderNumber] [nvarchar](20) NOT NULL,
[SalesOrderLineNumber] [tinyint] NOT NULL,
[RevisionNumber] [tinyint] NULL,
[OrderQuantity] [smallint] NULL,
[UnitPrice] [money] NULL,
[ExtendedAmount] [money] NULL,
[UnitPriceDiscountPct] [float] NULL,
[DiscountAmount] [float] NULL,
[ProductStandardCost] [money] NULL,
[TotalProductCost] [money] NULL,
[SalesAmount] [money] NULL,
[TaxAmt] [money] NULL,
[Freight] [money] NULL,
[CarrierTrackingNumber] [nvarchar](25) NULL,
[CustomerPONumber] [nvarchar](25) NULL,
[OrderDate] [datetime] NULL,
[DueDate] [datetime] NULL,
[ShipDate] [datetime] NULL
) ON [PRIMARY]
GO
*/
USE ColumnstoreDemo
GO
EXEC sp_estimate_data_compression_savings 'DBO', 'FactResellerSalesXL_Heap', NULL,
NULL, 'ROW';
EXEC sp_estimate_data_compression_savings 'DBO', 'FactResellerSalesXL_Heap', NULL,
NULL, 'PAGE';
--Checks for Percentage of Update Statements
SELECT o.name AS [Table_Name] ,
x.name AS [Index_Name] ,
i.partition_number AS [Partition] ,
i.index_id AS [Index_ID] ,
x.type_desc AS [Index_Type] ,
i.leaf_update_count * 100.0 / ( i.range_scan_count
+ i.leaf_insert_count
+ i.leaf_delete_count
+ i.leaf_update_count
+ i.leaf_page_merge_count
+ i.singleton_lookup_count ) AS [Percent_Update]
FROM sys.dm_db_index_operational_stats(DB_ID(), NULL, NULL, NULL) i
JOIN sys.objects o ON o.object_id = i.object_id
JOIN sys.indexes x ON x.object_id = i.object_id
AND x.index_id = i.index_id
WHERE ( i.range_scan_count + i.leaf_insert_count + i.leaf_delete_count
+ leaf_update_count + i.leaf_page_merge_count
+ i.singleton_lookup_count ) != 0
AND OBJECTPROPERTY(i.object_id, 'IsUserTable') = 1
ORDER BY [Percent_Update] DESC;
--Checks for Percentage of Scans
SELECT o.name AS [Table_Name] ,
x.name AS [Index_Name] ,
i.partition_number AS [Partition] ,
i.index_id AS [Index_ID] ,
x.type_desc AS [Index_Type] ,
i.range_scan_count * 100.0 / ( i.range_scan_count
+ i.leaf_insert_count
+ i.leaf_delete_count
+ i.leaf_update_count
+ i.leaf_page_merge_count
+ i.singleton_lookup_count ) AS [Percent_Scan]
FROM sys.dm_db_index_operational_stats(DB_ID(), NULL, NULL, NULL) i
JOIN sys.objects o ON o.object_id = i.object_id
JOIN sys.indexes x ON x.object_id = i.object_id
AND x.index_id = i.index_id
WHERE ( i.range_scan_count + i.leaf_insert_count + i.leaf_delete_count
+ leaf_update_count + i.leaf_page_merge_count
+ i.singleton_lookup_count ) != 0
AND OBJECTPROPERTY(i.object_id, 'IsUserTable') = 1
ORDER BY [Percent_Scan] DESC
--TurnOn Compression
/*
ALTER TABLE [dbo].[FactResellerSalesXL_HeapHistory] REBUILD PARTITION = ALL
WITH
(DATA_COMPRESSION = PAGE
);
ALTER INDEX [compression_index] ON [dbo].[FactResellerSalesXL_HeapHistory]
REBUILD PARTITION = ALL WITH
(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, DATA_COMPRESSION = PAGE);
ALTER TABLE [dbo].[FactResellerSalesXL_Heap_row] REBUILD PARTITION = ALL
WITH
(DATA_COMPRESSION = ROW
);
ALTER INDEX [compression_index_row] ON [dbo].[FactResellerSalesXL_HeapHistory_row]
REBUILD PARTITION = ALL WITH
(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, DATA_COMPRESSION = ROW);
*/
--Show Compression
SELECT b.name ,
a.object_id ,
a.data_compression,
A.data_compression_desc
FROM sys.partitions a ,
sys.objects b
WHERE a.data_compression <> 0
AND a.object_id = b.object_id
AND object_name(a.object_id) like '%FactResellerSalesXL%';
/* Indicates the state of compression for each partition:
0 = NONE
1 = ROW
2 = PAGE
3 = COLUMNSTORE
4 = COLUMNSTORE ARCHIVAL*/
--Show Pages
select * from sys.dm_db_database_page_allocations(db_id(),object_id('bigProduct'),0,null,'DETAILED');
dbcc traceon (3604)
--Uncompressed Page
dbcc page ('ColumnstoreDemo',1,143628,2)
--Compressed Page
dbcc page ('ColumnstoreDemo',1,511510,2)
--Set stats on
SET STATISTICS IO ON;
SET STATISTICS TIME ON;
--Run Initial Query
SELECT frs.orderdate,
p.EnglishProductName,
avg(frs.SalesAmount) AS [Avg_Sales_Amount]
FROM FactResellerSalesXL_Heap FRS
JOIN DimProduct P ON FRS.ProductKey = P.ProductKey
GROUP BY frs.orderdate,
p.EnglishProductName
ORDER BY AVS_Sales_Amount DESC;
--Show and Capture Statistics
EXEC dbo.show_buffers;
--Run Same Query Compressed
SELECT frs.orderdate,
p.EnglishProductName,
avg(frs.SalesAmount) AS [Avg_Sales_Amount]
FROM FactResellerSalesXL_PageCompressed FRS
JOIN DimProduct P ON FRS.ProductKey = P.ProductKey
GROUP BY frs.orderdate,
p.EnglishProductName
ORDER BY AVS_Sales_Amount DESC;
--Capture Statistics and Buffers
EXEC dbo.show_buffers;
--Show Cost of Update (Single Row)
UPDATE dbo.FactResellerSalesXL_Heap
SET UnitPrice = 27.293
WHERE SalesOrderNumber = 'SO45736';
UPDATE dbo.FactResellerSalesXL_Heap_row
SET UnitPrice = 27.293
WHERE SalesOrderNumber = 'SO45736';
UPDATE dbo.FactResellerSalesXL_Heap
SET UnitPrice = 27.293
WHERE SalesOrderNumber = 'SO45736';
--Show Cost of Update (Bulk)
--Page Compressed
BEGIN TRANSACTION
UPDATE dbo.FactResellerSalesXL_Page
SET UnitPrice = 27.293
ROLLBACK TRANSACTION
--Columnstore Compressed
BEGIN TRANSACTION
UPDATE dbo.FactResellerSalesXL_CCI
SET UnitPrice = 27.293
ROLLBACK TRANSACTION
--Uncompressed
BEGIN TRANSACTION
UPDATE dbo.FactResellerSalesXL_Heap
SET UnitPrice = 27.293
ROLLBACK TRANSACTION
--Inline Compression Demos
CREATE TABLE People (
_id int primary key identity,
name nvarchar(max),
surname nvarchar(max),
info varbinary(max)
)
INSERT INTO People (name, surname, info)
SELECT FirstName, LastName, COMPRESS(AdditionalContactInfo) FROM AdventureWorks2016CTP3.Person.Person
SELECT name, surname, DECOMPRESS(info) AS original
FROM People
--As an alternative, we can add computed column (non-persisted) that dynamically decompress data:
ALTER TABLE People
ADD info_text as CAST( DECOMPRESS(info) AS NVARCHAR(MAX))
--Columnstore Demos
--Show Small Load versus Large Load
TRUNCATE TABLE cs_load_demo;
INSERT INTO cs_load_demo SELECT TOP 102399 * FROM dbo.FactResellerSalesXL_Heap_cs;
INSERT INTO cs_load_demo SELECT TOP 1024000 * FROM dbo.FactResellerSalesXL_Heap_cs;
SELECT * FROM sys.column_store_row_groups WHERE object_id=885578193
--Show query performance without Columnstore Index
SELECT transactiondate
,avg(quantity)
,avg(actualcost)
FROM FactResellerSalesXL_Heap
WHERE TransactionDate < '2007-07-01'
AND Quantity > 70
AND Quantity < 92
GROUP BY TransactionDate
ORDER BY transactionDate;
SELECT * FROM dbo.FactResellerSalesXL_Heap_cs
--Show query performance with Columnstore Index
SELECT transactiondate
,avg(quantity)
,avg(actualcost)
FROM dbo.FactResellerSalesXL_Heap_cs
WHERE TransactionDate < '2007-07-01'
AND Quantity > 70
AND Quantity < 92
GROUP BY TransactionDate
ORDER BY transactionDate;
EXEC dbo.show_buffers
EXEC dbo.show_CS_Buffer