Adding Lightness Better Performance through Compression SQL Saturday #474 - Salt Lake City Jay Robinson jrobinson@salesforce.com | @downshiftdata
Thanks to our Sponsors! Yearly Partners Gold Sponsors
Adding Lightness: A Thesis Data compression in SQL Server is not a strategy for consuming less disk space. It is a strategy for improving performance. This session will cover how it works, how to use it, when to use it, and how to take it into consideration when designing your data architecture.
The Obligatory Bio Page Jay Robinson Senior Systems Engineer, Salesforce Marketing Cloud (formerly ExactTarget) Supporting hundreds of instances of SQL Server across multiple data centers Email: jrobinson@salesforce.com Twitter: @downshiftdata Blog: downshiftdata.wordpress.com
“Simplify, then add lightness” Colin Chapman, Founder of Lotus With Jim Clark in the Lotus 49 after winning the 1967 Dutch Grand Prix at Zandvoort, the car’s first race.
1. Who Can Use It? Introduced in SQL Server 2008 Only available in Enterprise and Developer Editions (and 2008 R2’s Datacenter Edition) From MSDN: “The details of data compression are subject to change without notice in service packs or subsequent releases.”
2. What Will It NOT Do? Compress system tables Change the maximum row size on a page from 8060 bytes Compress a table when the overhead pushes the row size past 8060 Compress LOB pages?!?!
3. How Is It Added? For new tables and indexes... CREATE TABLE table_name (...) WITH (DATA_COMPRESSION = ROW|PAGE); CREATE INDEX index_name ON table_name (...) WITH (DATA_COMPRESSION = ROW|PAGE, ONLINE = ON); For existing tables and indexes... ALTER TABLE table_name REBUILD ALTER INDEX index_name ON table_name REBUILD
4. What Is Row Compression? Meta overhead is reduced (usually) Blanks removed from fixed strings Smaller data types are used Some types benefit, others do not. Beneficiaries: smallint, int, bigint, decimal, numeric, bit, smallmoney, money, float, real, datetime, datetime2, datetimeoffset, char, nchar, binary, timestamp
5. What Is Page Compression? LastName Smith Smithfield Smithwick &1 = “Smith” Page compression includes: Row compression Prefix compression Repeated string prefixes are replaced with a pointer to the prefix in the page header Dictionary compression Repeated strings are replaced with a pointer to the string in the page header Occurs when pages are full and a row is added Leaf level only LastName &1 &1field &1wick Address 123 Cherry St. 456 Cherry Ln. 78 Cherry Ave. 90 Cherry Rd. &2 = “ Cherry ” Address 123&2St. 456&2Ln. 78&2Ave. 90&2Rd.
6. How Are Indexes Compressed? The table (aka clustered index) and each non-clustered index can be compressed differently (none, row, or page). Reminder: leaf nodes of the non-clustered index contain all clustered index columns and included columns. Column Data Type Index SiteId int clustered, primary key SiteName varchar(100) Url nvarchar(256) non-clustered Url Url, SiteId
7. How Are Heaps Compressed? Heaps will only use PAGE compression under certain (odd) circumstances: Bulk operations ALTER TABLE ... REBUILD A compressed clustered index is applied then removed Non-clustered indexes will need to be rebuilt to reset the page pointers
8. What About Other Caveats? Indexed views can also be compressed Partitioned tables and indexes have each partition compressed separately Columnstore tables and indexes have their own compression Not configurable Can be further compressed with archival compression Not compatible with sparse columns
9. How Much Will You Gain? SQL Server 2014 + only EXEC dbo.sp_estimate_data_compression_savings @schema_name = N'Sales', @object_name = N'SalesOrderDetail', @index_id = 1, @partition_number = 1, @data_compression = 'PAGE';
sp_estimate_data_compression_savings DEMO sp_estimate_data_compression_savings
10. Why Does It Matter? Which wait types are more prevalent in your system? PAGEIOLATCH_SH and PAGEIOLATCH_EX SOS_SCHEDULER_YIELD Compression is a trade-off of CPU time for disk space. Most of our systems are IO-bound, not CPU-bound. We can afford this trade-off!
No Compression versus Row Compression Page Compression DEMO No Compression versus Row Compression Page Compression
Thank You! "Adding power makes you faster on the straights; subtracting weight makes you faster everywhere“ - Colin Chapman Don’t Forget The Survey! Jay Robinson Email: jrobinson@salesforce.com Twitter: @downshiftdata Blog: downshiftdata.wordpress.com