-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcol6.sql
More file actions
144 lines (110 loc) · 4.35 KB
/
col6.sql
File metadata and controls
144 lines (110 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Fundamentals of Columnstore: Nonclustered Columnstore Advantages
v1.0 - 2020-10-17
https://www.BrentOzar.com/go/columnfund
This demo requires:
* SQL Server 2016 or newer
* Stack Overflow database 2018-06 version: https://www.BrentOzar.com/go/querystack
This first RAISERROR is just to make sure you don't accidentally hit F5 and
run the entire script. You don't need to run this:
*/
RAISERROR(N'Oops! No, don''t just hit F5. Run these demos one at a time.', 20, 1) WITH LOG;
GO
/* I'm using the large Stack database: */
USE StackOverflow;
ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 130;
GO
DropIndexes @TableName = 'Users';
GO
/* The Users table isn't a good fit for
clustered columnstore because:
* It's small
* We update it a lot
But what if we only use columnstore on the
columns that AREN'T updated, like:
* Id
* CreationDate
* DisplayName (rarely updated)
* Location (rarely updated)
That's where nonclustered columnstore indexes
come in: you can pick the columns to index.
Note that I'm creating this index on top of the
Users table, not Users_columnstore: */
CREATE NONCLUSTERED COLUMNSTORE INDEX NCCI
ON dbo.Users(Location, DisplayName, CreationDate, Id);
GO
/* And create a conventional rowstore one: */
CREATE INDEX Location_DisplayName_CreationDate_Id
ON dbo.Users(Location, DisplayName, CreationDate, Id);
GO
/* Compare their sizes: */
sp_BlitzIndex @TableName = 'Users'
/* Run an analytical-style query on Location: */
SET STATISTICS TIME, IO ON;
SELECT Location, COUNT(*) AS UsersCreated
FROM dbo.Users WITH (INDEX = NCCI)
GROUP BY Location
ORDER BY COUNT(*) DESC;
SELECT Location, COUNT(*) AS UsersCreated
FROM dbo.Users WITH (INDEX = Location_DisplayName_CreationDate_Id)
GROUP BY Location
ORDER BY COUNT(*) DESC;
/* But that's kinda close because our nonclustered
index started with Location: */
CREATE INDEX Location_DisplayName_CreationDate_Id
ON dbo.Users(Location, DisplayName, CreationDate, Id);
GO
/* What happens if we run analytics on a DIFFERENT
leading column? That's where columnstore shines:
where you can't predict what people are going to
filter or group by: */
SELECT YEAR(CreationDate) AS Yr, MONTH(CreationDate) AS Mo,
COUNT(*) AS UsersCreated
FROM dbo.Users WITH (INDEX = NCCI)
GROUP BY YEAR(CreationDate), MONTH(CreationDate)
ORDER BY YEAR(CreationDate), MONTH(CreationDate);
SELECT YEAR(CreationDate) AS Yr, MONTH(CreationDate) AS Mo,
COUNT(*) AS UsersCreated
FROM dbo.Users WITH (INDEX = Location_DisplayName_CreationDate_Id)
GROUP BY YEAR(CreationDate), MONTH(CreationDate)
ORDER BY YEAR(CreationDate), MONTH(CreationDate);
/* And then if someone does an update, and they're
not touching columns in the columnstore index,
it's fast: */
UPDATE dbo.Users
SET LastAccessDate = GETDATE()
WHERE Location = N'London, United Kingdom';
DELETE dbo.Users
WHERE Location = N'Paris, France';
/* And it doesn't affect the columnstore row groups:
no rows are deleted and no new delta stores are added. */
sp_BlitzIndex @TableName = 'Users'
/* What we learned in this module:
Nonclustered columnstore indexes:
* Are better for transactional (OLTP) tables
that have inserts, updates, and deletes
* Let you pick which columns you want to index
(but you don't have to worry about column order)
* Get you great compression ratios
* If you run analytical-style queries on those
rarely-updated columns, they can help
* But honestly, Users still isn't a good fit:
it's tiny, and we'd still have to deal with
index maintenance as we insert/delete rows.
*/
/*
License: Creative Commons Attribution-ShareAlike 4.0 Unported (CC BY-SA 4.0)
More info: https://creativecommons.org/licenses/by-sa/4.0/
You are free to:
* Share - copy and redistribute the material in any medium or format
* Adapt - remix, transform, and build upon the material for any purpose, even
commercially
Under the following terms:
* Attribution - You must give appropriate credit, provide a link to the license,
and indicate if changes were made. You may do so in any reasonable manner,
but not in any way that suggests the licensor endorses you or your use.
* ShareAlike - If you remix, transform, or build upon the material, you must
distribute your contributions under the same license as the original.
* No additional restrictions — You may not apply legal terms or technological
measures that legally restrict others from doing anything the license permits.
*/