Published on

October 21, 2011

Understanding Columnstore Indexes in SQL Server

Columnstore indexes are a powerful feature in SQL Server that can significantly improve the performance of queries on large tables. However, there are certain limitations and considerations when it comes to updating tables with a Columnstore index.

When a Columnstore index is created on a table, the table becomes read-only, meaning that no insert, update, or delete operations can be performed on the table. This is because Columnstore indexes are designed to optimize data retrieval for large tables with millions or billions of rows.

If you need to update a table with a Columnstore index, there are several ways to do it. One of the easiest ways is to disable the index, perform the update, and then enable the index again. Here’s an example:

USE AdventureWorks;

-- Create a new table
CREATE TABLE [dbo].[MySalesOrderDetail] (
    [SalesOrderID] [int] NOT NULL,
    [SalesOrderDetailID] [int] NOT NULL,
    [CarrierTrackingNumber] [nvarchar](25) NULL,
    [OrderQty] [smallint] NOT NULL,
    [ProductID] [int] NOT NULL,
    [SpecialOfferID] [int] NOT NULL,
    [UnitPrice] [money] NOT NULL,
    [UnitPriceDiscount] [money] NOT NULL,
    [LineTotal] [numeric](38, 6) NOT NULL,
    [rowguid] [uniqueidentifier] NOT NULL,
    [ModifiedDate] [datetime] NOT NULL
) ON [PRIMARY];

-- Create a clustered index
CREATE CLUSTERED INDEX [CL_MySalesOrderDetail] ON [dbo].[MySalesOrderDetail] ([SalesOrderDetailID]);

-- Insert sample data into the table
INSERT INTO [dbo].[MySalesOrderDetail]
SELECT S1.*
FROM Sales.SalesOrderDetail S1
GO 100;

-- Create a Columnstore index
CREATE NONCLUSTERED COLUMNSTORE INDEX [IX_MySalesOrderDetail_ColumnStore] ON [MySalesOrderDetail] (UnitPrice, OrderQty, ProductID);

-- Attempt to update the table
UPDATE [dbo].[MySalesOrderDetail]
SET OrderQty = OrderQty + 1
WHERE [SalesOrderID] = 43659;

When you run the update statement, you will receive an error message indicating that data cannot be updated in a table with a Columnstore index. To work around this, you can disable the Columnstore index, perform the update, and then rebuild the index:

-- Disable the Columnstore index
ALTER INDEX [IX_MySalesOrderDetail_ColumnStore] ON [dbo].[MySalesOrderDetail] DISABLE;

-- Perform the update
UPDATE [dbo].[MySalesOrderDetail]
SET OrderQty = OrderQty + 1
WHERE [SalesOrderID] = 43659;

-- Rebuild the Columnstore index
ALTER INDEX [IX_MySalesOrderDetail_ColumnStore] ON [dbo].[MySalesOrderDetail] REBUILD;

This time, the update will be successful without any errors. It’s important to note that disabling and enabling the Columnstore index can have an impact on query performance, so it’s recommended to perform these operations during maintenance windows or when the system is not under heavy load.

Once you have finished updating the table, you can clean up by dropping the Columnstore index and truncating or dropping the table:

-- Cleanup
DROP INDEX [IX_MySalesOrderDetail_ColumnStore] ON [dbo].[MySalesOrderDetail];
TRUNCATE TABLE dbo.MySalesOrderDetail;
DROP TABLE dbo.MySalesOrderDetail;

In the next post, we will explore how to use partitioning to update tables with a Columnstore index.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.