SQL Server Data Compression: ROW vs PAGE vs COLUMNSTORE
For SQL Server users who know CREATE TABLE and indexes, and want a large table to take less disk space.
On this page
Data compression stores the rows of a SQL Server table or index in fewer bytes. For example, you can shrink an order history table that has taken most of a drive.
You choose between three kinds: ROW, PAGE and columnstore. The table then fills fewer pages, and queries read fewer pages from disk. In exchange, the server spends extra CPU to compress and decompress the rows.
One statement sets the compression of an existing table:
ALTER TABLE table_name
REBUILD PARTITION = { ALL | partition_number }
WITH (DATA_COMPRESSION = { NONE | ROW | PAGE | COLUMNSTORE | COLUMNSTORE_ARCHIVE });
ALTER TABLE ... REBUILDrewrites every page of the table with the new setting. For an index, the statement isALTER INDEX index_name ON table_name REBUILD.PARTITION = ALLrebuilds the whole table. A number rebuilds one partition only. A table that isn't partitioned has one partition, number 1.NONEstores the rows uncompressed.ROWstores each value in only the bytes it needs.PAGEstores each value likeROW, then stores the values that repeat on a page only once.COLUMNSTOREandCOLUMNSTORE_ARCHIVEapply only to a columnstore table, which stores its data column by column.COLUMNSTORE_ARCHIVEcompresses the columnstore data a second time.
Data compression is in every edition of SQL Server 2022, Express included. The SQL Server 2016 feature list dates that to SQL Server 2016 SP1. The statements below follow the Microsoft Learn reference for SQL Server 2022.
Create the sample table, a small order history stored as a heap, a table without a clustered index:
CREATE TABLE dbo.OrderHistory (
OrderID int NOT NULL,
OrderDate date NOT NULL,
Status char(10) NOT NULL,
Amount decimal(9,2) NOT NULL
);
INSERT INTO dbo.OrderHistory (OrderID, OrderDate, Status, Amount) VALUES
(1, '2024-01-05', 'shipped', 120.00),
(2, '2024-01-05', 'shipped', 0.00),
(3, '2024-01-06', 'cancelled', 45.50),
(4, '2024-01-06', 'shipped', 980.00);
Three of the four orders have the status shipped, and order 2 has an amount of 0.00. Both details matter to compression.
sp_estimate_data_compression_savings
sp_estimate_data_compression_savings estimates the size of a table or index under another compression setting. For example, you can estimate the order history's size with PAGE compression before you rebuild it.
EXEC sys.sp_estimate_data_compression_savings
@schema_name = 'dbo',
@object_name = 'OrderHistory',
@index_id = NULL,
@partition_number = NULL,
@data_compression = 'PAGE';
The procedure returns one row for each index and partition. The order history has one: the heap, index_id 0, partition 1. Two of its columns answer the question:
size_with_current_compression_setting (KB)is the table as it is now.size_with_requested_compression_setting (KB)is the estimated size withPAGEcompression.
The estimate comes from a sample of the rows, which SQL Server copies into tempdb, the system database for temporary objects, and compresses there. Run the procedure once with 'ROW' and once with 'PAGE' to compare the two settings.
If the estimate comes back larger than the current size, leave the table uncompressed. The rows then use nearly the full precision of their data types, and the compressed format costs more bytes than it saves, as the procedure's documentation explains.
ROW compression
ROW compression stores each value in only the bytes it needs. For example, you can store the small order numbers of the order history in one byte each.
ALTER TABLE dbo.OrderHistory
REBUILD PARTITION = ALL
WITH (DATA_COMPRESSION = ROW);
The statement returns no rows, and a SELECT still returns the same four orders. Only their storage changed:
OrderIDvalues 1 to 4 take 1 byte each instead of 4.- The status shipped takes 7 bytes instead of 10, because the trailing blanks of the
char(10)value are dropped. - The amount 0.00 of order 2 takes no bytes, as a zero or a NULL does in any data type.
OrderDatekeeps its 3 bytes. Row compression saves nothing on adate.
Row compression leaves varchar, nvarchar, varbinary, uniqueidentifier and xml values as they are. The row compression page lists what it does for each data type.
PAGE compression
PAGE compression applies row compression, then stores the values that repeat on a page only once. For example, you can store the status shipped once for a whole page of orders.
ALTER TABLE dbo.OrderHistory
REBUILD PARTITION = ALL
WITH (DATA_COMPRESSION = PAGE);
SQL Server first row-compresses the four orders. Prefix compression then looks for a beginning that the values of one column share, and stores it once.
Dictionary compression then stores each value that repeats anywhere on the page once. The status shipped of orders 1, 2 and 4 is stored once, and each of the three rows keeps a reference to it.
SQL Server keeps a page compressed this way only where the space saved, minus the space of the page's prefixes and dictionary, is significant.
On an index, only the bottom level of pages, the leaf level, gets page compression. The levels above the leaf level get row compression, as the page compression page explains.
sys.partitions
sys.partitions is a catalog view with one row for each partition of each table and index. Its data_compression_desc column shows the compression setting of each one. For example, you can check the setting of the order history after the rebuild.
SELECT index_id, partition_number, data_compression_desc
FROM sys.partitions
WHERE object_id = OBJECT_ID('dbo.OrderHistory');
The query returns one row: index_id 0, the heap, in partition 1, with PAGE. A nonclustered index would add a row of its own, with its own setting.
Clustered columnstore index
A clustered columnstore index stores a whole table column by column, and always compresses it. For example, you can turn the order history into a columnstore for reports that scan all of it.
CREATE CLUSTERED COLUMNSTORE INDEX cci_OrderHistory
ON dbo.OrderHistory;
The heap becomes a columnstore table: the clustered columnstore index now holds all of its rows. The sys.partitions query now returns index_id 1, the clustered index, with COLUMNSTORE.
A columnstore is made for large tables. SQL Server compresses its rows in groups of up to 1,048,576 rows, called rowgroups. A bulk load of fewer than 102,400 rows goes to the deltastore first, which keeps the rows in ordinary row format until enough rows collect.
COLUMNSTORE_ARCHIVE
COLUMNSTORE_ARCHIVE compresses columnstore data a second time, with Microsoft's XPRESS algorithm. For example, you can archive the order history once reports no longer read it.
ALTER TABLE dbo.OrderHistory
REBUILD PARTITION = ALL
WITH (DATA_COMPRESSION = COLUMNSTORE_ARCHIVE);
SQL Server rebuilds the table with archival compression on top of columnstore compression, and sys.partitions shows COLUMNSTORE_ARCHIVE. Queries on the table now run slower than on a plain columnstore. To take the archival compression off, rebuild with DATA_COMPRESSION = COLUMNSTORE.
On a table partitioned by month, REBUILD PARTITION = 1 archives only the first partition, such as the oldest month. To split a table into partitions first, see SQL Server table partitioning.
ROW, PAGE or columnstore
| Setting | Stores | Costs | Use it for |
|---|---|---|---|
ROW | each value in the bytes it needs | CPU to compress and decompress | tables that take many updates |
PAGE | ROW, plus repeated values once per page | CPU, which can be higher than for ROW | tables that are mostly read and scanned |
COLUMNSTORE | the table column by column, compressed | the table becomes a columnstore index | large tables that reports scan |
COLUMNSTORE_ARCHIVE | columnstore data, compressed again | slower queries | partitions you rarely read |
Choose ROW for a table that changes often, and PAGE for a table that is mostly read, such as an order history. Microsoft's compression whitepaper for SQL Server 2008 gives the rule: the fewer updates and the more scans a table has, the better it suits page compression. It puts the CPU cost of row compression at usually 10 percent or less.
Choose a columnstore when queries scan most of a large table, rather than look up single rows.
What goes wrong
New rows in a page-compressed heap
Ordinary inserts into a heap with PAGE compression land on new pages without page compression. A heap gets page compression only in these cases:
- a bulk import with bulk optimizations enabled
- an
INSERT INTO ... WITH (TABLOCK)into a heap with no nonclustered index - an
ALTER TABLE ... REBUILDwithPAGEcompression
Rebuild the heap again from time to time, or give the table a clustered index, which doesn't have this gap. A clustered index created on the heap takes over the heap's compression setting.
Indexes keep their own setting
A nonclustered index doesn't inherit the compression of its table: a new index starts with NONE. To compress an index, add WITH (DATA_COMPRESSION = PAGE) to its CREATE INDEX, or rebuild it with ALTER INDEX ... REBUILD WITH (DATA_COMPRESSION = PAGE).
A rebuild locks the table
A rebuild needs as much free disk space as rebuilding an index of the same size. On a partitioned table, rebuild one partition at a time to need less space.
Outside Enterprise Edition, schedule the rebuild for a quiet hour. Without ONLINE = ON, the rebuild locks the table, and no query can read the table until the rebuild ends. ONLINE = ON needs Enterprise Edition.
Tables and values that stay uncompressed
The data compression page lists these limits:
- System tables can't be compressed.
- A table with sparse columns can't be compressed, and a compressed table can't get a sparse column.
- Compression adds an overhead to each row. A table can't be compressed when its largest possible row plus that overhead exceeds 8,060 bytes.
- Values stored off the row, such as a large
xmlorvarchar(max)value, stay uncompressed.
Execution plans change
After you compress a table, check the execution plans of its slowest queries, and not only its new size. Compression can change those plans, because the rows fill a different number of pages.
Run the estimate in DbSchema
The SQL Editor in DbSchema runs T-SQL on a connected SQL Server. Paste the sp_estimate_data_compression_savings call and press Run: the estimate shows in the result grid. An ALTER TABLE ... REBUILD that you run there changes the live database.
Write down why a table is compressed in the Comment box of the Edit Table dialog. When DbSchema is connected, it writes the comment to SQL Server as the table's MS_Description extended property. The HTML5 documentation shows the comment beside the table.
Download DbSchema at https://dbschema.com/download.html, connect it to your SQL Server database, and run the estimate on your largest table. Connecting and the SQL Editor are in the free Community Edition. The HTML5 documentation is in Pro.

