Lesson 3 of 3 Tables, Data Types and Indexes

Columnstore Indexes for Analytical Queries

This lesson explains columnar storage, rowgroups and batch mode, and when to use clustered or nonclustered columnstore indexes.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how rowgroups, compression, segment elimination and batch mode speed up scans.
  • Distinguish between clustered and nonclustered columnstore indexes.
  • Choose a columnstore design for analytical and real-time operational analytics workloads.

How columnstore storage works

A columnstore index divides rows into rowgroups of up to 1,048,576 rows and stores each column of a rowgroup as a compressed segment. Because values in one column are similar, compression often reaches ten times or more, and a query reads only the columns it needs. Each segment records its minimum and maximum values, so predicates can skip whole rowgroups (segment elimination). Queries run in batch mode, processing around 900 rows at a time per operator, which greatly reduces CPU per row.

Small inserts first go to a rowstore delta store, which a background tuple mover compresses once it reaches the rowgroup size; bulk loads of at least 102,400 rows compress directly. Deletes are marked in a delete bitmap and updates are a delete plus an insert, so heavily updated columnstore tables benefit from periodic ALTER INDEX ... REORGANIZE. An ordered clustered columnstore index (ORDER (col)) sorts data during the build to improve segment elimination on that column.

The following diagram shows rowgroups, column segments and the delta store.

How columnstore storage works. Rows are divided into rowgroups of up to 1,048,576 rows, and each column of a rowgroup is stored as a compressed segment, so a query reads only the columns it needs. Each segment records its minimum and maximum, so predicates skip whole rowgroups (segment elimination). Small inserts go to a rowstore delta store, which the tuple mover compresses later; bulk loads of at least 102,400 rows compress directly.
Reading fewer columns and skipping rowgroups is what makes columnstore fast.
Match the pairs

Columnstore terms

Exam skills 1.1.1

Match each columnstore term with its meaning.

Log in to try this activity Interactive activities are free with an account.

Clustered and nonclustered columnstore

The two forms serve different workloads, as the following comparison shows.

Clustered columnstore
What it isThe table itself is stored in columnar format
Typical workloadData warehouse fact tables, large history tables
Other indexesNonclustered B-tree indexes can be added for seeks
Write costLow for bulk loads, higher for singleton updates
Choose when Mostly scans and aggregations over millions of rows
Nonclustered columnstore
What it isA columnar copy of selected columns on a rowstore table
Typical workloadOLTP tables that also need real-time analytics
Other indexesThe table keeps its clustered rowstore index
Write costEvery write also maintains the columnstore
Choose when Point operations plus analytical queries on the same table

Columnstore indexes are available in SQL Server, Azure SQL Managed Instance, SQL database in Fabric and every vCore tier of Azure SQL Database. They are not a good choice for small tables or for workloads dominated by single-row lookups and updates, where a B-tree is faster.

Sort into groups

Which index type?

Exam skills 1.1.1

Choose the index type that fits each table.

Log in to try this activity Interactive activities are free with an account.

Creating and checking columnstore indexes

A columnstore index is created with CREATE CLUSTERED COLUMNSTORE INDEX, which converts the whole table, or CREATE NONCLUSTERED COLUMNSTORE INDEX with a list of columns, which adds a columnar copy beside the rowstore table. After loading, the sys.dm_db_column_store_row_group_physical_stats view shows the state and size of each rowgroup, which reveals whether data is still waiting in delta stores. The following statements show both forms and the check.

-- Convert a large history table to columnar storage
CREATE CLUSTERED COLUMNSTORE INDEX CCI_SalesHistory
    ON Sales.SalesHistory
    ORDER (SaleDate);

-- Add real-time analytics to an OLTP table
CREATE NONCLUSTERED COLUMNSTORE INDEX NCCI_OrderLines
    ON Sales.OrderLines (ProductID, Quantity, UnitPrice, OrderDate);

-- Inspect rowgroup state and size
SELECT object_name(object_id) AS table_name, state_desc, total_rows, deleted_rows
FROM sys.dm_db_column_store_row_group_physical_stats;

Rowgroups in the OPEN or CLOSED state are still delta stores; COMPRESSED rowgroups are columnar. Many small compressed rowgroups or a high deleted_rows count indicate that the index should be reorganized.

True or false

Checking rowgroups

Exam skills 1.1.1

Decide whether each statement about columnstore rowgroup states is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

Real-time analytics on an orders table

An Azure SQL database stores 400 million order lines. The application inserts and updates single lines all day, and a new dashboard must aggregate revenue by product and month from the same table with minimal delay. Analysts cannot wait for a nightly ETL. Which index design should the developer use?

Keep the clustered rowstore index that serves the application's point operations and add a nonclustered columnstore index on the columns the dashboard aggregates. The dashboard queries then run in batch mode on compressed columns, while the application continues to use B-tree seeks. Converting the table to a clustered columnstore index would slow the frequent singleton updates, and an ETL into a separate warehouse would add the delay that the requirement rules out.

Check yourself

Where do small inserts into a columnstore index go first?

Show answer

Into a rowstore delta store, which the tuple mover compresses once it fills a rowgroup; bulk loads of at least 102,400 rows compress directly.

Exam Tips

Remember for the exam

  • Large fact or history table, mostly scans: clustered columnstore.
  • Analytics on a busy OLTP table: nonclustered columnstore.
  • Rowgroups hold up to 1,048,576 rows; bulk loads of 102,400 rows or more compress directly.
  • Batch mode and segment elimination are the main sources of columnstore speed.