Back to blog

// article

Columnstore indexes in SQL Server: when rows are not enough

Columnstore indexes can dramatically speed up analytical queries, but they are not a universal replacement for regular indexes. Let’s look at the difference between clustered and nonclustered columnstore indexes and when to use them.

6 min read#SQLServer#Columnstore#Analytics#Performanceupdated
Article contents9

Do you know the difference between a regular index and a columnstore index? A regular index is like an index at the back of a book. A columnstore index is more like a warehouse where someone stopped storing boxes by order and started grouping the same kinds of things together.

And suddenly, when you do not need to find one specific order, but calculate revenue for the last five years, that warehouse starts to make a lot of sense.

In the previous article, we gave SQL Server labels so it would not have to walk through the whole warehouse. Today we will look at a case where labels are not enough. Sometimes it is better to change how the data is physically stored.

Rowstore vs. columnstore

#

A regular SQL Server table is usually stored by rows. This is called rowstore.

Imagine a Sales table:

sql
SELECT
    CustomerId,
    OrderDate,
    TotalPrice,
    Currency,
    Status
FROM dbo.Sales;

In rowstore storage, values from one row are stored together. Something like this:

text
Order 1: CustomerId, OrderDate, TotalPrice, Currency, Status
Order 2: CustomerId, OrderDate, TotalPrice, Currency, Status
Order 3: CustomerId, OrderDate, TotalPrice, Currency, Status

That is great when you are looking for one specific order, customer detail, or a few rows through an index.

But analytical queries often look different:

sql
SELECT
    YEAR(OrderDate) AS [Year],
    SUM(TotalPrice) AS [Revenue]
FROM dbo.Sales
WHERE OrderDate >= '2023-01-01'
GROUP BY YEAR(OrderDate);

This query does not want one row. It wants to scan many rows, but only a few columns.

At that point, rowstore starts to feel like moving an entire wardrobe when you only need one drawer.

How columnstore thinks

#

A columnstore index stores data by columns.

Instead of this:

text
Order 1: 10, 2024-01-01, 1200, CZK, Paid
Order 2: 11, 2024-01-02,  350, CZK, Paid
Order 3: 10, 2024-01-03,  890, CZK, Cancelled
Order 4: 14, 2024-01-03,  125, CZK, Paid

you can imagine it like this:

text
CustomerId:  10, 11, 10, 14
OrderDate:   2024-01-01, 2024-01-02, 2024-01-03, 2024-01-03
TotalPrice:  1200, 350, 890, 125
Currency:    CZK, CZK, CZK, CZK
Status:      Paid, Paid, Cancelled, Paid

When SQL Server needs to calculate SUM(TotalPrice) by OrderDate, it does not have to read everything. It can focus mostly on the columns that the query actually needs.

And because similar values are stored together, columnstore data often compresses very well. Less data on disk, less data in memory, less work during reads.

That sounds good. And most of the time, it is good.

It is just not a magic patch for every slow query.

Clustered columnstore index

#

A clustered columnstore index means the table itself is stored as columnstore.

It is not just a helper index next to the table. It is the main storage format for the data.

sql
CREATE CLUSTERED COLUMNSTORE INDEX CCI_Sales
ON dbo.Sales;

This makes the most sense for large analytical tables. Typical examples are fact tables in a data warehouse, historical measurements, logs, sales data, or event data.

Use it when:

  • the table has many rows,
  • aggregations are common,
  • queries read many rows but only some columns,
  • data is mostly appended,
  • you do not constantly update individual rows.

Example:

sql
SELECT
    ProductId,
    SUM(Quantity) AS [TotalQuantity],
    SUM(TotalPrice) AS [Revenue]
FROM dbo.Sales
WHERE OrderDate >= '2024-01-01'
GROUP BY ProductId;

This is exactly the kind of query where columnstore can shine.

Not because SQL Server suddenly gets a turbo button. But because it does not have to pull from disk everything the query does not need.

Nonclustered columnstore index

#

A nonclustered columnstore index is a different story.

The table remains a regular rowstore table, but you add a columnstore index next to it for analytical queries.

sql
CREATE NONCLUSTERED COLUMNSTORE INDEX NCCI_Sales_Report
ON dbo.Sales
(
    CustomerId,
    OrderDate,
    ProductId,
    Quantity,
    TotalPrice
);

This is useful when the table still serves the application, but someone also runs reports against it.

The application may still do this:

sql
SELECT *
FROM dbo.Sales
WHERE Id = @id;

A regular B-tree index is still a good fit for that.

But reporting may do this:

sql
SELECT
    CustomerId,
    SUM(TotalPrice) AS [Revenue]
FROM dbo.Sales
WHERE OrderDate >= '2024-01-01'
GROUP BY CustomerId;

And that is where a nonclustered columnstore index can help.

It is like having a reporting copy of the most important shelves next to the operational warehouse, organized specifically for calculations.

Clustered vs. nonclustered columnstore

#

In simple terms:

Type What it means When it makes sense
Clustered columnstore The table is stored as columnstore Large analytical tables
Nonclustered columnstore A columnstore index exists next to a rowstore table OLTP table with reporting queries

A clustered columnstore index is a bigger decision. You are changing how the table is stored.

A nonclustered columnstore index is a more careful option. You are adding an analytical layer next to an existing table.

That does not mean nonclustered is always safe and clustered is always risky. It only means you need to understand what the table is supposed to do.

When to use columnstore

#

Columnstore indexes are strong when queries:

  • read many rows,
  • use only some columns,
  • aggregate data,
  • filter by larger ranges,
  • support reporting, BI, or analytics.

Typical example:

sql
SELECT
    YEAR(OrderDate) AS [Year],
    MONTH(OrderDate) AS [Month],
    SUM(TotalPrice) AS [Revenue]
FROM dbo.Sales
GROUP BY
    YEAR(OrderDate),
    MONTH(OrderDate);

If this query runs over millions of rows, columnstore can be the difference between “I will go get coffee” and “done”.

When not to use columnstore

#

Columnstore is not a good fit everywhere.

It is usually not the best tool for:

  • small tables,
  • finding one row by ID,
  • very frequent small update/delete operations,
  • queries that need almost every column anyway,
  • OLTP workflows where the application constantly changes individual records.

For example:

sql
SELECT *
FROM dbo.Sales
WHERE Id = 123;

This is not a job for columnstore. This is a job for a well-designed regular index.

When you go to the warehouse for one specific invoice, you do not want to reorganize the whole archive by columns.

What to remember

#

A columnstore index is not a “better index”.

It is a different type of index for a different type of work.

Regular indexes are great when you need to find specific rows or small amounts of data.

Columnstore indexes are great when you need to read and calculate over a large amount of data.

Practical rule:

  • looking for a specific record → use a regular rowstore index,
  • calculating a report over millions of rows → consider columnstore,
  • the table is purely analytical → consider clustered columnstore,
  • the table is operational but has heavy reports → consider nonclustered columnstore.

Conclusion

#

Columnstore indexes are one of those SQL Server features that look quiet until you use them in the right place.

Then suddenly a query that used to feel like dragging data through a swamp finishes before you can prepare an excuse for the users.

But just like with regular indexes, the rule is the same: understand the query first, then add the index.

SQL Server is not a magician. Sometimes it just needs you to stop throwing all data into one big pile.