Data Compression
Data Compression
For partitioned tables and indexes, the compression option can be configured for each
partition, and the various partitions of an object do not have to have the same compression
setting.
The compression setting of a table is not automatically applied to its nonclustered indexes. Each
index must be set individually. Compression is not available for system tables. Tables and indexes
1 2
can be compressed when they are created by using the CREATE TABLE and CREATE INDEX
3
statements. To change the compression state of a table, index, or partition, use the ALTER TABLE
4
or ALTER INDEX statements.
Note
If the existing data is fragmented, you might be able to reduce the size of the index by
rebuilding the index instead of using compression. The fill factor of an index will be applied
during an index rebuild, which could potentially increase the size of the index. For more
5
information, see Fill Factor .
Compression is available only in the SQL Server 2008 Enterprise and Developer editions.
Compression can allow more rows to be stored on a page, but does not change the
maximum row size of a table or index.
A table cannot be enabled for compression when the maximum row size plus the
compression overhead exceeds the maximum row size of 8060 bytes. For example, a table
that has the columns c1 char(8000) and c2 char(53) cannot be compressed because of the
additional compression overhead. When the vardecimal storage format is used, the row-
size check is performed when the format is enabled. For row and page compression, the
row-size check is performed when the object is initially compressed, and then checked as
each row is inserted or modified. Compression enforces the following two rules:
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 2 of 11
Disabling data compression must always succeed. Even if the compressed row fits on
the page, which means that it is less than 8060 bytes; SQL Server prevents updates
that would not fit on the row when it is uncompressed.
When a list of partitions is specified, the compression type can be set to ROW, PAGE, or
NONE on individual partitions. If the list of partitions is not specified, all partitions are set
with the data compression property that is specified in the statement. When a table or
index is created, data compression is set to NONE unless otherwise specified. When a table
is modified, the existing compression is preserved unless otherwise specified.
If you specify a list of partitions or a partition that is out of range, an error will be
generated.
Nonclustered indexes do not inherit the compression property of the table. To compress
indexes, you must explicitly set the compression property of the indexes. By default, the
compression setting for indexes will set to NONE when the index is created.
When a clustered index is created on a heap, the clustered index inherits the compression
state of the heap unless an alternative compression state is specified.
A table is rebuilt by executing the ALTER TABLE ... REBUILD statement with the PAGE
compression option.
New pages allocated in a heap as part of DML operations will not use PAGE compression
until the heap is rebuilt. Rebuild the heap by removing and reapplying compression, or by
creating and removing a clustered index.
Changing the compression setting of a heap requires all nonclustered indexes on the table
to be rebuilt so that they have pointers to the new row locations in the heap.
You can enable or disable ROW or PAGE compression online or offline. Enabling
compression on a heap is single threaded for an online operation.
The disk space requirements for enabling or disabling row or page compression are the
same as for creating or rebuilding an index. For partitioned data, you can reduce the space
that is required by enabling or disabling compression for one partition at a time.
When you are compressing indexes, leaf-level pages can be compressed with both row and
page compression. Non–leaf-level pages do not receive page compression.
Because of their size, large-value data types are sometimes stored separately from the
normal row data on special purpose pages. Data compression is not available for the data
that is stored separately.
Tables which implemented the vardecimal storage format in SQL Server 2005 will retain
that setting when upgraded. You can apply row compression to a table that has the
vardecimal storage format. However, because row compression is a superset of the
vardecimal storage format, there is no reason to retain the vardecimal storage format.
Decimal values gain no additional compression when you combine the vardecimal storage
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 3 of 11
format with row compression. You can apply page compression to a table that has the
vardecimal storage format; however, the vardecimal storage format columns probably will
not achieve additional compression.
Note
SQL Server 2008 supports the vardecimal storage format; however, because row-level
compression achieves the same goals, the vardecimal storage format is deprecated.
This feature will be removed in a future version of Microsoft SQL Server. Avoid using
this feature in new development work, and plan to modify applications that currently
use this feature.
Compression Implementation
For a summary of the implementation of data compression, see Row Compression
6 7
Implementation , Page Compression Implementation , and Unicode Compression
8
Implementation .
Splitting a range
When partitions are split by using the ALTER PARTITION statement, both partitions inherit
the data compression attribute of the original partition.
Merging a range
When two partitions are merged, the resultant partition inherits the data compression
attribute of the destination partition.
Switching partitions
To switch a partition, the data compression property of the partition must match the
compression property of the table.
There are two syntax variations that you can use to modify the compression of a
partitioned table or index:
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 4 of 11
The following syntax rebuilds the whole table by using the existing compression
setting for any partitions that are not referenced:
When a clustered index is dropped, the corresponding heap partitions retain their data
compression setting unless the partitioning scheme is modified. If the partitioning scheme
is changed, all partitions are rebuilt to an uncompressed state. To drop a clustered index
and change the partitioning scheme requires the following steps:
2. Modify the table by using the ALTER TABLE ... REBUILD ... option that specifies the
compression option.
To drop a clustered index OFFLINE is a very fast operation, because only the upper levels of
clustered indexes are removed. When a clustered index is dropped ONLINE, SQL
Server must rebuild the heap two times, once for step 1 and once for step 2.
When the Snapshot Agent generates the initial schema script, the new schema will use the
same compression settings for both the table and its indexes. Compression cannot be
enabled on just the table and not the index.
For transactional replication the article schema option determines what dependent objects
and properties have to be scripted. For more information, see sp_addarticle10.
The Distribution Agent does not check for down-level Subscribers when it applies scripts. If
the replication of compression is selected, creating the table on down-level Subscribers will
fail. In the case of a mixed topology, do not enable the replication of compression.
For merge replication, publication compatibility level overrides the schema options and
determines the schema objects that will be scripted. For more information about
11
compatibility level, see Using Multiple Versions of SQL Server in a Replication Topology .
In the case of a mixed topology, if it is not required to support the new compression
options, the publication compatibility level should be set to the down-level Subscriber
version. If it is required, compress tables on the Subscriber after they have been created.
The following table shows replication settings that control compression during replication.
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 5 of 11
Replicate
Replicate
partition
User intent compression Scripting behavior
scheme for a
settings
table or index
When data is exported, even in native format, the data is output in the uncompressed row
format. This can cause the size of exported data file to be significantly larger than the
source data.
When data is imported, if the target table has been enabled for compression, the data is
converted by the storage engine into compressed row format. This can cause increased CPU
usage compared to when data is imported into an uncompressed table.
When data is bulk imported into a heap with page compression, the bulk import operation
will try to compress the data with page compression when the data is inserted.
Enabling compression can cause query plans to change because the data is stored using a
different number of pages and number of rows per page.
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 6 of 11
Data compression is supported by SQL Server Management Studio through the Data
Compression Wizard.
In Object Explorer, right-click a table, index, or indexed view, point to Storage, and then
click Compress.
Monitoring Compression
To monitor compression of the whole instance of SQL Server, use the Page compression
attempts/sec and Pages compressed/sec counters of the SQL Server, Access Methods Object.
Examples
Some of the following examples use partitioned tables and require a database that has filegroups.
To create a database that has filegroups, execute the following statement.
USE TestDatabase
GO
The following example creates a table and sets the compression to ROW.
CREATE TABLE T1
(c1 int, c2 nvarchar(50) )
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 7 of 11
The following example create a table and sets the compression to PAGE.
CREATE TABLE T2
(c1 int, c2 nvarchar(50) )
WITH (DATA_COMPRESSION = PAGE);
GO
The following example uses the TestDatabase table that is created by using the code provided
earlier in this section. The example creates a partition function and scheme, and then creates a
partitioned table and specifies the compression options for the partitions of the table. In this
example, partition 1 is configured for ROW compression, and the remaining partitions are
configured for PAGE compression.
The following example uses the database that is used in example C. The example creates a table
by using the syntax for noncontiguous partitions.
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 8 of 11
ON myRangePS1 (col1)
WITH
(
DATA_COMPRESSION = ROW ON PARTITIONS (1,3),
DATA_COMPRESSION = NONE ON PARTITIONS (2,4)
);
GO
The following example changes the compression of the nonpartitioned table that is created in
example A.
ALTER TABLE T1
REBUILD WITH (DATA_COMPRESSION = PAGE);
GO
The following example changes the compression of the partitioned table that is created in
example C. The REBUILD PARTITION = 1 syntax causes only partition number 1 to be rebuilt.
The same operation that uses the following alternate syntax causes all partitions in the table to
be rebuilt.
The REBUILD PARTITION = ... syntax can rebuild only one partition. To rebuild more than one
partition, you must execute multiple statements, or execute the following example to rebuild all
partitions, using the current compression settings for unspecified partitions.
The following example uses the table that is created in example A and creates an index on the
column C2.
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 9 of 11
The following example creates an index on a partitioned table that uses row compression on all
partitions of the index.
To create the index so that is uses different compression settings for different partitions, use the
ON PARTITIONS syntax. The following example creates an index on a partitioned table that uses
row compression on partition 1 of the index and page compression on partitions 2 through 4 of
the index.
The REBUILD PARTITION = ... syntax can rebuild only one partition. To rebuild more than one
partition, you must execute multiple statements, or execute the following example to rebuild all
partitions, using the current compression settings for unspecified partitions.
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 10 of 11
) ;
GO
See Also
Reference
12
sys.partitions (Transact-SQL)
13
CREATE PARTITION SCHEME (Transact-SQL)
14
CREATE PARTITION FUNCTION (Transact-SQL)
1
CREATE TABLE (Transact-SQL)
ALTER TABLE (Transact-SQL)3
2
CREATE INDEX (Transact-SQL)
ALTER INDEX (Transact-SQL)4
Concepts
8
Unicode Compression Implementation
Links Table
1
http://msdn.microsoft.com/en-us/library/ms174979.aspx
2
http://msdn.microsoft.com/en-us/library/ms188783.aspx
3
http://msdn.microsoft.com/en-us/library/ms190273.aspx
4
http://msdn.microsoft.com/en-us/library/ms188388.aspx
5
http://msdn.microsoft.com/en-us/library/ms177459.aspx
6
http://msdn.microsoft.com/en-us/library/cc280576.aspx
7
http://msdn.microsoft.com/en-us/library/cc280464.aspx
8
http://msdn.microsoft.com/en-us/library/ee240835.aspx
9
http://msdn.microsoft.com/en-us/library/cc280574.aspx
10
http://msdn.microsoft.com/en-us/library/ms173857.aspx
11
http://msdn.microsoft.com/en-us/library/ms143241.aspx
12
http://msdn.microsoft.com/en-us/library/ms175012.aspx
13
http://msdn.microsoft.com/en-us/library/ms179854.aspx
14
http://msdn.microsoft.com/en-us/library/ms187802.aspx
Community Content
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011
Creating Compressed Tables and Indexes Page 11 of 11
http://msdn.microsoft.com/en-us/library/cc280449(d=printer).aspx 3/21/2011