Columnstore indexes in SQL Server, explained

What a columnstore index is, how rowgroups and segments store data by column, why fact tables and reports love it, and how to load it well.

Short answer: a columnstore index stores a table by column instead of by row, in rowgroups of up to about a million rows, each column compressed on its own. A query that sums one column over millions of rows reads only that column, compressed many times over, and runs in batch mode. Use a clustered columnstore for large fact tables and reports; keep rowstore indexes for tables you look up or update row by row.

Analytics queries touch few columns and many rows; order entry touches all columns of few rows. The two storage layouts each suit one of those.

How is it stored?

  1. Rowgroupsup to 1,048,576 rows each
  2. Segmentsone per column per rowgroup, compressed on its own
  3. Delta storenew rows wait here in rowstore until a rowgroup is full
  4. Delete bitmapmarks deleted rows until the rowgroup is rebuilt
A clustered columnstore table

Each segment also keeps the minimum and maximum of its values, so a filter on a date range can skip whole rowgroups without opening them.

The querywhat does it touch?
Columnstoremany rows, few columns: sums and counts for reports
Rowstore (B-tree)few rows, all columns: look up an order, update a row
Which index for which work

How do you create and load one?

CREATE TABLE dbo.FactSales (
    DateKey     int NOT NULL,
    CustomerKey int NOT NULL,
    ProductKey  int NOT NULL,
    Quantity    int NOT NULL,
    Amount      decimal(18,2) NOT NULL,
    INDEX cci_FactSales CLUSTERED COLUMNSTORE
);

Load in large batches. A bulk insert of 102,400 rows or more goes straight into compressed rowgroups; smaller inserts land in the delta store and are compressed later by a background task. Thousands of single-row inserts keep rows in the delta store, uncompressed, and every read scans them row by row.

SELECT OBJECT_NAME(object_id) AS TableName, state_desc, total_rows, deleted_rows
FROM sys.dm_db_column_store_row_group_physical_stats
WHERE object_id = OBJECT_ID('dbo.FactSales');

What goes wrong?

  • Small rowgroups. Many rowgroups well below a million rows compress poorly. Rebuild or reorganise the index after many small loads.
  • Heavy updates and deletes. An update is a delete plus an insert; deleted rows stay in the bitmap until a rebuild. Columnstore suits tables that mostly grow.
  • Lookups by key. Finding one order by its ID scans segments. Add a nonclustered rowstore index for such queries, or keep the table rowstore.

Where can you check this?

Where do you start?

Star schema in Power BI explains the fact tables that suit this index, and DirectQuery models in Power BI storage modes are fastest on a source built this way.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.