Columnstore Index: Differences between Columnstore Index vs BTree index

Published Mar 23 2019 02:39 PM 514 Views
Microsoft
First published on MSDN on Jul 17, 2016
In earlier blog why columnstore index , we had discussed what is a columnstore index and why do we need it. The columnstore storage model in SQL Server 2016 comes in two flavors; Clustered Columnstore Index (CCI) and Nonclustered Columnstore Index (NCCI) but these indexes are actually quite different than the traditional btree indexes. Here are the key differences

  • No key column(s): This may come as a surprise. Yes, though there are no key column(s) and yet these are considered as indexes. The reason for no key column(s) is that it would be very expensive to maintain row-order based on key column(s). Rowstore is organized as rows in pages with an auxiliary structure, row-offset table, to allow for an easy ordering of rows. On the other hand, the columnstore index organizes data as columns and compresses each column in one or more segments which would require to uncompress, insert the row, and then re-compress each segment. One of the alternative could be to maintain the row(s) outside of compressed segments but again it adds lot of complexity. Since there are no key-column(s), searching for qualifying rows, in the absence on nonclustered indexes, requires scanning of full columnstore index which can get expensive unless one or more rowgroups can be eliminated based on filter conditions. A common case for DW is that the data is inserted in date/time order and if filtering is done on data/time column, you can get significant rowgroup elimination which can speed up the query performance. For example, if you have 10 years of data in the FACT table and you want to run analytics query on say last quarter, SQL Server will only need to look at 1/40 (i.e. 2.5%) of the data.

  • Heap vs Clustered Columnstore index: One way to look at clustered columnstore index is like a ‘heap’ that is organized as columns. Like rowstore ‘heap’, there is no ordering of the rows. A nonclustered index leaf node will refer to a row in columnstore index as <rowgroup-id, row-number> which is similar to how a row is reference is RID<page-id, row-id> for rowstore heaps. When searching for a row through nonclustered index in (NCI/CCI) case, the leaf row of nonclustered index will point to a <rowgroup-id, row-number> which can then be retrieved by accessing the referenced rowgroup.

  • Only One Columnstore Index: Unlike rowstore btree indices, you can only create one columnstore index, either CCI or NCCI,  on a table.

  • Index Fragmentation: For rowstore based indexes, it is considered fragmented if (a) the physical order of pages in out of sync with the index-key order. (b) the data pages (clustered index) or index pages (for nonclustered index) are partially filled. A fragmented index will lead to significantly higher physical IOs and can potentially put more pressure on memory which can ultimately slowdown queries. Most organizations run a periodic index maintenance job to defragment indexes. For details, please refer to https://msdn.microsoft.com/en-us/library/ms189858.aspx#Fragmentation best practices on how to maintain btree indexes. For columnstore index, an index fragmentation is considered fragmented if (a) there are 10% or more rows marked as deleted in a compressed rowgroup (b) one or more smaller compressed rowgroups can be combined to create a larger compressed rowgroup such that the resultant compressed rowgroup has less than or equal to 1 million rows. Note, if a compressed rowgroup has less than 1 million rows due to dictionary size, it is not considered fragmented because there is nothing that can be done to increase its size.  Also recall that a columnstore index consists of zero or more delta rowgroups as shown the in the picture below.




The rows within delta rowgroup are organized as regular btree rowstore and they can get fragmented just like any other btree index but we don’t consider this as fragmentation because delta rowgroups are transitory and they eventually get compressed into compressed rowgroups. Please refer to https://blogs.msdn.microsoft.com/sqlserverstorageengine/2016/03/07/columnstore-index-defragmenta... on details how to defragment columnstore index, both NCCI and CCI.

Thanks

Sunil Agarwal
%3CLINGO-SUB%20id%3D%22lingo-sub-384763%22%20slang%3D%22en-US%22%3EColumnstore%20Index%3A%20Differences%20between%20Columnstore%20Index%20vs%20BTree%20index%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-384763%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Jul%2017%2C%202016%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20In%20earlier%20blog%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsqlserverstorageengine%2F2016%2F03%2F13%2Fcolumnstore-index-why-do-we-need-it%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20why%20columnstore%20index%20%3C%2FA%3E%20%2C%20we%20had%20discussed%20what%20is%20a%20columnstore%20index%20and%20why%20do%20we%20need%20it.%20The%20columnstore%20storage%20model%20in%20SQL%20Server%202016%20comes%20in%20two%20flavors%3B%20Clustered%20Columnstore%20Index%20(CCI)%20and%20Nonclustered%20Columnstore%20Index%20(NCCI)%20but%20these%20indexes%20are%20actually%20quite%20different%20than%20the%20traditional%20btree%20indexes.%20Here%20are%20the%20key%20differences%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3ENo%20key%20column(s)%3A%20%3C%2FSTRONG%3E%20This%20may%20come%20as%20a%20surprise.%20Yes%2C%20though%20there%20are%20no%20key%20column(s)%20and%20yet%20these%20are%20considered%20as%20indexes.%20The%20reason%20for%20no%20key%20column(s)%20is%20that%20it%20would%20be%20very%20expensive%20to%20maintain%20row-order%20based%20on%20key%20column(s).%20Rowstore%20is%20organized%20as%20rows%20in%20pages%20with%20an%20auxiliary%20structure%2C%20row-offset%20table%2C%20to%20allow%20for%20an%20easy%20ordering%20of%20rows.%20On%20the%20other%20hand%2C%20the%20columnstore%20index%20organizes%20data%20as%20columns%20and%20compresses%20each%20column%20in%20one%20or%20more%20segments%20which%20would%20require%26nbsp%3Bto%20uncompress%2C%20insert%20the%20row%2C%20and%20then%20re-compress%20each%20segment.%20One%20of%20the%26nbsp%3Balternative%20could%20be%20to%26nbsp%3Bmaintain%20the%20row(s)%20outside%20of%20compressed%20segments%20but%20again%20it%20adds%20lot%20of%20complexity.%20Since%20there%20are%20no%20key-column(s)%2C%20searching%20for%20qualifying%20rows%2C%20in%20the%20absence%20on%20nonclustered%20indexes%2C%20requires%20scanning%20of%20full%20columnstore%20index%20which%20can%20get%20expensive%20unless%20one%20or%20more%20rowgroups%20can%20be%20eliminated%20based%20on%20filter%20conditions.%20A%20common%20case%20for%20DW%20is%20that%20the%20data%20is%20inserted%20in%20date%2Ftime%20order%20and%20if%20filtering%20is%20done%20on%20data%2Ftime%20column%2C%20you%20can%20get%20significant%20rowgroup%20elimination%20which%20can%20speed%20up%20the%20query%20performance.%20For%20example%2C%20if%20you%20have%2010%20years%20of%20data%20in%20the%20FACT%20table%20and%20you%20want%20to%20run%20analytics%20query%20on%20say%20last%20quarter%2C%20SQL%20Server%20will%20only%20need%20to%20look%20at%201%2F40%20(i.e.%202.5%25)%20of%20the%20data.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3EHeap%20vs%20Clustered%20Columnstore%20index%3A%20%3C%2FSTRONG%3E%20One%20way%20to%20look%20at%20clustered%20columnstore%20index%20is%20like%20a%20%E2%80%98heap%E2%80%99%20that%20is%20organized%20as%20columns.%20Like%20rowstore%20%E2%80%98heap%E2%80%99%2C%20there%20is%20no%20ordering%20of%20the%20rows.%20A%20nonclustered%20index%20leaf%20node%20will%20refer%20to%20a%20row%20in%20columnstore%20index%20as%20%3CROWGROUP-ID%3E%20which%20is%20similar%20to%20how%20a%20row%20is%20reference%20is%20RID%3CPAGE-ID%3E%20for%20rowstore%20heaps.%20When%20searching%20for%20a%20row%20through%20nonclustered%20index%20in%20(NCI%2FCCI)%20case%2C%20the%20leaf%20row%20of%20nonclustered%20index%20will%20point%20to%20a%20%3CROWGROUP-ID%3E%20which%20can%20then%20be%20retrieved%20by%20accessing%20the%20referenced%20rowgroup.%3C%2FROWGROUP-ID%3E%3C%2FPAGE-ID%3E%3C%2FROWGROUP-ID%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3EOnly%20One%20Columnstore%20Index%3A%20%3C%2FSTRONG%3E%20Unlike%20rowstore%20btree%20indices%2C%20you%20can%20only%20create%20one%20columnstore%20index%2C%20either%20CCI%20or%20NCCI%2C%20%26nbsp%3Bon%26nbsp%3Ba%20table.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3EIndex%20Fragmentation%3A%20%3C%2FSTRONG%3E%20For%20rowstore%20based%20indexes%2C%20it%20is%20considered%20fragmented%20if%20(a)%20the%20physical%20order%20of%20pages%20in%20out%20of%20sync%20with%20the%20index-key%20order.%20(b)%20the%20data%20pages%20(clustered%20index)%20or%20index%20pages%20(for%20nonclustered%20index)%20are%20partially%20filled.%20A%20fragmented%20index%20will%20lead%20to%20significantly%20higher%20physical%20IOs%20and%20can%20potentially%20put%20more%20pressure%20on%20memory%20which%20can%20ultimately%20slowdown%20queries.%20Most%20organizations%20run%20a%20periodic%20index%20maintenance%20job%20to%20defragment%20indexes.%20For%20details%2C%20please%20refer%20to%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms189858.aspx%23Fragmentation%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms189858.aspx%23Fragmentation%20%3C%2FA%3E%20best%20practices%20on%20how%20to%20maintain%20btree%20indexes.%20For%20columnstore%20index%2C%20an%20index%20fragmentation%20is%20considered%20fragmented%20if%20(a)%20there%20are%2010%25%20or%20more%20rows%20marked%20as%20deleted%20in%20a%20compressed%20rowgroup%20(b)%20one%20or%20more%20smaller%20compressed%20rowgroups%20can%20be%20combined%20to%20create%20a%20larger%20compressed%20rowgroup%20such%20that%20the%20resultant%20compressed%20rowgroup%20has%20less%20than%20or%20equal%20to%201%20million%20rows.%20Note%2C%20if%20a%20compressed%20rowgroup%20has%20less%20than%201%20million%20rows%20due%20to%20dictionary%20size%2C%20it%20is%20not%20considered%20fragmented%20because%20there%20is%20nothing%20that%20can%20be%20done%20to%20increase%20its%20size.%20%26nbsp%3BAlso%20recall%20that%20a%20columnstore%20index%20consists%20of%20zero%20or%20more%20delta%20rowgroups%20as%20shown%20the%20in%20the%20picture%20below.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98039iA74F7C6596B58148%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20rows%20within%20delta%20rowgroup%20are%20organized%20as%20regular%20btree%20rowstore%20and%20they%20can%20get%20fragmented%20just%20like%20any%20other%20btree%20index%20but%20we%20don%E2%80%99t%20consider%20this%20as%20fragmentation%20because%20delta%20rowgroups%20are%20transitory%20and%20they%20eventually%20get%20compressed%20into%20compressed%20rowgroups.%20Please%20refer%20to%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsqlserverstorageengine%2F2016%2F03%2F07%2Fcolumnstore-index-defragmentation-using-reorganize-command%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsqlserverstorageengine%2F2016%2F03%2F07%2Fcolumnstore-index-defragmentation-using-reorganize-command%2F%20%3C%2FA%3E%20on%20details%20how%20to%20defragment%20columnstore%20index%2C%20both%20NCCI%20and%20CCI.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Thanks%20Sunil%20Agarwal%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-384763%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Jul%2017%2C%202016%20In%20earlier%20blog%20why%20columnstore%20index%2C%20we%20had%20discussed%20what%20is%20a%20columnstore%20index%20and%20why%20do%20we%20need%20it.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-384763%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerStorageEngine%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 02:39 PM
Updated by: