-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
join_order
2768 lines (2708 loc) · 89.4 KB
/
join_order
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
exec-ddl
CREATE TABLE bx (
b INT PRIMARY KEY,
x INT
)
----
exec-ddl
CREATE TABLE cy (
c INT PRIMARY KEY,
y INT
)
----
exec-ddl
CREATE TABLE dz (
d INT PRIMARY KEY,
z INT
)
----
exec-ddl
CREATE TABLE abc (
a INT PRIMARY KEY,
b INT,
c INT,
d INT
)
----
opt join-limit=2 expect=ReorderJoins
SELECT * FROM abc, bx, cy WHERE a = 1 AND abc.b = bx.b AND abc.c = cy.c
----
inner-join (lookup cy)
├── columns: a:1!null b:2!null c:3!null d:4 b:7!null x:8 c:11!null y:12
├── key columns: [3] = [11]
├── lookup columns are key
├── cardinality: [0 - 1]
├── key: ()
├── fd: ()-->(1-4,7,8,11,12), (2)==(7), (3)==(11), (11)==(3), (7)==(2)
├── inner-join (lookup bx)
│ ├── columns: a:1!null abc.b:2!null abc.c:3 d:4 bx.b:7!null x:8
│ ├── key columns: [2] = [7]
│ ├── lookup columns are key
│ ├── cardinality: [0 - 1]
│ ├── key: ()
│ ├── fd: ()-->(1-4,7,8), (7)==(2), (2)==(7)
│ ├── scan abc
│ │ ├── columns: a:1!null abc.b:2 abc.c:3 d:4
│ │ ├── constraint: /1: [/1 - /1]
│ │ ├── cardinality: [0 - 1]
│ │ ├── key: ()
│ │ └── fd: ()-->(1-4)
│ └── filters (true)
└── filters (true)
opt join-limit=2 expect=ReorderJoins
SELECT * FROM bx, abc, cy WHERE a = 1 AND abc.b = bx.b AND abc.c = cy.c
----
inner-join (lookup bx)
├── columns: b:1!null x:2 a:5!null b:6!null c:7!null d:8 c:11!null y:12
├── key columns: [6] = [1]
├── lookup columns are key
├── cardinality: [0 - 1]
├── key: ()
├── fd: ()-->(1,2,5-8,11,12), (7)==(11), (1)==(6), (6)==(1), (11)==(7)
├── inner-join (lookup cy)
│ ├── columns: a:5!null abc.b:6 abc.c:7!null d:8 cy.c:11!null y:12
│ ├── key columns: [7] = [11]
│ ├── lookup columns are key
│ ├── cardinality: [0 - 1]
│ ├── key: ()
│ ├── fd: ()-->(5-8,11,12), (11)==(7), (7)==(11)
│ ├── scan abc
│ │ ├── columns: a:5!null abc.b:6 abc.c:7 d:8
│ │ ├── constraint: /5: [/1 - /1]
│ │ ├── cardinality: [0 - 1]
│ │ ├── key: ()
│ │ └── fd: ()-->(5-8)
│ └── filters (true)
└── filters (true)
opt join-limit=2 expect=ReorderJoins
SELECT * FROM bx, cy, abc WHERE a = 1 AND abc.b = bx.b AND abc.c = cy.c
----
inner-join (lookup bx)
├── columns: b:1!null x:2 c:5!null y:6 a:9!null b:10!null c:11!null d:12
├── key columns: [10] = [1]
├── lookup columns are key
├── cardinality: [0 - 1]
├── key: ()
├── fd: ()-->(1,2,5,6,9-12), (5)==(11), (1)==(10), (10)==(1), (11)==(5)
├── inner-join (lookup cy)
│ ├── columns: cy.c:5!null y:6 a:9!null abc.b:10 abc.c:11!null d:12
│ ├── key columns: [11] = [5]
│ ├── lookup columns are key
│ ├── cardinality: [0 - 1]
│ ├── key: ()
│ ├── fd: ()-->(5,6,9-12), (11)==(5), (5)==(11)
│ ├── scan abc
│ │ ├── columns: a:9!null abc.b:10 abc.c:11 d:12
│ │ ├── constraint: /9: [/1 - /1]
│ │ ├── cardinality: [0 - 1]
│ │ ├── key: ()
│ │ └── fd: ()-->(9-12)
│ └── filters (true)
└── filters (true)
# Reorder subtrees of size 2.
exploretrace join-limit=2 rule=ReorderJoins format=hide-all
SELECT *
FROM bx, cy, dz, abc
WHERE a = 1 AND abc.b = bx.b AND abc.c = cy.c AND cy.c = dz.d
----
----
================================================================================
ReorderJoins
================================================================================
Source expression:
inner-join (hash)
├── scan bx
├── inner-join (hash)
│ ├── scan cy
│ ├── inner-join (hash)
│ │ ├── scan dz
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ └── filters
│ │ └── dz.d = abc.c
│ └── filters
│ └── cy.c = dz.d
└── filters
└── abc.b = bx.b
New expression 1 of 1:
inner-join (hash)
├── scan bx
├── inner-join (hash)
│ ├── scan cy
│ ├── inner-join (hash)
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ ├── scan dz
│ │ └── filters
│ │ └── dz.d = abc.c
│ └── filters
│ └── cy.c = dz.d
└── filters
└── abc.b = bx.b
================================================================================
ReorderJoins
================================================================================
Source expression:
inner-join (hash)
├── scan bx
├── inner-join (hash)
│ ├── scan cy
│ ├── inner-join (lookup dz)
│ │ ├── lookup columns are key
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ └── filters (true)
│ └── filters
│ └── cy.c = dz.d
└── filters
└── abc.b = bx.b
New expression 1 of 5:
inner-join (hash)
├── scan bx
├── inner-join (hash)
│ ├── inner-join (lookup dz)
│ │ ├── lookup columns are key
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ └── filters (true)
│ ├── scan cy
│ └── filters
│ └── cy.c = dz.d
└── filters
└── abc.b = bx.b
New expression 2 of 5:
inner-join (hash)
├── scan bx
├── inner-join (hash)
│ ├── scan dz
│ ├── inner-join (hash)
│ │ ├── scan cy
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ └── filters
│ │ └── cy.c = abc.c
│ └── filters
│ └── dz.d = abc.c
└── filters
└── abc.b = bx.b
New expression 3 of 5:
inner-join (hash)
├── scan bx
├── inner-join (hash)
│ ├── inner-join (hash)
│ │ ├── scan cy
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ └── filters
│ │ └── cy.c = abc.c
│ ├── scan dz
│ └── filters
│ └── dz.d = abc.c
└── filters
└── abc.b = bx.b
New expression 4 of 5:
inner-join (hash)
├── scan bx
├── inner-join (hash)
│ ├── inner-join (hash)
│ │ ├── scan cy
│ │ ├── scan dz
│ │ └── filters
│ │ └── cy.c = dz.d
│ ├── scan abc
│ │ └── constraint: /13: [/1 - /1]
│ └── filters
│ └── dz.d = abc.c
└── filters
└── abc.b = bx.b
New expression 5 of 5:
inner-join (hash)
├── scan bx
├── inner-join (hash)
│ ├── scan abc
│ │ └── constraint: /13: [/1 - /1]
│ ├── inner-join (hash)
│ │ ├── scan cy
│ │ ├── scan dz
│ │ └── filters
│ │ └── cy.c = dz.d
│ └── filters
│ └── dz.d = abc.c
└── filters
└── abc.b = bx.b
================================================================================
ReorderJoins
================================================================================
Source expression:
inner-join (hash)
├── scan bx
├── inner-join (lookup cy)
│ ├── lookup columns are key
│ ├── inner-join (lookup dz)
│ │ ├── lookup columns are key
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ └── filters (true)
│ └── filters (true)
└── filters
└── abc.b = bx.b
New expression 1 of 3:
inner-join (hash)
├── inner-join (lookup cy)
│ ├── lookup columns are key
│ ├── inner-join (lookup dz)
│ │ ├── lookup columns are key
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ └── filters (true)
│ └── filters (true)
├── scan bx
└── filters
└── abc.b = bx.b
New expression 2 of 3:
inner-join (hash)
├── scan cy
├── inner-join (hash)
│ ├── scan bx
│ ├── inner-join (lookup dz)
│ │ ├── lookup columns are key
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ └── filters (true)
│ └── filters
│ └── abc.b = bx.b
└── filters
└── cy.c = dz.d
New expression 3 of 3:
inner-join (hash)
├── inner-join (hash)
│ ├── scan bx
│ ├── inner-join (lookup dz)
│ │ ├── lookup columns are key
│ │ ├── scan abc
│ │ │ └── constraint: /13: [/1 - /1]
│ │ └── filters (true)
│ └── filters
│ └── abc.b = bx.b
├── scan cy
└── filters
└── cy.c = dz.d
----
----
# No joins should be reordered besides commutation.
memo join-limit=0 expect-not=ReorderJoins
SELECT * FROM bx, cy, abc WHERE a = 1 AND abc.b = bx.b AND abc.c = cy.c
----
memo (optimized, ~22KB, required=[presentation: b:1,x:2,c:5,y:6,a:9,b:10,c:11,d:12])
├── G1: (inner-join G2 G3 G4) (merge-join G2 G3 G5 inner-join,+1,+10)
│ └── [presentation: b:1,x:2,c:5,y:6,a:9,b:10,c:11,d:12]
│ ├── best: (merge-join G2="[ordering: +1]" G3 G5 inner-join,+1,+10)
│ └── cost: 2151.99
├── G2: (scan bx,cols=(1,2))
│ ├── [ordering: +1]
│ │ ├── best: (scan bx,cols=(1,2))
│ │ └── cost: 1064.42
│ └── []
│ ├── best: (scan bx,cols=(1,2))
│ └── cost: 1064.42
├── G3: (inner-join G6 G7 G8) (merge-join G6 G7 G5 inner-join,+5,+11)
│ └── []
│ ├── best: (merge-join G6="[ordering: +5]" G7 G5 inner-join,+5,+11)
│ └── cost: 1078.54
├── G4: (filters G9)
├── G5: (filters)
├── G6: (scan cy,cols=(5,6))
│ ├── [ordering: +5]
│ │ ├── best: (scan cy,cols=(5,6))
│ │ └── cost: 1064.42
│ └── []
│ ├── best: (scan cy,cols=(5,6))
│ └── cost: 1064.42
├── G7: (select G10 G11) (scan abc,cols=(9-12),constrained)
│ └── []
│ ├── best: (scan abc,cols=(9-12),constrained)
│ └── cost: 5.09
├── G8: (filters G12)
├── G9: (eq G13 G14)
├── G10: (scan abc,cols=(9-12))
│ └── []
│ ├── best: (scan abc,cols=(9-12))
│ └── cost: 1104.82
├── G11: (filters G15)
├── G12: (eq G16 G17)
├── G13: (variable abc.b)
├── G14: (variable bx.b)
├── G15: (eq G18 G19)
├── G16: (variable abc.c)
├── G17: (variable cy.c)
├── G18: (variable a)
└── G19: (const 1)
memo join-limit=2
SELECT * FROM bx, cy, abc WHERE a = 1 AND abc.b = bx.b AND abc.c = cy.c
----
memo (optimized, ~33KB, required=[presentation: b:1,x:2,c:5,y:6,a:9,b:10,c:11,d:12])
├── G1: (inner-join G2 G3 G4) (inner-join G3 G2 G4) (inner-join G5 G6 G7) (inner-join G6 G5 G7) (merge-join G2 G3 G8 inner-join,+1,+10) (merge-join G3 G2 G8 inner-join,+10,+1) (lookup-join G3 G8 bx,keyCols=[10],outCols=(1,2,5,6,9-12)) (merge-join G5 G6 G8 inner-join,+5,+11) (merge-join G6 G5 G8 inner-join,+11,+5) (lookup-join G6 G8 cy,keyCols=[11],outCols=(1,2,5,6,9-12))
│ └── [presentation: b:1,x:2,c:5,y:6,a:9,b:10,c:11,d:12]
│ ├── best: (lookup-join G3 G8 bx,keyCols=[10],outCols=(1,2,5,6,9-12))
│ └── cost: 17.13
├── G2: (scan bx,cols=(1,2))
│ ├── [ordering: +1]
│ │ ├── best: (scan bx,cols=(1,2))
│ │ └── cost: 1064.42
│ └── []
│ ├── best: (scan bx,cols=(1,2))
│ └── cost: 1064.42
├── G3: (inner-join G5 G9 G7) (inner-join G9 G5 G7) (merge-join G5 G9 G8 inner-join,+5,+11) (merge-join G9 G5 G8 inner-join,+11,+5) (lookup-join G9 G8 cy,keyCols=[11],outCols=(5,6,9-12))
│ └── []
│ ├── best: (lookup-join G9 G8 cy,keyCols=[11],outCols=(5,6,9-12))
│ └── cost: 11.13
├── G4: (filters G10)
├── G5: (scan cy,cols=(5,6))
│ ├── [ordering: +5]
│ │ ├── best: (scan cy,cols=(5,6))
│ │ └── cost: 1064.42
│ └── []
│ ├── best: (scan cy,cols=(5,6))
│ └── cost: 1064.42
├── G6: (inner-join G2 G9 G4) (inner-join G9 G2 G4) (merge-join G2 G9 G8 inner-join,+1,+10) (merge-join G9 G2 G8 inner-join,+10,+1) (lookup-join G9 G8 bx,keyCols=[10],outCols=(1,2,9-12))
│ └── []
│ ├── best: (lookup-join G9 G8 bx,keyCols=[10],outCols=(1,2,9-12))
│ └── cost: 11.13
├── G7: (filters G11)
├── G8: (filters)
├── G9: (select G12 G13) (scan abc,cols=(9-12),constrained)
│ └── []
│ ├── best: (scan abc,cols=(9-12),constrained)
│ └── cost: 5.09
├── G10: (eq G14 G15)
├── G11: (eq G16 G17)
├── G12: (scan abc,cols=(9-12))
│ └── []
│ ├── best: (scan abc,cols=(9-12))
│ └── cost: 1104.82
├── G13: (filters G18)
├── G14: (variable abc.b)
├── G15: (variable bx.b)
├── G16: (variable abc.c)
├── G17: (variable cy.c)
├── G18: (eq G19 G20)
├── G19: (variable a)
└── G20: (const 1)
opt join-limit=3 expect=ReorderJoins
SELECT * FROM bx, cy, dz, abc WHERE a = 1
----
inner-join (cross)
├── columns: b:1!null x:2 c:5!null y:6 d:9!null z:10 a:13!null b:14 c:15 d:16
├── key: (1,5,9)
├── fd: ()-->(13-16), (1)-->(2), (5)-->(6), (9)-->(10)
├── inner-join (cross)
│ ├── columns: cy.c:5!null y:6 dz.d:9!null z:10 a:13!null abc.b:14 abc.c:15 abc.d:16
│ ├── key: (5,9)
│ ├── fd: ()-->(13-16), (5)-->(6), (9)-->(10)
│ ├── scan cy
│ │ ├── columns: cy.c:5!null y:6
│ │ ├── key: (5)
│ │ └── fd: (5)-->(6)
│ ├── inner-join (cross)
│ │ ├── columns: dz.d:9!null z:10 a:13!null abc.b:14 abc.c:15 abc.d:16
│ │ ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
│ │ ├── key: (9)
│ │ ├── fd: ()-->(13-16), (9)-->(10)
│ │ ├── scan dz
│ │ │ ├── columns: dz.d:9!null z:10
│ │ │ ├── key: (9)
│ │ │ └── fd: (9)-->(10)
│ │ ├── scan abc
│ │ │ ├── columns: a:13!null abc.b:14 abc.c:15 abc.d:16
│ │ │ ├── constraint: /13: [/1 - /1]
│ │ │ ├── cardinality: [0 - 1]
│ │ │ ├── key: ()
│ │ │ └── fd: ()-->(13-16)
│ │ └── filters (true)
│ └── filters (true)
├── scan bx
│ ├── columns: bx.b:1!null x:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── filters (true)
opt join-limit=2 format=show-all
SELECT * FROM abc, bx, cy, dz WHERE a = 1
----
inner-join (cross)
├── columns: a:1(int!null) b:2(int) c:3(int) d:4(int) b:7(int!null) x:8(int) c:11(int!null) y:12(int) d:15(int!null) z:16(int)
├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
├── stats: [rows=1e+09]
├── cost: 32525746.2
├── key: (7,11,15)
├── fd: ()-->(1-4), (7)-->(8), (11)-->(12), (15)-->(16)
├── prune: (2-4,7,8,11,12,15,16)
├── interesting orderings: (+7) (+11) (+15)
├── inner-join (cross)
│ ├── columns: t.public.bx.b:7(int!null) t.public.bx.x:8(int) t.public.cy.c:11(int!null) t.public.cy.y:12(int) t.public.dz.d:15(int!null) t.public.dz.z:16(int)
│ ├── stats: [rows=1e+09]
│ ├── cost: 10025741.1
│ ├── key: (7,11,15)
│ ├── fd: (7)-->(8), (11)-->(12), (15)-->(16)
│ ├── prune: (7,8,11,12,15,16)
│ ├── interesting orderings: (+7) (+11) (+15)
│ ├── inner-join (cross)
│ │ ├── columns: t.public.cy.c:11(int!null) t.public.cy.y:12(int) t.public.dz.d:15(int!null) t.public.dz.z:16(int)
│ │ ├── stats: [rows=1000000]
│ │ ├── cost: 12159.0063
│ │ ├── key: (11,15)
│ │ ├── fd: (11)-->(12), (15)-->(16)
│ │ ├── prune: (11,12,15,16)
│ │ ├── interesting orderings: (+11) (+15)
│ │ ├── scan t.public.cy
│ │ │ ├── columns: t.public.cy.c:11(int!null) t.public.cy.y:12(int)
│ │ │ ├── stats: [rows=1000]
│ │ │ ├── cost: 1064.42
│ │ │ ├── key: (11)
│ │ │ ├── fd: (11)-->(12)
│ │ │ ├── prune: (11,12)
│ │ │ ├── interesting orderings: (+11)
│ │ │ └── unfiltered-cols: (11-14)
│ │ ├── scan t.public.dz
│ │ │ ├── columns: t.public.dz.d:15(int!null) t.public.dz.z:16(int)
│ │ │ ├── stats: [rows=1000]
│ │ │ ├── cost: 1064.42
│ │ │ ├── key: (15)
│ │ │ ├── fd: (15)-->(16)
│ │ │ ├── prune: (15,16)
│ │ │ ├── interesting orderings: (+15)
│ │ │ └── unfiltered-cols: (15-18)
│ │ └── filters (true)
│ ├── scan t.public.bx
│ │ ├── columns: t.public.bx.b:7(int!null) t.public.bx.x:8(int)
│ │ ├── stats: [rows=1000]
│ │ ├── cost: 1064.42
│ │ ├── key: (7)
│ │ ├── fd: (7)-->(8)
│ │ ├── prune: (7,8)
│ │ ├── interesting orderings: (+7)
│ │ └── unfiltered-cols: (7-10)
│ └── filters (true)
├── scan t.public.abc
│ ├── columns: t.public.abc.a:1(int!null) t.public.abc.b:2(int) t.public.abc.c:3(int) t.public.abc.d:4(int)
│ ├── constraint: /1: [/1 - /1]
│ ├── cardinality: [0 - 1]
│ ├── stats: [rows=1, distinct(1)=1, null(1)=0, avgsize(1)=4]
│ ├── cost: 5.09
│ ├── key: ()
│ ├── fd: ()-->(1-4)
│ └── prune: (2-4)
└── filters (true)
# Note the difference in memo size for with and without reorder-joins, for only four tables.
# TODO(justin): Find a way to reduce this.
memo join-limit=0
SELECT * FROM bx, cy, dz, abc WHERE x = y AND y = z AND z = a
----
memo (optimized, ~28KB, required=[presentation: b:1,x:2,c:5,y:6,d:9,z:10,a:13,b:14,c:15,d:16])
├── G1: (inner-join G2 G3 G4) (merge-join G2 G3 G5 inner-join,+2,+6)
│ └── [presentation: b:1,x:2,c:5,y:6,d:9,z:10,a:13,b:14,c:15,d:16]
│ ├── best: (inner-join G2 G3 G4)
│ └── cost: 5635.39
├── G2: (scan bx,cols=(1,2))
│ ├── [ordering: +2]
│ │ ├── best: (sort G2)
│ │ └── cost: 1303.90
│ └── []
│ ├── best: (scan bx,cols=(1,2))
│ └── cost: 1064.42
├── G3: (inner-join G6 G7 G8) (merge-join G6 G7 G5 inner-join,+6,+10)
│ ├── [ordering: +(6|10|13)]
│ │ ├── best: (merge-join G6="[ordering: +6]" G7="[ordering: +(10|13)]" G5 inner-join,+6,+10)
│ │ └── cost: 3860.46
│ └── []
│ ├── best: (inner-join G6 G7 G8)
│ └── cost: 3401.72
├── G4: (filters G9)
├── G5: (filters)
├── G6: (scan cy,cols=(5,6))
│ ├── [ordering: +6]
│ │ ├── best: (sort G6)
│ │ └── cost: 1303.90
│ └── []
│ ├── best: (scan cy,cols=(5,6))
│ └── cost: 1064.42
├── G7: (inner-join G10 G11 G12) (merge-join G10 G11 G5 inner-join,+10,+13) (lookup-join G10 G5 abc,keyCols=[10],outCols=(9,10,13-16))
│ ├── [ordering: +(10|13)]
│ │ ├── best: (merge-join G10="[ordering: +10]" G11="[ordering: +13]" G5 inner-join,+10,+13)
│ │ └── cost: 2438.64
│ └── []
│ ├── best: (inner-join G10 G11 G12)
│ └── cost: 2209.31
├── G8: (filters G13)
├── G9: (eq G14 G15)
├── G10: (scan dz,cols=(9,10))
│ ├── [ordering: +10]
│ │ ├── best: (sort G10)
│ │ └── cost: 1303.90
│ └── []
│ ├── best: (scan dz,cols=(9,10))
│ └── cost: 1064.42
├── G11: (scan abc,cols=(13-16))
│ ├── [ordering: +13]
│ │ ├── best: (scan abc,cols=(13-16))
│ │ └── cost: 1104.82
│ └── []
│ ├── best: (scan abc,cols=(13-16))
│ └── cost: 1104.82
├── G12: (filters G16)
├── G13: (eq G15 G17)
├── G14: (variable x)
├── G15: (variable y)
├── G16: (eq G17 G18)
├── G17: (variable z)
└── G18: (variable a)
# This query is the worst-case scenario for join ordering because all relations
# are connected, which allows every join order to be added to the memo.
# TODO(drewk): implement branch pruning and/or a max operator limit for the
# memo.
memo join-limit=3
SELECT * FROM bx, cy, dz, abc WHERE x = y AND y = z AND z = a
----
memo (optimized, ~62KB, required=[presentation: b:1,x:2,c:5,y:6,d:9,z:10,a:13,b:14,c:15,d:16])
├── G1: (inner-join G2 G3 G4) (inner-join G3 G2 G4) (inner-join G5 G6 G7) (inner-join G6 G5 G7) (inner-join G8 G9 G7) (inner-join G9 G8 G7) (inner-join G10 G11 G12) (inner-join G11 G10 G12) (inner-join G13 G14 G12) (inner-join G14 G13 G12) (inner-join G15 G16 G12) (inner-join G16 G15 G12) (inner-join G17 G18 G12) (inner-join G18 G17 G12) (merge-join G3 G2 G19 inner-join,+6,+2) (merge-join G6 G5 G19 inner-join,+10,+6) (merge-join G9 G8 G19 inner-join,+10,+6) (merge-join G11 G10 G19 inner-join,+13,+10) (merge-join G14 G13 G19 inner-join,+13,+10) (merge-join G16 G15 G19 inner-join,+13,+10) (lookup-join G17 G19 abc,keyCols=[10],outCols=(1,2,5,6,9,10,13-16)) (merge-join G18 G17 G19 inner-join,+13,+10)
│ └── [presentation: b:1,x:2,c:5,y:6,d:9,z:10,a:13,b:14,c:15,d:16]
│ ├── best: (inner-join G3 G2 G4)
│ └── cost: 5576.62
├── G2: (scan bx,cols=(1,2))
│ ├── [ordering: +2]
│ │ ├── best: (sort G2)
│ │ └── cost: 1303.90
│ └── []
│ ├── best: (scan bx,cols=(1,2))
│ └── cost: 1064.42
├── G3: (inner-join G5 G9 G7) (inner-join G9 G5 G7) (inner-join G10 G14 G12) (inner-join G14 G10 G12) (inner-join G15 G18 G12) (inner-join G18 G15 G12) (merge-join G9 G5 G19 inner-join,+10,+6) (merge-join G14 G10 G19 inner-join,+13,+10) (lookup-join G15 G19 abc,keyCols=[10],outCols=(5,6,9,10,13-16)) (merge-join G18 G15 G19 inner-join,+13,+10)
│ ├── [ordering: +(6|10|13)]
│ │ ├── best: (merge-join G9="[ordering: +(10|13)]" G5="[ordering: +6]" G19 inner-join,+10,+6)
│ │ └── cost: 3860.48
│ └── []
│ ├── best: (inner-join G5 G9 G7)
│ └── cost: 3401.72
├── G4: (filters G20)
├── G5: (scan cy,cols=(5,6))
│ ├── [ordering: +6]
│ │ ├── best: (sort G5)
│ │ └── cost: 1303.90
│ └── []
│ ├── best: (scan cy,cols=(5,6))
│ └── cost: 1064.42
├── G6: (inner-join G2 G9 G21) (inner-join G9 G2 G21) (inner-join G10 G16 G12) (inner-join G16 G10 G12) (inner-join G13 G18 G12) (inner-join G18 G13 G12) (merge-join G9 G2 G19 inner-join,+10,+2) (merge-join G16 G10 G19 inner-join,+13,+10) (lookup-join G13 G19 abc,keyCols=[10],outCols=(1,2,9,10,13-16)) (merge-join G18 G13 G19 inner-join,+13,+10)
│ ├── [ordering: +(2|10|13)]
│ │ ├── best: (merge-join G9="[ordering: +(10|13)]" G2="[ordering: +2]" G19 inner-join,+10,+2)
│ │ └── cost: 3860.48
│ └── []
│ ├── best: (inner-join G2 G9 G21)
│ └── cost: 3401.72
├── G7: (filters G22)
├── G8: (inner-join G2 G5 G4) (inner-join G5 G2 G4)
│ ├── [ordering: +(2|6)]
│ │ ├── best: (sort G8)
│ │ └── cost: 5458.98
│ └── []
│ ├── best: (inner-join G2 G5 G4)
│ └── cost: 2257.02
├── G9: (inner-join G10 G18 G12) (inner-join G18 G10 G12) (lookup-join G10 G19 abc,keyCols=[10],outCols=(9,10,13-16)) (merge-join G18 G10 G19 inner-join,+13,+10)
│ ├── [ordering: +(10|13)]
│ │ ├── best: (merge-join G18="[ordering: +13]" G10="[ordering: +10]" G19 inner-join,+13,+10)
│ │ └── cost: 2438.64
│ └── []
│ ├── best: (inner-join G10 G18 G12)
│ └── cost: 2209.31
├── G10: (scan dz,cols=(9,10))
│ ├── [ordering: +10]
│ │ ├── best: (sort G10)
│ │ └── cost: 1303.90
│ └── []
│ ├── best: (scan dz,cols=(9,10))
│ └── cost: 1064.42
├── G11: (inner-join G2 G14 G4) (inner-join G14 G2 G4) (inner-join G5 G16 G4) (inner-join G16 G5 G4) (inner-join G8 G18 G23) (inner-join G18 G8 G23) (merge-join G14 G2 G19 inner-join,+6,+2) (merge-join G16 G5 G19 inner-join,+2,+6) (lookup-join G8 G19 abc,keyCols=[2],outCols=(1,2,5,6,13-16)) (merge-join G18 G8 G19 inner-join,+13,+2)
│ ├── [ordering: +(2|6|13)]
│ │ ├── best: (merge-join G14="[ordering: +(6|13)]" G2="[ordering: +2]" G19 inner-join,+6,+2)
│ │ └── cost: 3860.48
│ └── []
│ ├── best: (inner-join G2 G14 G4)
│ └── cost: 3401.72
├── G12: (filters G24)
├── G13: (inner-join G2 G10 G21) (inner-join G10 G2 G21)
│ ├── [ordering: +(2|10)]
│ │ ├── best: (sort G13)
│ │ └── cost: 5458.98
│ └── []
│ ├── best: (inner-join G2 G10 G21)
│ └── cost: 2257.02
├── G14: (inner-join G5 G18 G25) (inner-join G18 G5 G25) (lookup-join G5 G19 abc,keyCols=[6],outCols=(5,6,13-16)) (merge-join G18 G5 G19 inner-join,+13,+6)
│ ├── [ordering: +(6|13)]
│ │ ├── best: (merge-join G18="[ordering: +13]" G5="[ordering: +6]" G19 inner-join,+13,+6)
│ │ └── cost: 2438.64
│ └── []
│ ├── best: (inner-join G5 G18 G25)
│ └── cost: 2209.31
├── G15: (inner-join G5 G10 G7) (inner-join G10 G5 G7)
│ ├── [ordering: +(6|10)]
│ │ ├── best: (sort G15)
│ │ └── cost: 5458.98
│ └── []
│ ├── best: (inner-join G5 G10 G7)
│ └── cost: 2257.02
├── G16: (inner-join G2 G18 G23) (inner-join G18 G2 G23) (lookup-join G2 G19 abc,keyCols=[2],outCols=(1,2,13-16)) (merge-join G18 G2 G19 inner-join,+13,+2)
│ ├── [ordering: +(2|13)]
│ │ ├── best: (merge-join G18="[ordering: +13]" G2="[ordering: +2]" G19 inner-join,+13,+2)
│ │ └── cost: 2438.64
│ └── []
│ ├── best: (inner-join G2 G18 G23)
│ └── cost: 2209.31
├── G17: (inner-join G2 G15 G4) (inner-join G15 G2 G4) (inner-join G5 G13 G7) (inner-join G13 G5 G7) (inner-join G8 G10 G7) (inner-join G10 G8 G7)
│ ├── [ordering: +(2|6|10)]
│ │ ├── best: (sort G17)
│ │ └── cost: 45812.65
│ └── []
│ ├── best: (inner-join G15 G2 G4)
│ └── cost: 4431.91
├── G18: (scan abc,cols=(13-16))
│ ├── [ordering: +13]
│ │ ├── best: (scan abc,cols=(13-16))
│ │ └── cost: 1104.82
│ └── []
│ ├── best: (scan abc,cols=(13-16))
│ └── cost: 1104.82
├── G19: (filters)
├── G20: (eq G26 G27)
├── G21: (filters G28)
├── G22: (eq G27 G29)
├── G23: (filters G30)
├── G24: (eq G29 G31)
├── G25: (filters G32)
├── G26: (variable x)
├── G27: (variable y)
├── G28: (eq G26 G29)
├── G29: (variable z)
├── G30: (eq G26 G31)
├── G31: (variable a)
└── G32: (eq G27 G31)
opt
SELECT * FROM bx, cy, dz, abc WHERE x = y AND y = z AND z = a
----
inner-join (hash)
├── columns: b:1!null x:2!null c:5!null y:6!null d:9!null z:10!null a:13!null b:14 c:15 d:16
├── key: (1,5,9)
├── fd: (1)-->(2), (5)-->(6), (9)-->(10), (13)-->(14-16), (10)==(2,6,13), (13)==(2,6,10), (6)==(2,10,13), (2)==(6,10,13)
├── inner-join (hash)
│ ├── columns: cy.c:5!null y:6!null dz.d:9!null z:10!null a:13!null abc.b:14 abc.c:15 abc.d:16
│ ├── key: (5,9)
│ ├── fd: (5)-->(6), (9)-->(10), (13)-->(14-16), (10)==(6,13), (13)==(6,10), (6)==(10,13)
│ ├── scan cy
│ │ ├── columns: cy.c:5!null y:6
│ │ ├── key: (5)
│ │ └── fd: (5)-->(6)
│ ├── inner-join (hash)
│ │ ├── columns: dz.d:9!null z:10!null a:13!null abc.b:14 abc.c:15 abc.d:16
│ │ ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
│ │ ├── key: (9)
│ │ ├── fd: (9)-->(10), (13)-->(14-16), (10)==(13), (13)==(10)
│ │ ├── scan dz
│ │ │ ├── columns: dz.d:9!null z:10
│ │ │ ├── key: (9)
│ │ │ └── fd: (9)-->(10)
│ │ ├── scan abc
│ │ │ ├── columns: a:13!null abc.b:14 abc.c:15 abc.d:16
│ │ │ ├── key: (13)
│ │ │ └── fd: (13)-->(14-16)
│ │ └── filters
│ │ └── z:10 = a:13 [outer=(10,13), constraints=(/10: (/NULL - ]; /13: (/NULL - ]), fd=(10)==(13), (13)==(10)]
│ └── filters
│ └── y:6 = z:10 [outer=(6,10), constraints=(/6: (/NULL - ]; /10: (/NULL - ]), fd=(6)==(10), (10)==(6)]
├── scan bx
│ ├── columns: bx.b:1!null x:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── filters
└── x:2 = y:6 [outer=(2,6), constraints=(/2: (/NULL - ]; /6: (/NULL - ]), fd=(2)==(6), (6)==(2)]
# Regression test for #34795.
exec-ddl
CREATE TABLE a (id INT8 PRIMARY KEY)
----
opt join-limit=3
SELECT
1
FROM
a as a1
INNER JOIN a as a2 ON 1 = a2.id
INNER JOIN a AS a3 ON a1.id = a3.id
CROSS JOIN a as a4
WHERE
a4.id = 1 AND (SELECT true FROM a WHERE a1.id = 1)
----
project
├── columns: "?column?":17!null
├── fd: ()-->(17)
├── inner-join (cross)
│ ├── columns: a1.id:1!null a2.id:4!null a3.id:7!null a4.id:10!null bool:16!null
│ ├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
│ ├── key: (7)
│ ├── fd: ()-->(4,10,16), (1)==(7), (7)==(1)
│ ├── inner-join (cross)
│ │ ├── columns: a1.id:1!null a2.id:4!null a3.id:7!null bool:16!null
│ │ ├── multiplicity: left-rows(zero-or-more), right-rows(zero-or-one)
│ │ ├── key: (7)
│ │ ├── fd: ()-->(4,16), (1)==(7), (7)==(1)
│ │ ├── scan a [as=a2]
│ │ │ ├── columns: a2.id:4!null
│ │ │ ├── constraint: /4: [/1 - /1]
│ │ │ ├── cardinality: [0 - 1]
│ │ │ ├── key: ()
│ │ │ └── fd: ()-->(4)
│ │ ├── inner-join (lookup a [as=a3])
│ │ │ ├── columns: a1.id:1!null a3.id:7!null bool:16!null
│ │ │ ├── key columns: [1] = [7]
│ │ │ ├── lookup columns are key
│ │ │ ├── key: (7)
│ │ │ ├── fd: ()-->(16), (1)==(7), (7)==(1)
│ │ │ ├── select
│ │ │ │ ├── columns: a1.id:1!null bool:16!null
│ │ │ │ ├── key: (1)
│ │ │ │ ├── fd: ()-->(16)
│ │ │ │ ├── ensure-distinct-on
│ │ │ │ │ ├── columns: a1.id:1!null bool:16
│ │ │ │ │ ├── grouping columns: a1.id:1!null
│ │ │ │ │ ├── error: "more than one row returned by a subquery used as an expression"
│ │ │ │ │ ├── key: (1)
│ │ │ │ │ ├── fd: (1)-->(16)
│ │ │ │ │ ├── left-join (cross)
│ │ │ │ │ │ ├── columns: a1.id:1!null bool:16
│ │ │ │ │ │ ├── scan a [as=a1]
│ │ │ │ │ │ │ ├── columns: a1.id:1!null
│ │ │ │ │ │ │ └── key: (1)
│ │ │ │ │ │ ├── project
│ │ │ │ │ │ │ ├── columns: bool:16!null
│ │ │ │ │ │ │ ├── fd: ()-->(16)
│ │ │ │ │ │ │ ├── scan a
│ │ │ │ │ │ │ └── projections
│ │ │ │ │ │ │ └── true [as=bool:16]
│ │ │ │ │ │ └── filters
│ │ │ │ │ │ └── a1.id:1 = 1 [outer=(1), constraints=(/1: [/1 - /1]; tight), fd=()-->(1)]
│ │ │ │ │ └── aggregations
│ │ │ │ │ └── const-agg [as=bool:16, outer=(16)]
│ │ │ │ │ └── bool:16
│ │ │ │ └── filters
│ │ │ │ └── bool:16 [outer=(16), constraints=(/16: [/true - /true]; tight), fd=()-->(16)]
│ │ │ └── filters (true)
│ │ └── filters (true)
│ ├── scan a [as=a4]
│ │ ├── columns: a4.id:10!null
│ │ ├── constraint: /10: [/1 - /1]
│ │ ├── cardinality: [0 - 1]
│ │ ├── key: ()
│ │ └── fd: ()-->(10)
│ └── filters (true)
└── projections
└── 1 [as="?column?":17]
# An edge for b = d should be added to the graph.
reorderjoins format=hide-all
SELECT * FROM bx
INNER JOIN cy ON b = c
INNER JOIN (SELECT * FROM dz WHERE z > 0) ON c = d
----
--------------------------------------------------------------------------------
Join Tree #1
--------------------------------------------------------------------------------
inner-join (hash)
├── scan bx
├── scan cy
└── filters
└── b = c
Vertexes
A:
scan bx
B:
scan cy
Edges
b = c [inner, ses=AB, tes=AB, rules=()]
Joining AB
A B [inner, refs=AB]
B A [inner, refs=AB]
Joins Considered: 2
--------------------------------------------------------------------------------
Join Tree #2
--------------------------------------------------------------------------------
inner-join (hash)
├── inner-join (hash)
│ ├── scan bx
│ ├── scan cy
│ └── filters
│ └── b = c
├── select
│ ├── scan dz
│ └── filters
│ └── z > 0
└── filters
└── c = d
Vertexes
A:
scan bx
B:
scan cy
C:
select
├── scan dz
└── filters
└── z > 0
Edges
b = c [inner, ses=AB, tes=AB, rules=()]
c = d [inner, ses=BC, tes=BC, rules=()]
b = d [inner, ses=AC, tes=AC, rules=()]
Joining AB
A B [inner, refs=AB]
B A [inner, refs=AB]
Joining AC
A C [inner, refs=AC]
C A [inner, refs=AC]
Joining BC
B C [inner, refs=BC]
C B [inner, refs=BC]
Joining ABC
A BC [inner, refs=AB]
BC A [inner, refs=AB]
B AC [inner, refs=AB]
AC B [inner, refs=AB]
AB C [inner, refs=BC]
C AB [inner, refs=BC]
Joins Considered: 12
================================================================================
Final Plan
================================================================================
inner-join (merge)
├── scan bx
├── inner-join (merge)
│ ├── scan cy
│ ├── select
│ │ ├── scan dz
│ │ └── filters
│ │ └── z > 0
│ └── filters (true)
└── filters (true)
reorderjoins format=hide-all
SELECT * FROM bx
INNER JOIN cy ON b = c
INNER JOIN (SELECT max(z) AS m FROM dz) ON y = m
INNER JOIN abc ON m = a
----
--------------------------------------------------------------------------------
Join Tree #1
--------------------------------------------------------------------------------
inner-join (hash)
├── scan bx
├── scan cy
└── filters
└── bx.b = cy.c
Vertexes
A:
scan bx
B:
scan cy
Edges
bx.b = cy.c [inner, ses=AB, tes=AB, rules=()]
Joining AB
A B [inner, refs=AB]
B A [inner, refs=AB]
Joins Considered: 2
--------------------------------------------------------------------------------
Join Tree #2
--------------------------------------------------------------------------------
inner-join (hash)
├── inner-join (hash)
│ ├── scan bx
│ ├── scan cy
│ └── filters
│ └── bx.b = cy.c
├── scalar-group-by
│ ├── scan dz
│ └── aggregations
│ └── max
│ └── z
└── filters
└── y = max
Vertexes
A:
scan bx
B:
scan cy
C:
scalar-group-by
├── scan dz
└── aggregations
└── max
└── z
Edges
bx.b = cy.c [inner, ses=AB, tes=AB, rules=()]
y = max [inner, ses=BC, tes=BC, rules=()]
Joining AB
A B [inner, refs=AB]
B A [inner, refs=AB]
Joining BC
B C [inner, refs=BC]
C B [inner, refs=BC]
Joining ABC
A BC [inner, refs=AB]
BC A [inner, refs=AB]
AB C [inner, refs=BC]
C AB [inner, refs=BC]
Joins Considered: 8
--------------------------------------------------------------------------------
Join Tree #3
--------------------------------------------------------------------------------
inner-join (hash)
├── inner-join (hash)
│ ├── inner-join (hash)
│ │ ├── scan bx
│ │ ├── scan cy
│ │ └── filters
│ │ └── bx.b = cy.c
│ ├── scalar-group-by
│ │ ├── scan dz
│ │ └── aggregations
│ │ └── max
│ │ └── z
│ └── filters
│ └── y = max
├── scan abc