-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathscan
512 lines (485 loc) · 17.2 KB
/
scan
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
exec-ddl
CREATE TABLE a
(
k INT PRIMARY KEY,
i INT,
f FLOAT,
s STRING,
j JSON,
INDEX s_idx (s) STORING (i, f),
INDEX si_idx (s DESC, i DESC) STORING (j)
)
----
TABLE a
├── k int not null
├── i int
├── f float
├── s string
├── j jsonb
├── INDEX primary
│ └── k int not null
├── INDEX s_idx
│ ├── s string
│ ├── k int not null
│ ├── i int (storing)
│ └── f float (storing)
└── INDEX si_idx
├── s string desc
├── i int desc
├── k int not null
└── j jsonb (storing)
# --------------------------------------------------
# GenerateIndexScans
# --------------------------------------------------
# Revscan won't be used here because there is no index with f
# sorted by DESC
opt
SELECT k,f FROM a ORDER BY f DESC, k ASC LIMIT 10
----
limit
├── columns: k:1(int!null) f:3(float)
├── cardinality: [0 - 10]
├── key: (1)
├── fd: (1)-->(3)
├── ordering: -3,+1
├── sort
│ ├── columns: k:1(int!null) f:3(float)
│ ├── key: (1)
│ ├── fd: (1)-->(3)
│ ├── ordering: -3,+1
│ └── scan a@s_idx
│ ├── columns: k:1(int!null) f:3(float)
│ ├── key: (1)
│ └── fd: (1)-->(3)
└── const: 10 [type=int]
opt
SELECT k,f from a ORDER BY k DESC LIMIT 10
----
scan a,rev
├── columns: k:1(int!null) f:3(float)
├── limit: 10
├── key: (1)
├── fd: (1)-->(3)
└── ordering: -1
memo
SELECT k,f FROM a ORDER BY k DESC LIMIT 10
----
memo (optimized)
├── G1: (limit G2 G7 ordering=-1) (scan a,rev,cols=(1,3),lim=10) (index-join G3 a,cols=(1,3)) (index-join G4 a,cols=(1,3))
│ ├── "[presentation: k:1,f:3] [ordering: -1]"
│ │ ├── best: (scan a,rev,cols=(1,3),lim=10)
│ │ └── cost: 10.70
│ └── ""
│ ├── best: (scan a,rev,cols=(1,3),lim=10)
│ └── cost: 10.70
├── G2: (scan a,cols=(1,3)) (scan a,rev,cols=(1,3)) (scan a@s_idx,cols=(1,3)) (scan a@s_idx,rev,cols=(1,3)) (index-join G5 a,cols=(1,3)) (index-join G6 a,cols=(1,3))
│ ├── ""
│ │ ├── best: (scan a@s_idx,cols=(1,3))
│ │ └── cost: 1060.00
│ └── "[ordering: -1]"
│ ├── best: (scan a,rev,cols=(1,3))
│ └── cost: 1070.00
├── G3: (limit G5 G7 ordering=-1)
│ ├── ""
│ │ ├── best: (limit G5="[ordering: -1]" G7 ordering=-1)
│ │ └── cost: 1149.66
│ └── "[ordering: -1]"
│ ├── best: (limit G5="[ordering: -1]" G7 ordering=-1)
│ └── cost: 1149.66
├── G4: (limit G6 G7 ordering=-1)
│ ├── ""
│ │ ├── best: (limit G6="[ordering: -1]" G7 ordering=-1)
│ │ └── cost: 1149.66
│ └── "[ordering: -1]"
│ ├── best: (limit G6="[ordering: -1]" G7 ordering=-1)
│ └── cost: 1149.66
├── G5: (scan a@si_idx,cols=(1))
│ ├── ""
│ │ ├── best: (scan a@si_idx,cols=(1))
│ │ └── cost: 1050.00
│ └── "[ordering: -1]"
│ ├── best: (sort G5)
│ └── cost: 1149.66
├── G6: (scan a@si_idx,rev,cols=(1))
│ ├── ""
│ │ ├── best: (scan a@si_idx,rev,cols=(1))
│ │ └── cost: 1050.00
│ └── "[ordering: -1]"
│ ├── best: (sort G6)
│ └── cost: 1149.66
└── G7: (const 10)
opt
SELECT s FROM a ORDER BY k DESC
----
scan a,rev
├── columns: s:4(string)
├── key: (1)
├── fd: (1)-->(4)
└── ordering: -1
opt
SELECT k FROM a ORDER BY k ASC
----
scan a
├── columns: k:1(int!null)
├── key: (1)
└── ordering: +1
opt
SELECT k FROM a ORDER BY k DESC
----
scan a,rev
├── columns: k:1(int!null)
├── key: (1)
└── ordering: -1
opt
SELECT s,i,k,j FROM a ORDER BY s DESC, i DESC, k ASC
----
scan a@si_idx
├── columns: s:4(string) i:2(int) k:1(int!null) j:5(jsonb)
├── key: (1)
├── fd: (1)-->(2,4,5)
└── ordering: -4,-2,+1
# Revscan node won't be used because ordering is
# only partial (reverse) match with existing indices
opt
SELECT s,i,k,j FROM a ORDER BY s DESC, i DESC, k DESC
----
sort
├── columns: s:4(string) i:2(int) k:1(int!null) j:5(jsonb)
├── key: (1)
├── fd: (1)-->(2,4,5)
├── ordering: -4,-2,-1
└── scan a@si_idx
├── columns: k:1(int!null) i:2(int) s:4(string) j:5(jsonb)
├── key: (1)
└── fd: (1)-->(2,4,5)
# Revscan node won't be used because ordering is
# only partial (reverse) match with existing indices
opt
SELECT s,i,k,j FROM a ORDER BY s DESC, i ASC, k DESC
----
sort
├── columns: s:4(string) i:2(int) k:1(int!null) j:5(jsonb)
├── key: (1)
├── fd: (1)-->(2,4,5)
├── ordering: -4,+2,-1
└── scan a@si_idx
├── columns: k:1(int!null) i:2(int) s:4(string) j:5(jsonb)
├── key: (1)
└── fd: (1)-->(2,4,5)
opt
SELECT s,i,k,j FROM a ORDER BY s ASC, i ASC, k DESC
----
scan a@si_idx,rev
├── columns: s:4(string) i:2(int) k:1(int!null) j:5(jsonb)
├── key: (1)
├── fd: (1)-->(2,4,5)
└── ordering: +4,+2,-1
memo
SELECT k FROM a ORDER BY k ASC
----
memo (optimized)
└── G1: (scan a,cols=(1)) (scan a,rev,cols=(1)) (scan a@s_idx,cols=(1)) (scan a@s_idx,rev,cols=(1)) (scan a@si_idx,cols=(1)) (scan a@si_idx,rev,cols=(1))
├── "[presentation: k:1] [ordering: +1]"
│ ├── best: (scan a,cols=(1))
│ └── cost: 1060.00
└── ""
├── best: (scan a@s_idx,cols=(1))
└── cost: 1050.00
# Scan of secondary index is lowest cost.
opt
SELECT s, i, f FROM a ORDER BY s, k, i
----
scan a@s_idx
├── columns: s:4(string) i:2(int) f:3(float)
├── key: (1)
├── fd: (1)-->(2-4)
└── ordering: +4,+1
memo
SELECT s, i, f FROM a ORDER BY s, k, i
----
memo (optimized)
├── G1: (scan a,cols=(1-4)) (scan a,rev,cols=(1-4)) (scan a@s_idx,cols=(1-4)) (scan a@s_idx,rev,cols=(1-4)) (index-join G2 a,cols=(1-4)) (index-join G3 a,cols=(1-4))
│ ├── "[presentation: s:4,i:2,f:3] [ordering: +4,+1]"
│ │ ├── best: (scan a@s_idx,cols=(1-4))
│ │ └── cost: 1080.00
│ └── ""
│ ├── best: (scan a@s_idx,cols=(1-4))
│ └── cost: 1080.00
├── G2: (scan a@si_idx,cols=(1,2,4))
│ ├── ""
│ │ ├── best: (scan a@si_idx,cols=(1,2,4))
│ │ └── cost: 1070.00
│ └── "[ordering: +4,+1]"
│ ├── best: (sort G2)
│ └── cost: 1169.66
└── G3: (scan a@si_idx,rev,cols=(1,2,4))
├── ""
│ ├── best: (scan a@si_idx,rev,cols=(1,2,4))
│ └── cost: 1070.00
└── "[ordering: +4,+1]"
├── best: (sort G3)
└── cost: 1169.66
memo
SELECT s, i, f FROM a ORDER BY f
----
memo (optimized)
├── G1: (scan a,cols=(2-4)) (scan a,rev,cols=(2-4)) (scan a@s_idx,cols=(2-4)) (scan a@s_idx,rev,cols=(2-4)) (index-join G2 a,cols=(2-4)) (index-join G3 a,cols=(2-4))
│ ├── "[presentation: s:4,i:2,f:3] [ordering: +3]"
│ │ ├── best: (sort G1)
│ │ └── cost: 1169.66
│ └── ""
│ ├── best: (scan a@s_idx,cols=(2-4))
│ └── cost: 1070.00
├── G2: (scan a@si_idx,cols=(1,2,4))
│ └── ""
│ ├── best: (scan a@si_idx,cols=(1,2,4))
│ └── cost: 1070.00
└── G3: (scan a@si_idx,rev,cols=(1,2,4))
└── ""
├── best: (scan a@si_idx,rev,cols=(1,2,4))
└── cost: 1070.00
memo
SELECT s, i, f FROM a ORDER BY s DESC, i
----
memo (optimized)
├── G1: (scan a,cols=(2-4)) (scan a,rev,cols=(2-4)) (scan a@s_idx,cols=(2-4)) (scan a@s_idx,rev,cols=(2-4)) (index-join G2 a,cols=(2-4)) (index-join G3 a,cols=(2-4))
│ ├── "[presentation: s:4,i:2,f:3] [ordering: -4,+2]"
│ │ ├── best: (sort G1)
│ │ └── cost: 1169.66
│ └── ""
│ ├── best: (scan a@s_idx,cols=(2-4))
│ └── cost: 1070.00
├── G2: (scan a@si_idx,cols=(1,2,4))
│ ├── ""
│ │ ├── best: (scan a@si_idx,cols=(1,2,4))
│ │ └── cost: 1070.00
│ └── "[ordering: -4,+2]"
│ ├── best: (sort G2)
│ └── cost: 1169.66
└── G3: (scan a@si_idx,rev,cols=(1,2,4))
├── ""
│ ├── best: (scan a@si_idx,rev,cols=(1,2,4))
│ └── cost: 1070.00
└── "[ordering: -4,+2]"
├── best: (sort G3)
└── cost: 1169.66
exec-ddl
CREATE TABLE abc (
a INT,
b INT,
c INT,
d CHAR,
PRIMARY KEY (a, b, c),
UNIQUE INDEX bc (b, c),
INDEX ba (b, a),
FAMILY (a, b, c),
FAMILY (d)
)
----
TABLE abc
├── a int not null
├── b int not null
├── c int not null
├── d string
├── INDEX primary
│ ├── a int not null
│ ├── b int not null
│ └── c int not null
├── INDEX bc
│ ├── b int not null
│ ├── c int not null
│ └── a int not null (storing)
└── INDEX ba
├── b int not null
├── a int not null
└── c int not null
memo
SELECT d FROM abc ORDER BY lower(d)
----
memo (optimized)
├── G1: (project G2 G3)
│ ├── "[presentation: d:4] [ordering: +5]"
│ │ ├── best: (sort G1)
│ │ └── cost: 1149.66
│ └── ""
│ ├── best: (project G2 G3)
│ └── cost: 1050.00
├── G2: (scan abc,cols=(4)) (scan abc,rev,cols=(4)) (index-join G4 abc,cols=(4)) (index-join G5 abc,cols=(4)) (index-join G6 abc,cols=(4)) (index-join G7 abc,cols=(4))
│ └── ""
│ ├── best: (scan abc,cols=(4))
│ └── cost: 1050.00
├── G3: (projections G8 abc.d)
├── G4: (scan abc@bc,cols=(1-3))
│ └── ""
│ ├── best: (scan abc@bc,cols=(1-3))
│ └── cost: 1060.00
├── G5: (scan abc@bc,rev,cols=(1-3))
│ └── ""
│ ├── best: (scan abc@bc,rev,cols=(1-3))
│ └── cost: 1060.00
├── G6: (scan abc@ba,cols=(1-3))
│ └── ""
│ ├── best: (scan abc@ba,cols=(1-3))
│ └── cost: 1060.00
├── G7: (scan abc@ba,rev,cols=(1-3))
│ └── ""
│ ├── best: (scan abc@ba,rev,cols=(1-3))
│ └── cost: 1060.00
├── G8: (function G9 lower)
└── G9: (variable abc.d)
memo
SELECT j FROM a WHERE s = 'foo'
----
memo (optimized)
├── G1: (project G2 G3)
│ └── "[presentation: j:5]"
│ ├── best: (project G2 G3)
│ └── cost: 1.51
├── G2: (select G4 G11) (scan a@si_idx,cols=(4,5),constrained) (scan a@si_idx,rev,cols=(4,5),constrained) (index-join G5 a,cols=(4,5)) (index-join G6 a,cols=(4,5)) (index-join G7 a,cols=(4,5)) (index-join G8 a,cols=(4,5))
│ └── ""
│ ├── best: (scan a@si_idx,cols=(4,5),constrained)
│ └── cost: 1.51
├── G3: (projections a.j)
├── G4: (scan a,cols=(4,5)) (scan a,rev,cols=(4,5)) (index-join G9 a,cols=(4,5)) (index-join G10 a,cols=(4,5)) (scan a@si_idx,cols=(4,5)) (scan a@si_idx,rev,cols=(4,5))
│ └── ""
│ ├── best: (scan a@si_idx,cols=(4,5))
│ └── cost: 1060.00
├── G5: (select G9 G11) (scan a@s_idx,cols=(1,4),constrained)
│ └── ""
│ ├── best: (scan a@s_idx,cols=(1,4),constrained)
│ └── cost: 1.51
├── G6: (select G10 G11) (scan a@s_idx,rev,cols=(1,4),constrained)
│ └── ""
│ ├── best: (scan a@s_idx,rev,cols=(1,4),constrained)
│ └── cost: 1.51
├── G7: (scan a@s_idx,cols=(1,4),constrained)
│ └── ""
│ ├── best: (scan a@s_idx,cols=(1,4),constrained)
│ └── cost: 1.51
├── G8: (scan a@s_idx,rev,cols=(1,4),constrained)
│ └── ""
│ ├── best: (scan a@s_idx,rev,cols=(1,4),constrained)
│ └── cost: 1.51
├── G9: (scan a@s_idx,cols=(1,4))
│ └── ""
│ ├── best: (scan a@s_idx,cols=(1,4))
│ └── cost: 1060.00
├── G10: (scan a@s_idx,rev,cols=(1,4))
│ └── ""
│ ├── best: (scan a@s_idx,rev,cols=(1,4))
│ └── cost: 1060.00
├── G11: (filters G12)
├── G12: (eq G13 G14)
├── G13: (variable a.s)
└── G14: (const 'foo')
# Scan of primary index is lowest cost.
opt
SELECT s, i, f FROM a ORDER BY k, i, s
----
scan a
├── columns: s:4(string) i:2(int) f:3(float)
├── key: (1)
├── fd: (1)-->(2-4)
└── ordering: +1
memo
SELECT s, i, f FROM a ORDER BY k, i, s
----
memo (optimized)
├── G1: (scan a,cols=(1-4)) (scan a,rev,cols=(1-4)) (scan a@s_idx,cols=(1-4)) (scan a@s_idx,rev,cols=(1-4)) (index-join G2 a,cols=(1-4)) (index-join G3 a,cols=(1-4))
│ ├── "[presentation: s:4,i:2,f:3] [ordering: +1]"
│ │ ├── best: (scan a,cols=(1-4))
│ │ └── cost: 1090.00
│ └── ""
│ ├── best: (scan a@s_idx,cols=(1-4))
│ └── cost: 1080.00
├── G2: (scan a@si_idx,cols=(1,2,4))
│ ├── ""
│ │ ├── best: (scan a@si_idx,cols=(1,2,4))
│ │ └── cost: 1070.00
│ └── "[ordering: +1]"
│ ├── best: (sort G2)
│ └── cost: 1169.66
└── G3: (scan a@si_idx,rev,cols=(1,2,4))
├── ""
│ ├── best: (scan a@si_idx,rev,cols=(1,2,4))
│ └── cost: 1070.00
└── "[ordering: +1]"
├── best: (sort G3)
└── cost: 1169.66
# Secondary index has right order, but is not covering.
opt
SELECT s, j FROM a ORDER BY s
----
scan a@si_idx,rev
├── columns: s:4(string) j:5(jsonb)
└── ordering: +4
memo
SELECT s, j FROM a ORDER BY s
----
memo (optimized)
├── G1: (scan a,cols=(4,5)) (scan a,rev,cols=(4,5)) (index-join G2 a,cols=(4,5)) (index-join G3 a,cols=(4,5)) (scan a@si_idx,cols=(4,5)) (scan a@si_idx,rev,cols=(4,5))
│ ├── "[presentation: s:4,j:5] [ordering: +4]"
│ │ ├── best: (scan a@si_idx,rev,cols=(4,5))
│ │ └── cost: 1060.00
│ └── ""
│ ├── best: (scan a@si_idx,cols=(4,5))
│ └── cost: 1060.00
├── G2: (scan a@s_idx,cols=(1,4))
│ ├── ""
│ │ ├── best: (scan a@s_idx,cols=(1,4))
│ │ └── cost: 1060.00
│ └── "[ordering: +4]"
│ ├── best: (scan a@s_idx,cols=(1,4))
│ └── cost: 1060.00
└── G3: (scan a@s_idx,rev,cols=(1,4))
├── ""
│ ├── best: (scan a@s_idx,rev,cols=(1,4))
│ └── cost: 1060.00
└── "[ordering: +4]"
├── best: (sort G3)
└── cost: 1159.66
# Consider three different indexes, and pick index with multiple keys.
opt
SELECT i, k FROM a ORDER BY s DESC, i, k
----
sort
├── columns: i:2(int) k:1(int!null)
├── key: (1)
├── fd: (1)-->(2,4)
├── ordering: -4,+2,+1
└── scan a@s_idx
├── columns: k:1(int!null) i:2(int) s:4(string)
├── key: (1)
└── fd: (1)-->(2,4)
memo
SELECT i, k FROM a ORDER BY s DESC, i, k
----
memo (optimized)
└── G1: (scan a,cols=(1,2,4)) (scan a,rev,cols=(1,2,4)) (scan a@s_idx,cols=(1,2,4)) (scan a@s_idx,rev,cols=(1,2,4)) (scan a@si_idx,cols=(1,2,4)) (scan a@si_idx,rev,cols=(1,2,4))
├── "[presentation: i:2,k:1] [ordering: -4,+2,+1]"
│ ├── best: (sort G1)
│ └── cost: 1169.66
└── ""
├── best: (scan a@s_idx,cols=(1,2,4))
└── cost: 1070.00
memo
SELECT i, k FROM a WHERE s >= 'foo'
----
memo (optimized)
├── G1: (project G2 G3)
│ └── "[presentation: i:2,k:1]"
│ ├── best: (project G2 G3)
│ └── cost: 356.67
├── G2: (select G4 G5) (scan a@s_idx,cols=(1,2,4),constrained) (scan a@s_idx,rev,cols=(1,2,4),constrained) (scan a@si_idx,cols=(1,2,4),constrained) (scan a@si_idx,rev,cols=(1,2,4),constrained)
│ └── ""
│ ├── best: (scan a@s_idx,cols=(1,2,4),constrained)
│ └── cost: 356.67
├── G3: (projections a.k a.i)
├── G4: (scan a,cols=(1,2,4)) (scan a,rev,cols=(1,2,4)) (scan a@s_idx,cols=(1,2,4)) (scan a@s_idx,rev,cols=(1,2,4)) (scan a@si_idx,cols=(1,2,4)) (scan a@si_idx,rev,cols=(1,2,4))
│ └── ""
│ ├── best: (scan a@s_idx,cols=(1,2,4))
│ └── cost: 1070.00
├── G5: (filters G6)
├── G6: (ge G7 G8)
├── G7: (variable a.s)
└── G8: (const 'foo')