-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKeys_Demo.sql
More file actions
360 lines (303 loc) · 9.04 KB
/
Keys_Demo.sql
File metadata and controls
360 lines (303 loc) · 9.04 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
------------------------------
-- One Key to Rule Them All --
-- Demo Script ---------------
-- Ami Levin 2019 ------------
------------------------------
USE Master;
GO
-- Check Database Sizes Pre-Data Loading
EXEC Artificial_Keys..sp_spaceused @updateusage = 'TRUE', @oneresultset = 1;
EXEC Natural_Keys..sp_spaceused @updateusage = 'TRUE' , @oneresultset = 1;
GO
-- Load ~100K Rows of Data
-- Prepare staging table
DROP TABLE IF EXISTS #WebSites;
CREATE TABLE #WebSites (
URL VARCHAR(128) PRIMARY KEY,
Country VARCHAR(50),
City VARCHAR(50),
CountryCityID INT
);
GO
-- Load with non-linear proportional number of sites to population
WITH
TopCountryCities (Country, City, Population)
AS
(
SELECT TOP (50000) CC.Country, WCP.City, WCP.[Population]
FROM GeoData.dbo.countrycodes AS CC
INNER JOIN
GeoData.dbo.worldcitiespop AS WCP
ON CC.Code = WCP.Country
ORDER BY [Population] DESC
),
NTILED (Country, City, Tile)
AS
(
SELECT Country, City, NTILE(10) OVER(ORDER BY Population)
FROM TopCountryCities
)
INSERT INTO #WebSites
SELECT N'http://' + CAST(NEWID() AS VARCHAR(36)) + '.com' AS URL,
NTILED.Country,
NTILED.City,
CC.CountryCityID
FROM NTILED
INNER JOIN
(
VALUES (10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(10),
(9),(9),(9),(9),(9),(9),(9),(9),(9),(9),(9),(9),(9),(9),
(8),(8),(8),(8),(8),(8),(8),(8),(8),(8),
(7),(7),(7),(7),(7),(7),(7),
(6),(6),(6),(6),(6),(6),
(5),(5),(5),(5),(5),
(4),(4),(4),(4),
(3),(3),(3),
(2),(2),
(1)
) AS TileMultiply (Tile)
ON NTILED.Tile = TileMultiply.Tile
INNER JOIN
Artificial_Keys.dbo.Countries AS CO
ON CO.Country = NTILED.Country
INNER JOIN
Artificial_Keys.dbo.Cities AS CI
ON CI.City = NTILED.City
INNER JOIN
Artificial_Keys.dbo.CountryCities AS CC
ON CC.CountryID = CO.CountryID
AND
CC.CityID = CI.CityID;
GO
SELECT TOP 100 *
FROM #WebSites
GO
--BENCHMARK INSERT
-- Turn off SSMS execution plan if on...
-- Artificial Keys
DECLARE URL_Cursor CURSOR SCROLL STATIC
FOR
SELECT URL, Country, City, CountryCityID
FROM #WebSites
ORDER BY NEWID() -- RANDOM ORDER
SET NOCOUNT ON;
OPEN URL_Cursor;
DECLARE @URL VARCHAR(128), @Country VARCHAR(50), @City VARCHAR(50), @CountryCityID INT;
FETCH NEXT FROM URL_Cursor INTO @URL, @Country, @City, @CountryCityID;
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO Artificial_Keys.dbo.WebSites (
URL,
CountryCityID
)
VALUES (@URL, @CountryCityID);
FETCH NEXT FROM URL_Cursor INTO @URL, @Country, @City, @CountryCityID;
END
GO
--------------
DECLARE @URL VARCHAR(128), @Country VARCHAR(50), @City VARCHAR(50), @CountryCityID INT;
FETCH FIRST FROM URL_Cursor INTO @URL, @Country, @City, @CountryCityID;
WHILE @@FETCH_STATUS = 0
BEGIN
-- Natural Keys
INSERT INTO Natural_Keys.dbo.WebSites (
URL,
Country,
City
)
VALUES (@URL, @Country, @City);
FETCH NEXT FROM URL_Cursor INTO @URL, @Country, @City, @CountryCityID;
END
GO
CLOSE URL_Cursor;
DEALLOCATE URL_Cursor;
GO
-- Check Database Sizes Post-Data Loading
EXEC Artificial_Keys..sp_spaceused @updateusage = 'TRUE', @oneresultset = 1;
EXEC Natural_Keys..sp_spaceused @updateusage = 'TRUE' , @oneresultset = 1;
GO
-- Check Fragmanatation levels on clustered index
USE Artificial_Keys;
SELECT * FROM sys.dm_db_index_physical_stats
(DB_ID(N'Artificial_Keys'), OBJECT_ID(N'dbo.WebSites'), 1, NULL , NULL);
GO
USE Natural_Keys;
SELECT * FROM sys.dm_db_index_physical_stats
(DB_ID(N'Natural_Keys'), OBJECT_ID(N'dbo.WebSites'), 1, NULL , NULL);
GO
-- Check Fragmanatation levels on clustered index
USE Artificial_Keys;
SELECT * FROM sys.dm_db_index_physical_stats
(DB_ID(N'Artificial_Keys'), OBJECT_ID(N'dbo.WebSites'), NULL, NULL , NULL);
GO
-- Rebuild all large tables and shrink (don't do this at home...)
ALTER INDEX ALL ON Artificial_Keys..WebSites REBUILD;
ALTER INDEX ALL ON Natural_Keys..WebSites REBUILD;
ALTER INDEX ALL ON Artificial_Keys..CountryCities REBUILD;
ALTER INDEX ALL ON Natural_Keys..CountryCities REBUILD;
ALTER INDEX ALL ON Artificial_Keys..Cities REBUILD;
ALTER INDEX ALL ON Natural_Keys..Cities REBUILD;
DBCC SHRINKDATABASE (Artificial_Keys, 10);
DBCC SHRINKDATABASE (Natural_Keys, 10);
-- Check Database Sizes Post REBUILD and SHRINK
EXEC Artificial_Keys..sp_spaceused @updateusage = 'TRUE', @oneresultset = 1;
EXEC Natural_Keys..sp_spaceused @updateusage = 'TRUE' , @oneresultset = 1;
GO
------------
--QUERIES --
------------
USE master;
GO
--SET STATISTICS IO ON
--GO
-- Look for a particular URL (singleton lookup)
SELECT *
FROM Artificial_Keys.dbo.WebSites
WHERE URL = 'http://DoesntExist';
SELECT *
FROM Natural_Keys.dbo.WebSites
WHERE URL = 'http://DoesntExist';
GO
-- Look for particular Prefix (Range Lookup)
-- Narrow range
SELECT *
FROM Artificial_Keys.dbo.WebSites
WHERE URL LIKE 'http://AA%';
SELECT *
FROM Natural_Keys.dbo.WebSites
WHERE URL LIKE 'http://AA%';
GO
-- Wider range
SELECT *
FROM Artificial_Keys.dbo.WebSites
WHERE URL LIKE 'http://A%';
SELECT *
FROM Natural_Keys.dbo.WebSites
WHERE URL LIKE 'http://A%';
GO
-- Look for top 10 countries with largest number of web sites
SELECT C.Country, COUNT(*) AS NumberOfWebSites
FROM Artificial_Keys.dbo.WebSites AS WS
INNER JOIN
Artificial_Keys.dbo.CountryCities AS CC
ON WS.CountryCityID = CC.CountryCityID
INNER JOIN
Artificial_Keys.dbo.Countries AS C
ON C.CountryID = CC.CountryID
GROUP BY C.Country
ORDER BY COUNT(*) DESC
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
SELECT Country, COUNT(*) AS NumberOfWebSites
FROM Natural_Keys.dbo.WebSites
GROUP BY Country
ORDER BY COUNT(*) DESC
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
-- Bypass joins even when the key does not cover the query
-- Show Country ISO Codes for all countries that have websites
SELECT DISTINCT C.ISOCode
FROM Artificial_Keys.dbo.WebSites AS WS
INNER JOIN
Artificial_Keys.dbo.CountryCities AS CC
ON WS.CountryCityID = CC.CountryCityID
INNER JOIN
Artificial_Keys.dbo.Countries AS C
ON C.CountryID = CC.CountryID
ORDER BY C.ISOCode;
GO
SELECT DISTINCT C.ISOCode
FROM Natural_Keys.dbo.WebSites AS WS
INNER JOIN
Natural_Keys.dbo.Countries AS C
ON WS.Country = C.Country
ORDER BY C.ISOCode;
GO
-- Optimization attempt
CREATE NONCLUSTERED INDEX IDX1 ON Artificial_Keys..WebSites(CountryCityID);
CREATE NONCLUSTERED INDEX IDX1 ON Artificial_Keys..CountryCities(CountryID);
CREATE NONCLUSTERED INDEX IDX1 ON Natural_Keys..websites(Country);
--DROP INDEX IDX1 ON Artificial_Keys..WebSites;
--DROP INDEX IDX1 ON Artificial_Keys..CountryCities;
--DROP INDEX IDX1 ON Natural_Keys..websites;
-- Show all Tonga URLS
SELECT WS.URL
FROM Artificial_Keys..WebSites AS WS
INNER JOIN
Artificial_Keys..CountryCities AS CC
ON WS.CountryCityID = CC.CountryCityID
INNER JOIN
Artificial_Keys.dbo.Countries AS C
ON C.CountryID = CC.CountryID
WHERE C.Country = 'Tonga';
GO
SELECT URL
FROM Natural_Keys.dbo.WebSites
WHERE Country = 'Tonga';
-- Show all India URLs
SELECT WS.URL
FROM Artificial_Keys.dbo.WebSites AS WS
INNER JOIN
Artificial_Keys.dbo.CountryCities AS CC
ON WS.CountryCityID = CC.CountryCityID
INNER JOIN
Artificial_Keys.dbo.Countries AS C
ON C.CountryID = CC.CountryID
WHERE C.Country = 'India';
GO
SELECT URL
FROM Natural_Keys.dbo.WebSites
WHERE Country = 'India';
-- What cities are registered in the US?
SELECT DISTINCT CI.City
FROM Artificial_Keys.dbo.CountryCities AS CC
INNER JOIN
Artificial_Keys.dbo.Countries AS C
ON C.CountryID = CC.CountryID
INNER JOIN
Artificial_Keys.dbo.cities AS CI
ON CI.cityID = CC.CityID
WHERE C.Country = 'United States';
GO
SELECT DISTINCT city
FROM Natural_Keys.dbo.CountryCities
WHERE Country = 'United States';
GO
-- What web sites are in jerusalem, israel?
SELECT WS.[URL]
FROM Artificial_Keys.dbo.WebSites AS WS
INNER JOIN
Artificial_Keys.dbo.CountryCities AS CC
ON WS.CountryCityID = CC.CountryCityID
INNER JOIN
Artificial_Keys.dbo.Countries AS CO
ON CO.CountryID = CC.CountryID
INNER JOIN
Artificial_Keys.dbo.Cities AS CI
ON CI.CityID = CC.CityID
WHERE CO.Country = 'Israel'
AND
CI.City = 'Jerusalem';
SELECT [URL]
FROM Natural_Keys.dbo.WebSites
WHERE Country = 'Israel'
AND
City = 'Jerusalem';
GO
-- Optimization Attempt
CREATE NONCLUSTERED INDEX IDX2 ON Natural_Keys..WebSites(Country, City)
-- DROP INDEX IDX2 ON Natural_Keys..WebSites
-- And the down side?
-- Cote d'Ivoire has decided to change its name to english = Ivory Coast.
SET STATISTICS IO ON;
GO
UPDATE Artificial_Keys.dbo.Countries
SET Country = 'Ivory Coast'
WHERE Country = 'Cote d''Ivoire';
GO
UPDATE Natural_Keys.dbo.Countries
SET Country = 'Ivory coast'
WHERE Country = 'Cote d''Ivoire';
GO
SET STATISTICS IO OFF;
GO
-- Any ideas for additional queries?
-- EOF