都内で働くSEの技術的なひとりごと / Technical soliloquy of System Engineer working in Tokyo

都内でサラリーマンやってます。SQL Server を中心とした (2023年からは Azure も。) マイクロソフト系(たまに、OSS系などマイクロソフト以外の技術も...)の技術的なことについて書いています。日々の仕事の中で、気になったことを技術要素関係なく気まぐれに選んでいるので記事内容は開発言語、インフラ等ばらばらです。なお、当ブログで発信、発言は私個人のものであり、所属する組織、企業、団体等とは何のかかわりもございません。ブログの内容もきちんと検証して使用してください。英語の勉強のため、英語の

SQL Server 2014 CTP1 でカラムストアインデックスの検証もしてみた

 そういえば、2012 から追加されたカラムストアインデックスは、2014 になると更新も可能になるんですよね。ちょっと検証してみたいと思います。ここを参考にしました。AdventureWorksDW2012 の FactProductInventory テーブルを使用して検証してみたいと思います。カラムストアインデックスを作成には、下記のスクリプトを使用します。

USE [AdventureWorksDW2012]
CREATE NONCLUSTERED INDEX [IX_FactProductInventory] ON [dbo].[FactProductInventory]
([ProductKey] ASC
)

CREATE NONCLUSTERED COLUMNSTORE INDEX [IX_FactProductInventory]
ON [dbo].[FactProductInventory]
(ProductKey)
WITH (DROP_EXISTING =  ON, MAXDOP = 2)

 検証に使用した SQL は下記の通りです。

SELECT COUNT(ProductKey) FROM [dbo].[FactProductInventory]

 カラムストアインデックス適用前は下記の通りです。

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 2 ms.

(1 row(s) affected)
Table 'FactProductInventory'. Scan count 3, logical reads 6396, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row(s) affected)

 SQL Server Execution Times:
   CPU time = 250 ms,  elapsed time = 250 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

 カラムストアインデックス適用後は下記の通りです。

SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

(1 row(s) affected)
Table 'FactProductInventory'. Scan count 1, logical reads 9, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row(s) affected)

 SQL Server Execution Times:
   CPU time = 141 ms,  elapsed time = 343 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

 カラムストアインデックス適用後は、logical reads の回数が低下しており、I/O にも相当な影響があると思います。それぞれの 実行プラン内容を見ても明らかです。

※カラムストアインデックス適用前の実行プラン

f:id:koogucc11:20130915195511j:plain

f:id:koogucc11:20130915193136j:plain

※カラムストアインデックス適用後の実行プラン

f:id:koogucc11:20130915195557j:plain

f:id:koogucc11:20130915193405j:plain

 今回の検証で、ColumnStored Index Scan の I/O コストは、Clustered Index Scan の I/O コストと比較して非常に低いです。数千万、数億件のレコードを保持するテーブルから件数を取得するようなケースの場合は圧倒的な効果が得られるのでしょう。

 メモリ最適化テーブルと同様に、カラムストアインデックスも SQL Server 2014 の注目すべき技術の一つですね。