-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
scan
245 lines (228 loc) · 8.05 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
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) 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
├── k int not null
└── j jsonb (storing)
# --------------------------------------------------
# GenerateIndexScans
# --------------------------------------------------
# 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)
└── ordering: +4,+1,+2
memo
SELECT s, i, f FROM a ORDER BY s, k, i
----
[7: "p:s:4,i:2,f:3 o:+4,+1,+2"]
memo
├── 7: (scan a) (scan a@s_idx)
│ ├── "" [cost=1000.00]
│ │ └── best: (scan a)
│ └── "p:s:4,i:2,f:3 o:+4,+1,+2" [cost=1000.00]
│ └── best: (scan a@s_idx)
├── 6: (projections 2 3 4 5)
├── 5: (variable a.k)
├── 4: (variable a.f)
├── 3: (variable a.i)
├── 2: (variable a.s)
└── 1: (scan a)
# 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)
└── ordering: +1,+2,+4
memo
SELECT s, i, f FROM a ORDER BY k, i, s
----
[7: "p:s:4,i:2,f:3 o:+1,+2,+4"]
memo
├── 7: (scan a) (scan a@s_idx)
│ ├── "" [cost=1000.00]
│ │ └── best: (scan a)
│ └── "p:s:4,i:2,f:3 o:+1,+2,+4" [cost=1000.00]
│ └── best: (scan a)
├── 6: (projections 2 3 4 5)
├── 5: (variable a.k)
├── 4: (variable a.f)
├── 3: (variable a.i)
├── 2: (variable a.s)
└── 1: (scan a)
# Secondary index has right order, but is not covering.
opt
SELECT s, j FROM a ORDER BY s
----
sort
├── columns: s:4(string) j:5(jsonb)
├── ordering: +4
└── scan a
└── columns: a.s:4(string) a.j:5(jsonb)
memo
SELECT s, j FROM a ORDER BY s
----
[5: "p:s:4,j:5 o:+4"]
memo
├── 5: (scan a) (scan a@si_idx)
│ ├── "" [cost=1000.00]
│ │ └── best: (scan a)
│ └── "p:s:4,j:5 o:+4" [cost=1250.00]
│ └── best: (sort 5)
├── 4: (projections 2 3)
├── 3: (variable a.j)
├── 2: (variable a.s)
└── 1: (scan a)
# Consider three different indexes, and pick index with multiple keys.
opt
SELECT i, k FROM a ORDER BY s DESC, i, k
----
scan a@si_idx
├── columns: i:2(int) k:1(int!null)
└── ordering: -4,+2,+1
memo
SELECT i, k FROM a ORDER BY s DESC, i, k
----
[6: "p:i:2,k:1 o:-4,+2,+1"]
memo
├── 6: (scan a) (scan a@s_idx) (scan a@si_idx)
│ ├── "" [cost=1000.00]
│ │ └── best: (scan a)
│ └── "p:i:2,k:1 o:-4,+2,+1" [cost=1000.00]
│ └── best: (scan a@si_idx)
├── 5: (projections 2 3 4)
├── 4: (variable a.s)
├── 3: (variable a.k)
├── 2: (variable a.i)
└── 1: (scan a)
optsteps
SELECT i, k FROM a WHERE s >= 'foo'
----
----
*** Initial expr:
project
├── columns: i:2(int) k:1(int!null)
├── select
│ ├── columns: a.k:1(int!null) a.i:2(int) a.f:3(float) a.s:4(string) a.j:5(jsonb)
│ ├── scan a
│ │ └── columns: a.k:1(int!null) a.i:2(int) a.f:3(float) a.s:4(string) a.j:5(jsonb)
│ └── ge [type=bool, outer=(4), constraints=(/4: [/'foo' - ]; tight)]
│ ├── variable: a.s [type=string, outer=(4)]
│ └── const: 'foo' [type=string]
└── projections [outer=(1,2)]
├── variable: a.i [type=int, outer=(2)]
└── variable: a.k [type=int, outer=(1)]
*** EnsureSelectFilters applied; best expr changed:
project
├── columns: i:2(int) k:1(int!null)
├── select
│ ├── columns: a.k:1(int!null) a.i:2(int) a.f:3(float) a.s:4(string) a.j:5(jsonb)
│ ├── scan a
│ │ └── columns: a.k:1(int!null) a.i:2(int) a.f:3(float) a.s:4(string) a.j:5(jsonb)
- │ └── ge [type=bool, outer=(4), constraints=(/4: [/'foo' - ]; tight)]
- │ ├── variable: a.s [type=string, outer=(4)]
- │ └── const: 'foo' [type=string]
+ │ └── filters [type=bool, outer=(4), constraints=(/4: [/'foo' - ]; tight)]
+ │ └── ge [type=bool, outer=(4), constraints=(/4: [/'foo' - ]; tight)]
+ │ ├── variable: a.s [type=string, outer=(4)]
+ │ └── const: 'foo' [type=string]
└── projections [outer=(1,2)]
├── variable: a.i [type=int, outer=(2)]
└── variable: a.k [type=int, outer=(1)]
*** FilterUnusedSelectCols applied; best expr changed:
project
├── columns: i:2(int) k:1(int!null)
├── select
- │ ├── columns: a.k:1(int!null) a.i:2(int) a.f:3(float) a.s:4(string) a.j:5(jsonb)
+ │ ├── columns: a.k:1(int!null) a.i:2(int) a.s:4(string)
│ ├── scan a
- │ │ └── columns: a.k:1(int!null) a.i:2(int) a.f:3(float) a.s:4(string) a.j:5(jsonb)
+ │ │ └── columns: a.k:1(int!null) a.i:2(int) a.s:4(string)
│ └── filters [type=bool, outer=(4), constraints=(/4: [/'foo' - ]; tight)]
│ └── ge [type=bool, outer=(4), constraints=(/4: [/'foo' - ]; tight)]
│ ├── variable: a.s [type=string, outer=(4)]
│ └── const: 'foo' [type=string]
└── projections [outer=(1,2)]
├── variable: a.i [type=int, outer=(2)]
└── variable: a.k [type=int, outer=(1)]
*** GenerateIndexScans applied; best expr unchanged.
*** ConstrainScan applied; best expr unchanged.
*** ConstrainScan applied; best expr changed:
project
├── columns: i:2(int) k:1(int!null)
- ├── select
+ ├── scan a@s_idx,constrained
│ ├── columns: a.k:1(int!null) a.i:2(int) a.s:4(string)
- │ ├── scan a
- │ │ └── columns: a.k:1(int!null) a.i:2(int) a.s:4(string)
- │ └── filters [type=bool, outer=(4), constraints=(/4: [/'foo' - ]; tight)]
- │ └── ge [type=bool, outer=(4), constraints=(/4: [/'foo' - ]; tight)]
- │ ├── variable: a.s [type=string, outer=(4)]
- │ └── const: 'foo' [type=string]
+ │ └── constraint: /4/1: [/'foo' - ]
└── projections [outer=(1,2)]
├── variable: a.i [type=int, outer=(2)]
└── variable: a.k [type=int, outer=(1)]
*** ConstrainScan applied; best expr unchanged.
*** Final best expr:
project
├── columns: i:2(int) k:1(int!null)
├── scan a@s_idx,constrained
│ ├── columns: a.k:1(int!null) a.i:2(int) a.s:4(string)
│ └── constraint: /4/1: [/'foo' - ]
└── projections [outer=(1,2)]
├── variable: a.i [type=int, outer=(2)]
└── variable: a.k [type=int, outer=(1)]
----
----
memo
SELECT i, k FROM a WHERE s >= 'foo'
----
[12: "p:i:2,k:1"]
memo
├── 13: (true)
├── 12: (project 11 9)
│ └── "p:i:2,k:1" [cost=100.00]
│ └── best: (project 11 9)
├── 11: (select 10 5) (scan a@s_idx,constrained) (scan a@si_idx,constrained)
│ └── "" [cost=100.00]
│ └── best: (scan a@s_idx,constrained)
├── 10: (scan a) (scan a@s_idx) (scan a@si_idx)
│ └── "" [cost=1000.00]
│ └── best: (scan a)
├── 9: (projections 7 8)
├── 8: (variable a.k)
├── 7: (variable a.i)
├── 6: (select 1 5)
├── 5: (filters 4)
├── 4: (ge 2 3)
├── 3: (const 'foo')
├── 2: (variable a.s)
└── 1: (scan a)