分享

SQL Server Database Compression Notes

 ChenYimi 2012-07-24

SQL Server Data Compression

Kathi Kellenberger

kkellenberger@pragmaticworks.com

Database compression started from SQL 2008.

Agenda

1       Would compression cause a performance hit?

2.      How does it work?

We can store fixed size data as variable sized data in the row compression;

3.      What can I compress?

SQL 2008+, Developer or Enterprise, or Data Center

Tables, indexes, partitions

4.      What should I compress?

·        For read-only databases, we should compress with PAGE and then SHRINK and then defrag indexes.

·        For read-mostly databases, we should

Archive partitions with PAGE;

Medium age data with ROW;

Current data with NONE;

·        Record performance before and after

Two-level compressions

·        Row-level compression

We would not get a lot of benefit

·        Page-level compression

Syntax

CREATE NONCLUSTER INDEX [Index_ProductID] ON [dbo].SsalesOrderDetail_NC]

(

[ProductionID] ASC

)

GO

CREATE NONCLUSTER INDEX [Index_ProductID] ON [dbo].[SalesOrderDetail_C]

(

[ProductionID] ASC

) WITH (DATA_COMPRESSION = PAGE)

GO

ALTER TABLE dbo.SalesOrderDetail_C

REBUILD WITH (DATA_COMPRESSION = ROW)

GO

SELECT

OBJECT_NAME(sp.object_id) AS tableName,

si.name AS indexName, sp.partition_number, sp.rows, sp.data_compression_desc

FROM sys.partition sp

JOIN sys.indexes si ON si.object_id = sp.object_id AND si.index_id = sp.index_id

WHERE OBJECT(sp.object_id) IN (‘SalesOrderDetail_NC’, ‘SalesOrderDetail_C’)

SET STATISTICS IO ON

We can display IO information after executing a script.

In the demo 1, it was found that the performance is enhanced when having table scans and there is no difference when having index scan for the row compressed data.

Page compression takes less space than row compression.

SET STATISTICS TIME ON

How performance is affected when using UPDATE in compressed data?

The UPDATE command for a single row uses more CPU time and less elapsed time in the page compressed data.

DBCC IND(‘DBName’, ‘tblName’, partitionNumber)

partitionNumber (or indexed):

1: Data page

2: Index page

DBCC PAGE (DBName, field, pageID, viewType);

CREATE TABLE #RowEstimate ([object_name] SYSNAME, [schema_name] SYSNAME, index_id INT, partition_number INT, [size_with_current_compression_setting], [size_with_ROW(kb)] INT, [sample_size_with_current_compression_setting(kb)] INT, sample_size_with_ROW(kb)) INT)

CREATE TABLE #PageEstimate ([object_name] SYSNAME, [schema_name] SYSNAME, index_id INT, partition_number INT, [size_with_current_compression_setting], [size_with_PAGE(kb)] INT, [sample_size_with_current_compression_setting(kb)] INT, sample_size_with_PAGE(kb)) INT)

DECLARE @schema_name SYSNAME, @table_name SYSNAME

DECLARE tableList CURSOR FAST_FORWARD FOR

SELECT SCHEMA_NAME(schema_id), name FROM sys.tables WHERE SCHEMA_NAME(schema_id) = ‘Sales’

OPEN tableList

Fetch NEXT FROM tableList INTO @schema_name, @table_name

WHILE @@FETCH_STATUS =  0

BEGIN

INSERT INTO #RowEstimate EXEC sp_estimate_data_compression_savings

@schema_name = @schema_name, @object_name = @table_name, @index_id = NULL,

@partition_number = NULL, @data_compression = ‘PAGE;

 

INSERT INTO #PageEstimate EXEC sp_estimate_data_compression_savings

@schema_name = @schema_name, @object_name = @table_name, @index_id = NULL,

@partition_number = NULL, @data_compression = ‘PAGE’;

END

CLOSE tableList

DEALLOCATE tableList

 

Turn off compression

ALTER TABLE tableName REBUILD WITH (DATA_COMPRESSION = NONE);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多