Uso de índices ColumnStore de SQL Server
Bueno, está bien, solo uno: un índice CS agrupado.
Si quieres leer sobre el hardware en el que hice esto, dirígete aquí . Revelación completa, escribí esa publicación de blog en el sitio web de la empresa para la que trabajo.
¡A la prueba!
Aquí hay un código genérico para construir una tabla bastante grande. La misma advertencia que Evan, esto puede tomar un tiempo para construir e indexar.
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
Bueno, Evan gana por simplicidad, pero ya he hablado de eso antes.
Aquí está la definición del índice. La y dee y dah.
CREATE CLUSTERED COLUMNSTORE INDEX CX_WOAHMAMA ON dbo.t1
Mirando un conteo, cada Id tiene una distribución bastante uniforme:
SELECT t.Id, COUNT(*) AS [Records]
FROM dbo.t1 AS t
GROUP BY t.Id
ORDER BY t.Id
Resultados:
Id Records
0 5005005
1 5005006
2 5005006
3 5005006
4 5005006
5 5005006
...
994 5005005
995 5005005
996 5005005
997 5005005
998 5005005
Con cada Id que tiene ~ 5,005,005 filas, podemos ver un rango bastante pequeño de ID para obtener una suma de 10 millones de filas.
SELECT COUNT(*) AS [Records], SUM(t.Amount) AS [Total]
FROM dbo.t1 AS t
WHERE t.Id > 0
AND t.Id < 3;
Resultado:
Records Total
10010012 50015062308
Perfil de consulta:
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.
Por diversión, una agregación más grande:
SELECT COUNT(*) AS [Records], SUM(CONVERT(BIGINT, t.Amount)) AS [Total]
FROM dbo.t1 AS t
WHERE t.Id > 0
AND t.Id < 101;
Resultados:
Records Total
500500505 2501989114575
Perfil de consulta:
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.
¡Espero que esto ayude!