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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
--disable_warnings
DROP TABLE IF EXISTS t1,t2,t3,t4;
DROP DATABASE IF EXISTS world;
--enable_warnings
set names utf8;
CREATE DATABASE world;
use world;
--source include/world_schema.inc
--disable_query_log
--disable_result_log
--disable_warnings
--source include/world.inc
--enable_warnings
--enable_result_log
--enable_query_log
SELECT COUNT(*) FROM Country;
SELECT COUNT(*) FROM City;
SELECT COUNT(*) FROM CountryLanguage;
CREATE INDEX Name ON City(Name);
--disable_query_log
--disable_result_log
--disable_warnings
ANALYZE TABLE City;
--enable_warnings
--enable_result_log
--enable_query_log
SET SESSION optimizer_switch='index_merge_sort_intersection=on';
SELECT COUNT(*) FROM City;
# The output of the next 6 queries tells us about selectivities
# of the conditions utilized in 4 queries following after them
SELECT COUNT(*) FROM City WHERE Name LIKE 'C%';
SELECT COUNT(*) FROM City WHERE Name LIKE 'M%';
SELECT COUNT(*) FROM City WHERE Population > 1000000;
SELECT COUNT(*) FROM City WHERE Population > 500000;
SELECT COUNT(*) FROM City WHERE Population > 300000;
SELECT COUNT(*) FROM City WHERE Population > 5000000;
# The pattern of the WHERE condition used in the following 4 queries is
# range(key1) AND range(key2)
# Varying values of the constants in the conjuncts of the condition
# we can get either an index intersection retrieval over key1 and key2
# or a range index scan for one of these indexes
EXPLAIN
SELECT * FROM City WHERE
Name LIKE 'C%' AND Population > 1000000;
EXPLAIN
SELECT * FROM City WHERE
Name LIKE 'M%' AND Population > 500000;
EXPLAIN
SELECT * FROM City
WHERE Name LIKE 'M%' AND Population > 300000;
EXPLAIN
SELECT * FROM City
WHERE Name LIKE 'M%' AND Population > 5000000;
# The following 8 queries check that
# the previous 4 plans are valid and return
# the correct results when executed
SELECT * FROM City USE INDEX ()
WHERE Name LIKE 'C%' AND Population > 1000000;
SELECT * FROM City
WHERE Name LIKE 'C%' AND Population > 1000000;
SELECT * FROM City USE INDEX ()
WHERE Name LIKE 'M%' AND Population > 500000;
SELECT * FROM City
WHERE Name LIKE 'M%' AND Population > 500000;
SELECT * FROM City USE INDEX ()
WHERE Name LIKE 'M%' AND Population > 300000;
SELECT * FROM City
WHERE Name LIKE 'M%' AND Population > 300000;
SELECT * FROM City USE INDEX ()
WHERE Name LIKE 'M%' AND Population > 5000000;
SELECT * FROM City
WHERE Name LIKE 'M%' AND Population > 5000000;
# The output of the next 7 queries tells us about selectivities
# of the conditions utilized in 3 queries following after them
SELECT COUNT(*) FROM City WHERE Name BETWEEN 'M' AND 'N';
SELECT COUNT(*) FROM City WHERE Name BETWEEN 'G' AND 'J';
SELECT COUNT(*) FROM City WHERE Population > 1000000;
SELECT COUNT(*) FROM City WHERE Population > 700000;
SELECT COUNT(*) FROM City WHERE Population > 500000;
SELECT COUNT(*) FROM City WHERE Country LIKE 'C%';
SELECT COUNT(*) FROM City WHERE Country LIKE 'L%';
# The pattern of the WHERE condition used in the following 3 queries is
# range(key1) AND range(key2) AND range(key3)
# Varying values of the constants in the conjuncts of the condition
# we can get index intersection over different pairs of keys:
# over(key1,key2), over(key1,key3) and over(key2,key3)
EXPLAIN
SELECT * FROM City
WHERE Name BETWEEN 'M' AND 'N' AND Population > 1000000 AND Country LIKE 'C%';
EXPLAIN
SELECT * FROM City
WHERE Name BETWEEN 'G' AND 'J' AND Population > 700000 AND Country LIKE 'L%';
EXPLAIN
SELECT * FROM City
WHERE Name BETWEEN 'G' AND 'J' AND Population > 500000 AND Country LIKE 'C%';
# The following 6 queries check that
# the previous 3 plans are valid and return
# the correct results when executed
SELECT * FROM City USE INDEX ()
WHERE Name BETWEEN 'M' AND 'N' AND Population > 1000000 AND Country LIKE 'C%';
SELECT * FROM City
WHERE Name BETWEEN 'M' AND 'N' AND Population > 1000000 AND Country LIKE 'C%';
SELECT * FROM City USE INDEX ()
WHERE Name BETWEEN 'G' AND 'J' AND Population > 700000 AND Country LIKE 'M%';
SELECT * FROM City
WHERE Name BETWEEN 'G' AND 'J' AND Population > 700000 AND Country LIKE 'M%';
SELECT * FROM City USE INDEX ()
WHERE Name BETWEEN 'G' AND 'J' AND Population > 500000 AND Country LIKE 'C%';
SELECT * FROM City
WHERE Name BETWEEN 'G' AND 'J' AND Population > 500000 AND Country LIKE 'C%';
# The output of the next 9 queries tells us about selectivities
# of the conditions utilized in 5 queries following after them
SELECT COUNT(*) FROM City WHERE ID BETWEEN 500 AND 999;
SELECT COUNT(*) FROM City WHERE ID BETWEEN 3500 AND 3999;
SELECT COUNT(*) FROM City WHERE ID BETWEEN 1 AND 1000;
SELECT COUNT(*) FROM City WHERE Population > 700000;
SELECT COUNT(*) FROM City WHERE Population > 1000000;
SELECT COUNT(*) FROM City WHERE Population > 300000;
SELECT COUNT(*) FROM City WHERE Country LIKE 'C%';
SELECT COUNT(*) FROM City WHERE Country LIKE 'A%';
SELECT COUNT(*) FROM City WHERE Country BETWEEN 'S' AND 'Z';
# The pattern of the WHERE condition used in the following 5 queries is
# range(key1) AND range(key2) AND range(key3)
# with key1 happens to be a primary key (it matters only for InnoDB)
# Varying values of the constants in the conjuncts of the condition
# we can get index intersection either over all three keys, or over
# different pairs, or a range sacn over one of these keys.
# Bear in mind that the condition (Country LIKE 'A%') is actually
# equivalent to the condition (Country BETWEEN 'A' AND 'B') for the
# tested instance the table City.
EXPLAIN
SELECT * FROM City
WHERE ID BETWEEN 500 AND 999 AND Population > 700000 AND Country LIKE 'C%';
EXPLAIN
SELECT * FROM City
WHERE ID BETWEEN 500 AND 999 AND Population > 1000000 AND Country LIKE 'A%';
EXPLAIN
SELECT * FROM City
WHERE ID BETWEEN 500 AND 999 AND Population > 300000 AND Country LIKE 'C%';
EXPLAIN
SELECT * FROM City
WHERE ID BETWEEN 3500 AND 3999 AND Population > 700000
AND Country BETWEEN 'S' AND 'Z';
EXPLAIN
SELECT * FROM City
WHERE ID BETWEEN 1 AND 1000 AND Population > 700000
AND Country BETWEEN 'S' AND 'Z' ;
# The following 10 queries check that
# the previous 5 plans are valid and return
# the correct results when executed
SELECT * FROM City USE INDEX ()
WHERE ID BETWEEN 500 AND 999 AND Population > 700000 AND Country LIKE 'C%';
SELECT * FROM City
WHERE ID BETWEEN 500 AND 999 AND Population > 700000 AND Country LIKE 'C%';
SELECT * FROM City USE INDEX ()
WHERE ID BETWEEN 500 AND 999 AND Population > 1000000 AND Country LIKE 'A%';
SELECT * FROM City
WHERE ID BETWEEN 500 AND 999 AND Population > 1000000 AND Country LIKE 'A%';
SELECT * FROM City USE INDEX ()
WHERE ID BETWEEN 500 AND 999 AND Population > 300000 AND Country LIKE 'C%';
SELECT * FROM City
WHERE ID BETWEEN 500 AND 999 AND Population > 300000 AND Country LIKE 'C%';
SELECT * FROM City USE INDEX ()
WHERE ID BETWEEN 3500 AND 3999 AND Population > 700000
AND Country BETWEEN 'S' AND 'Z';
SELECT * FROM City
WHERE ID BETWEEN 3500 AND 3999 AND Population > 700000
AND Country BETWEEN 'S' AND 'Z';
SELECT * FROM City USE INDEX ()
WHERE ID BETWEEN 1 AND 1000 AND Population > 700000
AND Country BETWEEN 'S' AND 'Z' ;
SELECT * FROM City
WHERE ID BETWEEN 1 AND 1000 AND Population > 700000
AND Country BETWEEN 'S' AND 'Z' ;
SET SESSION sort_buffer_size = 2048;
# The following EXPLAIN command demonstrate that the execution plans
# may be different if sort_buffer_size is set to a small value
EXPLAIN
SELECT * FROM City WHERE
Name LIKE 'C%' AND Population > 1000000;
EXPLAIN
SELECT * FROM City WHERE
Name LIKE 'M%' AND Population > 500000;
EXPLAIN
SELECT * FROM City
WHERE Name LIKE 'C%' AND Population > 1000000 AND Country LIKE 'C%';
EXPLAIN
SELECT * FROM City
WHERE Name BETWEEN 'G' AND 'J' AND Population > 700000 AND Country LIKE 'M%';
EXPLAIN
SELECT * FROM City
WHERE Name BETWEEN 'G' AND 'J' AND Population > 500000 AND Country LIKE 'C%';
EXPLAIN
SELECT * FROM City
WHERE ID BETWEEN 500 AND 999 AND Population > 1000000 AND Country LIKE 'A%';
EXPLAIN
SELECT * FROM City
WHERE ID < 1000 AND Population > 700000 AND Country LIKE 'C%';
#Yet the query themselves return the correct results in this case as well
SELECT * FROM City WHERE
Name LIKE 'C%' AND Population > 1000000;
SELECT * FROM City WHERE
Name LIKE 'M%' AND Population > 500000;
SELECT * FROM City
WHERE Name BETWEEN 'M' AND 'N' AND Population > 1000000 AND Country LIKE 'C%';
SELECT * FROM City
WHERE Name BETWEEN 'G' AND 'J' AND Population > 700000 AND Country LIKE 'M%';
SELECT * FROM City
WHERE Name BETWEEN 'G' AND 'J' AND Population > 500000 AND Country LIKE 'C%';
SELECT * FROM City
WHERE ID BETWEEN 500 AND 999 AND Population > 1000000 AND Country LIKE 'A%';
SELECT * FROM City
WHERE ID < 1000 AND Population > 700000 AND Country LIKE 'C%';
SET SESSION sort_buffer_size = default;
# Instead of the index on the column Country create two compound indexes
# including this column as the first component
DROP INDEX Country ON City;
CREATE INDEX CountryID ON City(Country,ID);
CREATE INDEX CountryName ON City(Country,Name);
--disable_query_log
--disable_result_log
--disable_warnings
ANALYZE TABLE City;
--enable_warnings
--enable_result_log
--enable_query_log
# Check that the first component of a compound index can be used for
# index intersection, even in the cases when we have a ref access
# for this component
EXPLAIN
SELECT * FROM City
WHERE Country LIKE 'M%' AND Population > 700000;
EXPLAIN
SELECT * FROM City
WHERE Country='CHN' AND Population > 1000000;
EXPLAIN
SELECT * FROM City
WHERE Country='CHN' AND Population > 1000000 AND Name LIKE 'C%';
# Check that the previous 3 plans return the right results when executed
SELECT * FROM City USE INDEX ()
WHERE Country LIKE 'M%' AND Population > 700000;
SELECT * FROM City
WHERE Country LIKE 'M%' AND Population > 700000;
SELECT * FROM City USE INDEX ()
WHERE Country='CHN' AND Population > 1000000;
SELECT * FROM City
WHERE Country='CHN' AND Population > 1000000;
SELECT * FROM City USE INDEX ()
WHERE Country='CHN' AND Population > 1000000 AND Name LIKE 'C%';
SELECT * FROM City
WHERE Country='CHN' AND Population > 1000000 AND Name LIKE 'C%';
DROP DATABASE world;
use test;
SET SESSION optimizer_switch='index_merge_sort_intersection=on';
|