-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSP_AutoCompression.sql
401 lines (260 loc) · 13.7 KB
/
SP_AutoCompression.sql
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
USE [Database]
GO
CREATE PROC [dbo].[SP_AutoCompression] AS
/***********************************************************************************************************************************
OBJECT NAME: AutCompression
DESCRIPTION: Automated Compression, first checks all tables on database, excludes those that are alredy compressed or too small,
then creates estimates for each table with page/row compression, makes decision to compress and which method, and
finally compresses all tables using page/row compression.
If the savings is at least 10% of space, then will compress. If page is 10% greater than row, then uses page.
***********************************************************************************************************************************/
--------------------------------------------------------------------------------------------------------------
-- 1 CREATE TEMP TABLES/VARIABLES TO HOLD COMPRESSION ESTIMATES
--------------------------------------------------------------------------------------------------------------
SET NOCOUNT ON;
DECLARE @tableName VARCHAR(256)
, @schemaName VARCHAR(100)
, @sqlStatement VARCHAR(1000)
, @statusMsg VARCHAR(1000)
, @U_tableName VARCHAR(256)
, @U_schemaName VARCHAR(100)
, @compression VARCHAR (4)
, @sql_exec VarChar(1000);
IF object_id('tempdb..#u_tables')IS NOT NULL BEGIN DROP TABLE #u_tables END
if object_id('tempdb..#tables') is not null begin drop table #tables end
CREATE TABLE #tables
( schemaName sysname NULL
, tableName sysname NULL
, page_processed bit
, row_processed bit);
if object_id('tempdb..#updates') is not null begin drop table #updates end
CREATE TABLE #updates
( tableName sysname NULL
, spName sysname NULL)
if object_id('tempdb..#row_compression') is not null begin drop table #row_compression end
--IF EXISTS(SELECT * FROM tempdb.sys.tables WHERE name LIKE '%#row_compression%') DROP TABLE #row_compression;
CREATE TABLE #row_compression
( objectName varchar(100)
, schemaName varchar(50)
, index_id int
, partition_number int
, size_current_compression bigint
, size_requested_compression bigint
, sample_current_compression bigint
, sample_requested_compression bigint);
if object_id('tempdb..#page_compression') is not null begin drop table #page_compression end
CREATE TABLE #page_compression
( objectName varchar(100)
, schemaName varchar(50)
, index_id int
, partition_number int
, size_current_compression bigint
, size_requested_compression bigint
, sample_current_compression bigint
, sample_requested_compression bigint);
--------------------------------------------------------------------------------------------------------------
-- 2 GENERATE TABLE LIST FOR COMPRESSION ESTIMATES: Exclude (2.1) compressed, (2.2) small, (2.3)
--------------------------------------------------------------------------------------------------------------
PRINT '-------------------------------------------------------------------------------------'
PRINT 'PREP - Generating List of Tables and Exclusions'
PRINT '-------------------------------------------------------------------------------------'
PRINT ' '
INSERT INTO #tables
SELECT DISTINCT s.name AS schemaName, t.name AS tableName, 0, 0
FROM sys.tables T
JOIN sys.schemas s ON t.schema_id = s.schema_id
LEFT JOIN
(
-- 2.1 Exclude compressed tables
----------------------------------------------------------------------------------------------------------
SELECT DISTINCT SCHEMA_NAME(o.schema_id) as SchemaName,
OBJECT_NAME(o.object_id) as TableName,p.data_compression_desc AS CompressionType
FROM sys.partitions p
INNER JOIN sys.objects o ON p.object_id = o.object_id
WHERE p.data_compression > 0 AND SCHEMA_NAME(o.schema_id) <> 'SYS'
-------------------------------------------------------------------------------------
) Cx ON Cx.SchemaName=s.name AND Cx.TableName=t.Name
LEFT JOIN
(
-- 2.2 Exclude small tables
----------------------------------------------------------------------------------------------------------
SELECT *
FROM (
SELECT s.Name AS SchemaName, t.NAME AS TableName, SUM(a.used_pages)*8 AS UsedSpaceKB
FROM sys.tables t
INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id
GROUP BY t.Name, s.Name
) X
WHERE UsedSpaceKB < '99999'
-------------------------------------------------------------------------------------
) Sm ON Sm.SchemaName=s.Name AND Sm.TableName=t.Name
WHERE CX.TableName IS NULL AND SM.TableName IS NULL
SELECT * INTO #U_TABLES FROM #TABLES
-- 2.3 OPTION TO EXCLUDE PRODUCTION TABLES (Exclude objects that have frequent sproc writes, only include static tables)
--------------------------------------------------------------------------------------------------------------
/*
WHILE EXISTS(select * from #U_TABLES)
BEGIN
--A. Select a table to examine
----------------------------------------------------------------------------------------------------------
SELECT TOP 1 @u_tableName = tableName, @u_schemaName = schemaName
FROM #U_TABLES
--B. Examine if table has writes but no drop/recreates
----------------------------------------------------------------------------------------------------------
INSERT INTO #UPDATES
SELECT DISTINCT @u_tablename, object_name(id)
FROM SYS.syscomments
WHERE
( TEXT LIKE ('%INTO ' + 'dbo.' + @u_tablename + '%')
OR TEXT LIKE ('%DELETE FROM ' + 'dbo.' + @u_tablename + '%')
OR TEXT LIKE ('%UPDATE ' + 'dbo.' + @u_tablename + '%')
)
AND @U_tableName NOT IN (
SELECT DISTINCT @u_tablename AS u_tablename
FROM SYS.syscomments
WHERE TEXT LIKE ('%DROP TABLE ' + 'dbo.' + @u_tablename + '%')
)
--C. Update selection choices, remove table that was scanned in B
----------------------------------------------------------------------------------------------------------
DELETE FROM #U_TABLES WHERE @u_tableName = tableName AND @u_schemaName = schemaName
END
--2.4 Delete write-tables identified in loop from #tables
----------------------------------------------------------------------------------------------------------
DELETE FROM #TABLES
WHERE tablename in (SELECT DISTINCT tableName from #UPDATES)
*/
--------------------------------------------------------------------------------------------------------------
-- 3 ESTIMATE COMPRESSION FOR ALL TABLES FROM STEP 2
--------------------------------------------------------------------------------------------------------------
PRINT '-------------------------------------------------------------------------------------'
PRINT 'ESTIMATE '
PRINT '-------------------------------------------------------------------------------------'
-- 3.1 Process Row Compression
----------------------------------------------------------------------------------------------------------
WHILE EXISTS(SELECT * FROM #tables WHERE row_processed = 0)
BEGIN
--A. Pick first unproccessed table
----------------------------------------------------------------------------------------------------------
SELECT TOP 1 @tableName = tableName, @schemaName = schemaName
FROM #tables WHERE row_processed = 0;
--B. Create sp estimate string
----------------------------------------------------------------------------------------------------------
SET @sqlStatement = 'EXECUTE sp_estimate_data_compression_savings ''' +@schemaName+ ''', ''' +@tableName+ ''', NULL, NULL, ''ROW'';'
--C. Execute sp estimate string
----------------------------------------------------------------------------------------------------------
BEGIN
PRINT 'Row Estimate for ' + @tableName;
INSERT INTO #row_compression
EXECUTE sp_executesql @sqlStatement;
END;
--E. Update table as processed
----------------------------------------------------------------------------------------------------------
UPDATE #tables
SET row_processed = 1
WHERE tableName = @tableName AND schemaName = @schemaName;
END;
-- 3.2 Process Row Compression
----------------------------------------------------------------------------------------------------------
WHILE EXISTS(SELECT * FROM #tables WHERE page_processed = 0)
BEGIN
--A. Pick first unproccessed table
----------------------------------------------------------------------------------------------------------
SELECT TOP 1 @tableName = tableName, @schemaName = schemaName
FROM #tables WHERE page_processed = 0;
--C. Create sp estimate string
----------------------------------------------------------------------------------------------------------
SET @sqlStatement = 'EXECUTE sp_estimate_data_compression_savings ''' +@schemaName+ ''', ''' +@tableName+ ''', NULL, NULL, ''PAGE'';'
--D. Execute sp estimate string
----------------------------------------------------------------------------------------------------------
BEGIN
PRINT 'Page Estimtate for ' + @tableName;
INSERT INTO #page_compression
EXECUTE sp_executesql @sqlStatement;
END;
--E. Update table as processed
----------------------------------------------------------------------------------------------------------
UPDATE #tables
SET page_processed = 1
WHERE tableName = @tableName AND schemaName = @schemaName;
END;
--------------------------------------------------------------------------------------------------------------
-- 4. List estimated savings, make recommendations
--------------------------------------------------------------------------------------------------------------
IF OBJECT_ID('tempdb..#compression_estimates') IS NOT NULL DROP TABLE #compression_estimates
IF OBJECT_ID('tempdb..#ctables') IS NOT NULL DROP TABLE #ctables
-- 4.1 Compile row and page estimates
----------------------------------------------------------------------------------------------------------
SELECT pg.schemaName, pg.objectName as TableName, pg.size_current_compression as original_size, pg.page_size, pg.page_compression, rw.row_size, rw.row_compression
INTO #compression_estimates
FROM
(
SELECT schemaname, objectname, 'page' as kind, size_current_compression, size_requested_compression as page_size,
CASE WHEN size_current_compression = 0 THEN 0
WHEN size_requested_compression = 0 THEN 0
ELSE 1-CAST(size_requested_compression AS FLOAT)/size_current_compression
END AS page_compression
FROM #page_compression
WHERE index_id in (0,1)
) as pg
JOIN
(
SELECT schemaname, objectname, 'row' as kind, size_current_compression, size_requested_compression as row_size,
CASE WHEN size_current_compression = 0 THEN 0
WHEN size_requested_compression = 0 THEN 0
ELSE 1-CAST(size_requested_compression AS FLOAT)/size_current_compression
END AS row_compression
FROM #row_compression
WHERE index_id in (0,1)
) as rw
ON rw.schemaName=pg.schemaName AND rw.objectName=pg.objectName
-- 4.2 Determine whether row or page is ideal
----------------------------------------------------------------------------------------------------------
SELECT schemaName, TableName, original_size, page_size, row_size, page_compression, row_compression
,'Decision' = CASE WHEN page_compression > .299 AND (PAGE_compression-row_compression) > .1 THEN 'page'
WHEN row_compression > .19 AND (PAGE_compression-row_compression) <.2 THEN 'row'
ELSE 'na' END
,0 as compressed
INTO #ctables
FROM #compression_estimates
ORDER BY original_size
--drop table #compression_estimates
--drop table #ctables
--------------------------------------------------------------------------------------------------------------
-- 5. Compress tables based on algorithm in 4.2
--------------------------------------------------------------------------------------------------------------
PRINT '-------------------------------------------------------------------------------------'
PRINT 'COMPRESSING '
PRINT '-------------------------------------------------------------------------------------'
WHILE EXISTS (SELECT * FROM #CTABLES WHERE compressed = 0 AND decision IN ('ROW','PAGE'))
BEGIN
-- 5.1 Declare Table to be compressed
----------------------------------------------------------------------------------------------------------
SELECT TOP 1 @tableName = tableName, @schemaName = schemaName, @compression = decision
FROM #ctables WHERE compressed = 0 AND decision IN ('ROW','PAGE')
--select @tableName, @schemaName, @compression
-- 5.2 Compress as row or page
----------------------------------------------------------------------------------------------------------
IF @compression = 'row'
BEGIN
PRINT 'Row Compression on ' + @tableName;
SELECT @sql_exec = 'ALTER TABLE ' + @tableName +' REBUILD PARTITION = ALL WITH (DATA_COMPRESSION = ROW)';
Exec (@sql_exec)
--print @sql_exec
END
ELSE
BEGIN
PRINT 'Page Compression on ' + @tableName;
SELECT @sql_exec = 'ALTER TABLE ' + @tableName +' REBUILD PARTITION = ALL WITH (DATA_COMPRESSION = PAGE)';
Exec (@sql_exec)
--print @sql_exec
END
-- 5.3 Mark processed table for loop
----------------------------------------------------------------------------------------------------------
UPDATE #ctables
SET compressed = 1
WHERE @tableName = tableName AND @schemaName = schemaName AND @compression = decision
END
SELECT * FROM #ctables where compressed = 1