sqlintersection putting the "squeeze" on large tables improve performance and save space...

30
SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry .com Tuesday, 2:15-3:30

Upload: rebecca-stanley

Post on 18-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

SQLintersectionPutting the "Squeeze" on Large

TablesImprove Performance and Save Space with

Data CompressionJustin Randall

[email protected]

Tuesday, 2:15-3:30

Page 2: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Overview

Why compress data in SQL Server? Data compression basics Deciding what to compress Planning your compression efforts Resource requirements, options, and side effects Summary

Page 3: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Why Compress Data?

Save Space on Disk & in Memory

Improve Performance

Page 4: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Enterprise Edition & Azure SQL DB Only!

Reference: http://msdn.microsoft.com/en-us/library/cc645993(v=sql.120).aspx#Scalability

Scalability and Performance

Page 5: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Data Compression Basics

Compression does not change syntax or semantics of data Application changes are not required Compression is applied to a table, index, or partition

A whole table stored as a heap A whole table stored as a clustered index A whole non-clustered index A whole indexed view Partition-specific compression on partitioned tables

Page 6: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Data Compression Basics

In-row data only Compression is applied at the data page level Compressed pages remain compressed in memory Fill factor is not affected Special cases: Unicode and Columnstore More rules and conditions in Books Online

http://msdn.microsoft.com/en-us/library/cc280449.aspx

Page 7: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Data Compression Types: Row

A storage optimization technique Reduces metadata overhead associated with the row Stores fixed-length data types in variable-length storage

format Applies to leaf-level & non-leaf-level pages of indexes Removes blank characters NULL and 0 values require no additional space

Effect on Storage by data type

https://msdn.microsoft.com/en-US/library/cc280576%28v=sql.120%29.aspx

Page 8: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Data Compression Types: Row

Highest gains: data pages containing rows with a lot of unused space NULL values fixed-length character and numeric values not needing max

defined space Examples

char(100) column with most values < 50 – 75 characters integer values substantially smaller than max size allowed

Page 9: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Data Compression Types: Page

A superset of row compression Seeks to minimize data redundancy Applies equally to all data types Applies to leaf-level pages of tables and indexes only Pages must be full or nearly full before page level compression is

attempted Two techniques

Prefix compression Dictionary compression

Page 10: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Data Compression Types: Page

Prefix Compression Per column, identify a "common" prefix Row of all prefix values for a column is stored in CI structure of

page Repeated values in column replaced by reference to prefix in CI

Example from SQL Server 2014 Books Online: https://msdn.microsoft.com/en-us/library/cc280464%28v=sql.120%29.aspx

Prefix compression

applied

Page 11: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Data Compression Types: Page

Dictionary Compression Follows prefix compression Finds repeated values anywhere on the page Repeated values replaced by reference to value in CI

Example from SQL Server 2014 Books Online: https://msdn.microsoft.com/en-us/library/cc280464%28v=sql.120%29.aspx

Dictionary compression

applied

Page 12: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

Data Compression SyntaxCompress a Table, Partition, or Index

ALTER TABLE T1 REBUILD with (Data_Compression = Row | Page | None)

ALTER TABLE PT1 REBUILD PARTITION = 1 with (Data_Compression = Row | Page | None)

ALTER INDEX IDX1 ON T1 REBUILD with (Data_Compression = Row | Page | None)

Page 13: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

Demo

Examining data compression's effecton data pages and data types

Page 14: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Deciding What to CompressQuestions to Ask

Why?What?

Impact?

Page 15: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Evaluate Potential TargetsCompute Resources, SQL Server Instance, and Databases

SQL Server Edition Database portability Need for space savings

Database Files on Disk Backup Files

Available CPU headroom Maintenance window and available resources for initial compression

and index rebuilds

Page 16: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Evaluate Potential TargetsData Suitability

Is data likely to achieve high compression ratios? Compressible data types Empty space in columns NULLS Repeated values

EXEC sp_estimate_data_compression_savings @Schema_Name = 'dbo', @Object_Name = 'Contact', @Index_ID = 1, @Partition_Number = NULL, @Data_Compression = 'ROW';

Page 17: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

Demo

Estimating compression ratios

Page 18: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Evaluate Row vs. Page CompressionWorkload Suitability

Use row compression at a minimum when: Compression results in (significant) space savings, and System can accommodate < 10% increase in CPU utilization

Consider page compression when Low % of update operations vs. total operations High % of scans vs. total operations Total operations = scans + DMLs + lookups

Page 19: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Evaluate Workload

SELECT

ObjectName = object_schema_name(idx.object_id) + '.' + object_name(idx.object_id)

,IndexName = idx.name

,IndexType = CASE WHEN is_unique = 1 THEN 'Unique ' ELSE '' END + idx.type_desc

,User_Scans = us.user_scans

,User_Lookups = us.user_lookups

,User_Updates = us.user_updates

FROM sys.indexes idx

LEFT JOIN sys.dm_db_index_usage_stats us ON idx.object_id = us.object_id

AND idx.index_id = us.index_id

AND us.database_id = db_id()

WHERE OBJECT_NAME (us.[object_id]) = 'customer'

ORDER BY us.user_scans + us.user_updates DESC

Page 20: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Decision Matrix

Table.Index

Savings

ROW %

Savings PAGE

%S U

Decision

Notes

customer. customer_pk

12% 26% 77.7%

22.2%

PAGE Large, High S, Low UBig Savings

customer. ix_customer_nationkey

29% 34% 34.1%

65.9%

ROW Large, Low S, High USimilar Savings

customer. ix_customer_mktsegment_name

16% 66% 23.3%

76.7%

ROW Medium, Low S, High USimilar Savings

customer. ix_customer_name_phone_acctbal

16% 42% 85% 15% PAGE Medium, High S, Low U

Page 21: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

Demo

Evaluating workload suitability

Page 22: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Resource Requirements

Compression requires workspace, CPU, and I/O The mechanism is the same as rebuilding an index

Resource requirements depend on What is being compressed (heap, CI, NCI) Sort_In_Tempdb on or off Online option on or off Recovery Model

Page 23: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Resource RequirementsResources Affected During Compression

Workspace Free workspace needed in user db, transaction log, tempdb

I/O Proportional to workspace used

CPU Row compression uses about 1.5 times the CPU used for an

index rebuild Page compression 2 – 5 times the CPU used for an index

rebuild

Page 24: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Compression Operations Options

Online vs. offline Offline is faster, requires less resources, but locks table for

duration Sequential vs. concurrent

Concurrent requires greater resources (workspace, I/O, CPU) Order of operations

Smallest to largest so more free disk space is available for larger objects

Sort_In_Tempdb On/Off On is recommended, as requires less workspace in user

databases

Page 25: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Compression Side Effects

Compression rebuilds a partition or index so fragmentation is removed Compressing a heap rebuilds any nonclustered indexes

When ONLINE is OFF, indexes are rebuilt sequentially When ONLINE is ON, indexes are rebuilt simultaneously Space for an uncompressed heap is not released until index

rebuild is complete Compression frees space in the data file

Managing free space considerations apply as normal

Page 26: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Other Impacts

Workload performance Index rebuilds are slower, require more CPU Bulk inserts are slower Partition manipulation TDE not affected

Page 27: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

References

SQL Server Books Onlinehttps://msdn.microsoft.com/en-US/library/cc280449(v=sql.120).aspx

White Paper: Data Compression: Strategy, Capacity Planning and Best Practiceshttp://technet.microsoft.com/en-us/library/dd894051(v=sql.100).aspx

A Look Inside SQL Server Row and Page Compression – Jes Borlandhttp://blogs.lessthandot.com/index.php/datamgmt/dbprogramming/how-sql-server-data-compression/

Applying Compression to the SQL Sentry Database – Melissa Connorshttp://blogs.sqlsentry.com/melissaconnors/sql-sentry-data-compression-1/

Page 28: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

References

Data Compression and Backup Compression – Chad Boydhttps://www.mssqltips.com/sqlservertip/2253/sql-server-2008-data-compression-and-backup-compression/

Estimating Data Compression Ratios for All Partitionshttps://www.mssqltips.com/sqlservertip/2219/estimating-data-compression-ratios-for-all/

Estimating Data Compression Savings in SQL Server 2012 – Glenn Berryhttp://www.sqlskills.com/blogs/glenn/estimating-data-compression-savings-in-sql-server-2012/

Page 29: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Review

Why compress data in SQL Server? Data compression basics Deciding what to compress Planning your compression efforts Resource requirements, options, and side effects Summary

Page 30: SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall jrandall@sqlsentry.com Tuesday,

Don’t forget to complete an online evaluation on EventBoard!

Your evaluation helps organizers build better conferences and helps speakers improve their sessions.

Questions?

Thank you!

Putting the "Squeeze" on Large TablesImprove Performance and Save Space with Data Compression