使用SQL Server ColumnStore索引
好吧,好吧,一个-集群的CS索引。
如果您想了解我从事此工作的硬件,请转到此处。完全披露后,我将该博客文章写在了我工作的公司的网站上。
进行测试!
这是一些用于构建很大表的通用代码。与Evan同样的警告,可能需要一段时间才能进行构建和编制索引。
USE tempdb
CREATE TABLE t1 (Id INT NOT NULL, Amount INT NOT NULL)
;WITH T (N)
AS ( SELECT X.N
FROM (
VALUES (NULL), (NULL), (NULL),
(NULL), (NULL), (NULL),
(NULL), (NULL), (NULL),
(NULL) ) AS X (N)
), NUMS (N) AS (
SELECT TOP ( 710000000 )
ROW_NUMBER() OVER ( ORDER BY ( SELECT NULL )) AS N
FROM T AS T1, T AS T2, T AS T3,
T AS T4, T AS T5, T AS T6,
T AS T7, T AS T8, T AS T9,
T AS T10 )
INSERT dbo.t1 WITH ( TABLOCK ) (
Id, Amount )
SELECT NUMS.N % 999 AS Id, NUMS.N % 9999 AS Amount
FROM NUMS;
--(705032704 row(s) affected) --Aw, close enough
那么,埃文赢得了简化,但我已经谈到了这之前。
这是索引定义。La,Dee和Dah。
CREATE CLUSTERED COLUMNSTORE INDEX CX_WOAHMAMA ON dbo.t1
看一下计数,每个Id都有相当均匀的分布:
SELECT t.Id, COUNT(*) AS [Records]
FROM dbo.t1 AS t
GROUP BY t.Id
ORDER BY t.Id
结果:
Id Records
0 5005005
1 5005006
2 5005006
3 5005006
4 5005006
5 5005006
...
994 5005005
995 5005005
996 5005005
997 5005005
998 5005005
每个ID都有大约5,005,005行,我们可以查看很小范围的ID,以获取1000万行的总和。
SELECT COUNT(*) AS [Records], SUM(t.Amount) AS [Total]
FROM dbo.t1 AS t
WHERE t.Id > 0
AND t.Id < 3;
结果:
Records Total
10010012 50015062308
查询资料:
Table 't1'. Scan count 6, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 2560758, lob physical reads 0, lob read-ahead reads 0.
Table 't1'. Segment reads 4773, segment skipped 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 564 ms, elapsed time = 106 ms.
有趣的是,更大的聚合:
SELECT COUNT(*) AS [Records], SUM(CONVERT(BIGINT, t.Amount)) AS [Total]
FROM dbo.t1 AS t
WHERE t.Id > 0
AND t.Id < 101;
结果:
Records Total
500500505 2501989114575
查询资料:
Table 't1'. Scan count 6, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 2560758, lob physical reads 0, lob read-ahead reads 0.
Table 't1'. Segment reads 4773, segment skipped 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 1859 ms, elapsed time = 321 ms.
希望这可以帮助!