-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathjoin
321 lines (305 loc) · 11.4 KB
/
join
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
exec-ddl
CREATE TABLE a (k INT PRIMARY KEY, i INT, s STRING, d DECIMAL NOT NULL)
----
TABLE a
├── k int not null
├── i int
├── s string
├── d decimal not null
└── INDEX primary
└── k int not null
exec-ddl
CREATE TABLE b (x INT, z INT NOT NULL)
----
TABLE b
├── x int
├── z int not null
├── rowid int not null (hidden)
└── INDEX primary
└── rowid int not null (hidden)
opt
SELECT k, x FROM a INNER JOIN b ON k=x WHERE d=1.0
----
project
├── columns: k:1(int!null) x:5(int!null)
├── stats: [rows=100]
├── cost: 2124.725
├── fd: (1)==(5), (5)==(1)
└── inner-join
├── columns: k:1(int!null) d:4(decimal!null) x:5(int!null)
├── stats: [rows=100, distinct(1)=10, null(1)=0, distinct(4)=1, null(4)=0, distinct(5)=10, null(5)=0]
├── cost: 2123.715
├── fd: ()-->(4), (1)==(5), (5)==(1)
├── scan b
│ ├── columns: x:5(int)
│ ├── stats: [rows=1000, distinct(5)=100, null(5)=10]
│ └── cost: 1040.01
├── select
│ ├── columns: k:1(int!null) d:4(decimal!null)
│ ├── stats: [rows=10, distinct(1)=10, null(1)=0, distinct(4)=1, null(4)=0]
│ ├── cost: 1070.02
│ ├── key: (1)
│ ├── fd: ()-->(4)
│ ├── scan a
│ │ ├── columns: k:1(int!null) d:4(decimal!null)
│ │ ├── stats: [rows=1000, distinct(1)=1000, null(1)=0, distinct(4)=100, null(4)=0]
│ │ ├── cost: 1060.01
│ │ ├── key: (1)
│ │ └── fd: (1)-->(4)
│ └── filters
│ └── d = 1.0 [type=bool, outer=(4), constraints=(/4: [/1.0 - /1.0]; tight), fd=()-->(4)]
└── filters
└── k = x [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ]), fd=(1)==(5), (5)==(1)]
# Verify that we pick merge join if we force it.
opt
SELECT k, x FROM a INNER MERGE JOIN b ON k=x
----
inner-join (merge)
├── columns: k:1(int!null) x:5(int!null)
├── left ordering: +1
├── right ordering: +5
├── stats: [rows=1000, distinct(1)=100, null(1)=0, distinct(5)=100, null(5)=0]
├── cost: 2339.35569
├── fd: (1)==(5), (5)==(1)
├── scan a
│ ├── columns: k:1(int!null)
│ ├── stats: [rows=1000, distinct(1)=1000, null(1)=0]
│ ├── cost: 1050.01
│ ├── key: (1)
│ └── ordering: +1
├── sort
│ ├── columns: x:5(int)
│ ├── stats: [rows=1000, distinct(5)=100, null(5)=10]
│ ├── cost: 1259.33569
│ ├── ordering: +5
│ └── scan b
│ ├── columns: x:5(int)
│ ├── stats: [rows=1000, distinct(5)=100, null(5)=10]
│ └── cost: 1040.01
└── filters (true)
# Verify that we pick lookup join if we force it. Note that lookup join is only
# possible if b is the left table.
opt
SELECT k, x FROM b INNER LOOKUP JOIN a ON k=x
----
inner-join (lookup a)
├── columns: k:4(int!null) x:1(int!null)
├── key columns: [1] = [4]
├── stats: [rows=1000, distinct(1)=100, null(1)=0, distinct(4)=100, null(4)=0]
├── cost: 6090.02
├── fd: (1)==(4), (4)==(1)
├── scan b
│ ├── columns: x:1(int)
│ ├── stats: [rows=1000, distinct(1)=100, null(1)=10]
│ └── cost: 1040.01
└── filters (true)
# Verify that if we force lookup join but one isn't possible, the hash join has
# huge cost (this will result in an error if we try to execbuild the result).
opt
SELECT k, x FROM a INNER LOOKUP JOIN b ON k=x
----
inner-join
├── columns: k:1(int!null) x:5(int!null)
├── flags: no-merge-join;no-hash-join
├── stats: [rows=1000, distinct(1)=100, null(1)=0, distinct(5)=100, null(5)=0]
├── cost: 1e+100
├── fd: (1)==(5), (5)==(1)
├── scan a
│ ├── columns: k:1(int!null)
│ ├── stats: [rows=1000, distinct(1)=1000, null(1)=0]
│ ├── cost: 1050.01
│ └── key: (1)
├── scan b
│ ├── columns: x:5(int)
│ ├── stats: [rows=1000, distinct(5)=100, null(5)=10]
│ └── cost: 1040.01
└── filters
└── k = x [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ]), fd=(1)==(5), (5)==(1)]
exec-ddl
CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT, INDEX c_idx (c))
----
TABLE abc
├── a int not null
├── b int
├── c int
├── INDEX primary
│ └── a int not null
└── INDEX c_idx
├── c int
└── a int not null
exec-ddl
ALTER TABLE abc INJECT STATISTICS '[
{
"columns": ["a"],
"created_at": "2018-05-01 1:00:00.00000+00:00",
"row_count": 500000000,
"distinct_count": 500000000
}
]'
----
# Check that we choose the index join when it makes sense.
opt
SELECT * FROM abc WHERE c = 1
----
index-join abc
├── columns: a:1(int!null) b:2(int) c:3(int!null)
├── stats: [rows=9.9, distinct(1)=9.9, null(1)=0, distinct(3)=1, null(3)=0]
├── cost: 50.609
├── key: (1)
├── fd: ()-->(3), (1)-->(2)
└── scan abc@c_idx
├── columns: a:1(int!null) c:3(int!null)
├── constraint: /3/1: [/1 - /1]
├── stats: [rows=9.9, distinct(1)=9.9, null(1)=0, distinct(3)=1, null(3)=0]
├── cost: 10.306
├── key: (1)
└── fd: ()-->(3)
# Regression test for #34810: make sure we pick the lookup join that uses
# all equality columns.
exec-ddl
CREATE TABLE abcde (
a TEXT NOT NULL,
b UUID NOT NULL,
c UUID NOT NULL,
d VARCHAR(255) NOT NULL,
e TEXT NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (a, b, c),
UNIQUE INDEX idx_abd (a, b, d),
UNIQUE INDEX idx_abcd (a, b, c, d)
)
----
TABLE abcde
├── a string not null
├── b uuid not null
├── c uuid not null
├── d string not null
├── e string not null
├── INDEX primary
│ ├── a string not null
│ ├── b uuid not null
│ └── c uuid not null
├── INDEX idx_abd
│ ├── a string not null
│ ├── b uuid not null
│ ├── d string not null
│ └── c uuid not null (storing)
└── INDEX idx_abcd
├── a string not null
├── b uuid not null
├── c uuid not null
└── d string not null
exec-ddl
ALTER TABLE abcde INJECT STATISTICS '[
{
"columns": ["a"],
"created_at": "2019-02-08 04:10:40.001179+00:00",
"row_count": 250000,
"distinct_count": 1
},
{
"columns": ["b"],
"created_at": "2019-02-08 04:10:40.119954+00:00",
"row_count": 250000,
"distinct_count": 2
},
{
"columns": ["d"],
"created_at": "2019-02-08 04:10:40.119954+00:00",
"row_count": 250000,
"distinct_count": 125000
}
]'
----
exec-ddl
CREATE TABLE wxyz (
w TEXT NOT NULL,
x UUID NOT NULL,
y UUID NOT NULL,
z TEXT NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (w, x, y),
CONSTRAINT "foreign" FOREIGN KEY (w, x, y) REFERENCES abcde (a, b, c)
)
----
TABLE wxyz
├── w string not null
├── x uuid not null
├── y uuid not null
├── z string not null
├── INDEX primary
│ ├── w string not null
│ ├── x uuid not null
│ └── y uuid not null
└── FOREIGN KEY (w, x, y) REFERENCES t.public.abcde (a, b, c)
exec-ddl
ALTER TABLE wxyz INJECT STATISTICS '[
{
"columns": ["w"],
"created_at": "2019-02-08 04:10:40.001179+00:00",
"row_count": 10000,
"distinct_count": 1
},
{
"columns": ["x"],
"created_at": "2019-02-08 04:10:40.119954+00:00",
"row_count": 10000,
"distinct_count": 1
},
{
"columns": ["y"],
"created_at": "2019-02-08 04:10:40.119954+00:00",
"row_count": 10000,
"distinct_count": 2500
}
]'
----
opt
SELECT w, x, y, z
FROM wxyz
INNER JOIN abcde
ON w = a AND x = b AND y = c
WHERE w = 'foo' AND x = '2AB23800-06B1-4E19-A3BB-DF3768B808D2'
ORDER BY d
LIMIT 10
----
project
├── columns: w:1(string!null) x:2(uuid!null) y:3(uuid!null) z:4(string!null) [hidden: d:8(string!null)]
├── cardinality: [0 - 10]
├── stats: [rows=10]
├── cost: 122481.301
├── key: (8)
├── fd: ()-->(1,2), (3)-->(4,8), (8)-->(3,4)
├── ordering: +8 opt(1,2) [actual: +8]
└── limit
├── columns: w:1(string!null) x:2(uuid!null) y:3(uuid!null) z:4(string!null) a:5(string!null) b:6(uuid!null) c:7(uuid!null) d:8(string!null)
├── internal-ordering: +8 opt(1,2,5,6)
├── cardinality: [0 - 10]
├── stats: [rows=10]
├── cost: 122481.191
├── key: (7)
├── fd: ()-->(1,2,5,6), (3)-->(4), (7)-->(8), (8)-->(7), (1)==(5), (5)==(1), (2)==(6), (6)==(2), (3)==(7), (7)==(3)
├── ordering: +8 opt(1,2,5,6) [actual: +8]
├── sort
│ ├── columns: w:1(string!null) x:2(uuid!null) y:3(uuid!null) z:4(string!null) a:5(string!null) b:6(uuid!null) c:7(uuid!null) d:8(string!null)
│ ├── stats: [rows=50048.8759, distinct(1)=1, null(1)=0, distinct(2)=1, null(2)=0, distinct(3)=2500, null(3)=0, distinct(4)=1000, null(4)=0, distinct(5)=1, null(5)=0, distinct(6)=1, null(6)=0, distinct(7)=2500, null(7)=0, distinct(8)=38781.1698, null(8)=0]
│ ├── cost: 122481.081
│ ├── key: (7)
│ ├── fd: ()-->(1,2,5,6), (3)-->(4), (7)-->(8), (8)-->(7), (1)==(5), (5)==(1), (2)==(6), (6)==(2), (3)==(7), (7)==(3)
│ ├── ordering: +8 opt(1,2,5,6) [actual: +8]
│ └── inner-join (lookup abcde@idx_abcd)
│ ├── columns: w:1(string!null) x:2(uuid!null) y:3(uuid!null) z:4(string!null) a:5(string!null) b:6(uuid!null) c:7(uuid!null) d:8(string!null)
│ ├── key columns: [1 2 3] = [5 6 7]
│ ├── stats: [rows=50048.8759, distinct(1)=1, null(1)=0, distinct(2)=1, null(2)=0, distinct(3)=2500, null(3)=0, distinct(4)=1000, null(4)=0, distinct(5)=1, null(5)=0, distinct(6)=1, null(6)=0, distinct(7)=2500, null(7)=0, distinct(8)=38781.1698, null(8)=0]
│ ├── cost: 105853.783
│ ├── key: (7)
│ ├── fd: ()-->(1,2,5,6), (3)-->(4), (7)-->(8), (8)-->(7), (1)==(5), (5)==(1), (2)==(6), (6)==(2), (3)==(7), (7)==(3)
│ ├── scan wxyz
│ │ ├── columns: w:1(string!null) x:2(uuid!null) y:3(uuid!null) z:4(string!null)
│ │ ├── constraint: /1/2/3: [/'foo'/'2ab23800-06b1-4e19-a3bb-df3768b808d2' - /'foo'/'2ab23800-06b1-4e19-a3bb-df3768b808d2']
│ │ ├── stats: [rows=10000, distinct(1)=1, null(1)=0, distinct(2)=1, null(2)=0, distinct(3)=2500, null(3)=0, distinct(4)=1000, null(4)=0]
│ │ ├── cost: 10800.01
│ │ ├── key: (3)
│ │ └── fd: ()-->(1,2), (3)-->(4)
│ └── filters
│ ├── a = 'foo' [type=bool, outer=(5), constraints=(/5: [/'foo' - /'foo']; tight), fd=()-->(5)]
│ └── b = '2ab23800-06b1-4e19-a3bb-df3768b808d2' [type=bool, outer=(6), constraints=(/6: [/'2ab23800-06b1-4e19-a3bb-df3768b808d2' - /'2ab23800-06b1-4e19-a3bb-df3768b808d2']; tight), fd=()-->(6)]
└── const: 10 [type=int]