Skip to content

Commit

Permalink
opt: adjust cost of scan with unbounded cardinality to avoid bad plans
Browse files Browse the repository at this point in the history
This commit tweaks the application of the unbounded cardinality penalty
in the coster to add it directly to the cost of scans and zigzag joins
rather than to the row count. This helps prevent an issue in which the
number of index columns could dominate the scan cost and result in
suboptimal plans.

Fixes #68556

Release note (bug fix): Fixed a regression in the optimizer's cost model
that could cause it to choose suboptimal plans when choosing between two
non-unique index scans with different numbers of columns per index.
  • Loading branch information
rytaft committed Aug 11, 2021
1 parent f673e97 commit 9a3d1ab
Show file tree
Hide file tree
Showing 45 changed files with 1,293 additions and 1,209 deletions.
2 changes: 1 addition & 1 deletion pkg/ccl/logictestccl/testdata/logic_test/as_of
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ memo (optimized, ~7KB, required=[presentation: info:6])
├── G3: (scan t,cols=(1-3)) (scan t@t_k_key,cols=(1-3))
│ └── []
│ ├── best: (scan t,cols=(1-3))
│ └── cost: 1146.41
│ └── cost: 1145.21
├── G4: (filters G7 G8)
├── G5: (scan t,cols=(1-3),constrained)
│ └── []
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/prepare
Original file line number Diff line number Diff line change
Expand Up @@ -1171,14 +1171,14 @@ select
├── columns: k:1 str:2
├── immutable
├── stats: [rows=333.333333]
├── cost: 1074.83
├── cost: 1074.43
├── key: (1)
├── fd: (1)-->(2)
├── prune: (2)
├── scan t2
│ ├── columns: k:1 str:2
│ ├── stats: [rows=1000]
│ ├── cost: 1064.81
│ ├── cost: 1064.41
│ ├── key: (1)
│ ├── fd: (1)-->(2)
│ ├── prune: (1,2)
Expand Down
10 changes: 5 additions & 5 deletions pkg/sql/logictest/testdata/logic_test/vectorize_local
Original file line number Diff line number Diff line change
Expand Up @@ -104,35 +104,35 @@ EXPLAIN (OPT, VERBOSE) SELECT c.a FROM c INNER MERGE JOIN d ON c.a = d.b
project
├── columns: a:1
├── stats: [rows=10]
├── cost: 1122.17
├── cost: 1120.77
├── prune: (1)
└── inner-join (merge)
├── columns: c.a:1 d.b:8
├── flags: force merge join
├── left ordering: +1
├── right ordering: +8
├── stats: [rows=10, distinct(1)=1, null(1)=0, distinct(8)=1, null(8)=0]
├── cost: 1122.06
├── cost: 1120.66
├── fd: (1)==(8), (8)==(1)
├── sort
│ ├── columns: c.a:1
│ ├── stats: [rows=1, distinct(1)=1, null(1)=0]
│ ├── cost: 26.73
│ ├── cost: 25.93
│ ├── ordering: +1
│ ├── prune: (1)
│ ├── interesting orderings: (+1)
│ ├── unfiltered-cols: (1-6)
│ └── scan c@sec
│ ├── columns: c.a:1
│ ├── stats: [rows=1, distinct(1)=1, null(1)=0]
│ ├── cost: 26.69
│ ├── cost: 25.89
│ ├── prune: (1)
│ ├── interesting orderings: (+1)
│ └── unfiltered-cols: (1-6)
├── scan d
│ ├── columns: d.b:8
│ ├── stats: [rows=1000, distinct(8)=100, null(8)=0]
│ ├── cost: 1085.21
│ ├── cost: 1084.61
│ ├── ordering: +8
│ ├── prune: (8)
│ ├── interesting orderings: (+8)
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/exec/execbuilder/testdata/distsql_agg
Original file line number Diff line number Diff line change
Expand Up @@ -963,15 +963,15 @@ group-by
├── grouping columns: b:2
├── internal-ordering: +2 opt(1)
├── stats: [rows=9.5617925, distinct(2)=9.5617925, null(2)=0]
├── cost: 25.9256179
├── cost: 25.1256179
├── key: (2)
├── fd: (2)-->(5)
├── prune: (5)
├── scan data2
│ ├── columns: a:1 b:2
│ ├── constraint: /1/2: [/1 - /1]
│ ├── stats: [rows=10, distinct(1)=1, null(1)=0, distinct(2)=9.5617925, null(2)=0]
│ ├── cost: 25.61
│ ├── cost: 24.81
│ ├── key: (2)
│ ├── fd: ()-->(1)
│ ├── ordering: +2 opt(1) [actual: +2]
Expand Down
18 changes: 9 additions & 9 deletions pkg/sql/opt/exec/execbuilder/testdata/enums
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ distinct-on
├── internal-ordering: +1
├── cardinality: [0 - 4]
├── stats: [rows=4, distinct(1)=4, null(1)=0]
├── cost: 1115.67
├── cost: 1114.87
├── key: (1)
└── scan checks@checks_x_y_idx
├── columns: x:1
├── stats: [rows=1000, distinct(1)=4, null(1)=0]
├── cost: 1105.61
├── cost: 1104.81
├── ordering: +1
├── prune: (1)
└── interesting orderings: (+1)
Expand Down Expand Up @@ -170,25 +170,25 @@ union
├── internal-ordering: +11
├── cardinality: [0 - 5]
├── stats: [rows=5, distinct(11)=5, null(11)=1]
├── cost: 2278.80667
├── cost: 2276.80667
├── key: (11)
├── interesting orderings: (+11)
├── project
│ ├── columns: nulls.x:1
│ ├── stats: [rows=333.333333, distinct(1)=5, null(1)=3.33333333]
│ ├── cost: 1139.37333
│ ├── cost: 1138.37333
│ ├── ordering: +1
│ ├── interesting orderings: (+1)
│ └── select
│ ├── columns: nulls.x:1 y:2
│ ├── stats: [rows=333.333333, distinct(1)=5, null(1)=3.33333333, distinct(2)=33.3333333, null(2)=0]
│ ├── cost: 1136.03
│ ├── cost: 1135.03
│ ├── ordering: +1
│ ├── interesting orderings: (+1,+2)
│ ├── scan nulls@nulls_x_y_idx
│ │ ├── columns: nulls.x:1 y:2
│ │ ├── stats: [rows=1000, distinct(1)=5, null(1)=10, distinct(2)=100, null(2)=10]
│ │ ├── cost: 1126.01
│ │ ├── cost: 1125.01
│ │ ├── ordering: +1
│ │ ├── prune: (1,2)
│ │ └── interesting orderings: (+1,+2)
Expand All @@ -197,19 +197,19 @@ union
└── project
├── columns: nulls.x:6
├── stats: [rows=333.333333, distinct(6)=5, null(6)=3.33333333]
├── cost: 1139.37333
├── cost: 1138.37333
├── ordering: +6
├── interesting orderings: (+6)
└── select
├── columns: nulls.x:6 y:7
├── stats: [rows=333.333333, distinct(6)=5, null(6)=3.33333333, distinct(7)=33.3333333, null(7)=0]
├── cost: 1136.03
├── cost: 1135.03
├── ordering: +6
├── interesting orderings: (+6,+7)
├── scan nulls@nulls_x_y_idx
│ ├── columns: nulls.x:6 y:7
│ ├── stats: [rows=1000, distinct(6)=5, null(6)=10, distinct(7)=100, null(7)=10]
│ ├── cost: 1126.01
│ ├── cost: 1125.01
│ ├── ordering: +6
│ ├── prune: (6,7)
│ └── interesting orderings: (+6,+7)
Expand Down
68 changes: 34 additions & 34 deletions pkg/sql/opt/exec/execbuilder/testdata/explain
Original file line number Diff line number Diff line change
Expand Up @@ -1097,21 +1097,21 @@ EXPLAIN (OPT,VERBOSE) SELECT * FROM tc WHERE a = 10 ORDER BY b
sort
├── columns: a:1 b:2
├── stats: [rows=10, distinct(1)=1, null(1)=0]
├── cost: 87.5943856
├── cost: 86.7943856
├── fd: ()-->(1)
├── ordering: +2 opt(1) [actual: +2]
├── prune: (2)
└── index-join tc
├── columns: a:1 b:2
├── stats: [rows=10, distinct(1)=1, null(1)=0]
├── cost: 86.52
├── cost: 85.72
├── fd: ()-->(1)
├── prune: (2)
└── scan tc@c
├── columns: a:1 rowid:3
├── constraint: /1/3: [/10 - /10]
├── stats: [rows=10, distinct(1)=1, null(1)=0]
├── cost: 25.61
├── cost: 24.81
├── key: (3)
└── fd: ()-->(1)

Expand All @@ -1121,21 +1121,21 @@ EXPLAIN (OPT,TYPES) SELECT * FROM tc WHERE a = 10 ORDER BY b
sort
├── columns: a:1(int!null) b:2(int)
├── stats: [rows=10, distinct(1)=1, null(1)=0]
├── cost: 87.5943856
├── cost: 86.7943856
├── fd: ()-->(1)
├── ordering: +2 opt(1) [actual: +2]
├── prune: (2)
└── index-join tc
├── columns: a:1(int!null) b:2(int)
├── stats: [rows=10, distinct(1)=1, null(1)=0]
├── cost: 86.52
├── cost: 85.72
├── fd: ()-->(1)
├── prune: (2)
└── scan tc@c
├── columns: a:1(int!null) rowid:3(int!null)
├── constraint: /1/3: [/10 - /10]
├── stats: [rows=10, distinct(1)=1, null(1)=0]
├── cost: 25.61
├── cost: 24.81
├── key: (3)
└── fd: ()-->(1)

Expand Down Expand Up @@ -1183,20 +1183,20 @@ inner-join (hash)
├── columns: a:1 b:2 k:6 v:7
├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
├── stats: [rows=990, distinct(1)=99, null(1)=0, distinct(6)=99, null(6)=0]
├── cost: 2271.67625
├── cost: 2269.87625
├── fd: (6)-->(7), (1)==(6), (6)==(1)
├── prune: (2,7)
├── scan tc
│ ├── columns: a:1 b:2
│ ├── stats: [rows=1000, distinct(1)=100, null(1)=10]
│ ├── cost: 1126.01
│ ├── cost: 1125.01
│ ├── prune: (1,2)
│ ├── interesting orderings: (+1)
│ └── unfiltered-cols: (1-5)
├── scan t
│ ├── columns: k:6 v:7
│ ├── stats: [rows=1000, distinct(6)=1000, null(6)=0]
│ ├── cost: 1105.61
│ ├── cost: 1104.81
│ ├── key: (6)
│ ├── fd: (6)-->(7)
│ ├── prune: (6,7)
Expand Down Expand Up @@ -1224,7 +1224,7 @@ sort
├── columns: a:1 b:2 [hidden: column6:6]
├── immutable
├── stats: [rows=333.333333]
├── cost: 1215.26951
├── cost: 1214.26951
├── fd: (1,2)-->(6)
├── ordering: +6
├── prune: (1,2,6)
Expand All @@ -1233,20 +1233,20 @@ sort
├── columns: column6:6 a:1 b:2
├── immutable
├── stats: [rows=333.333333]
├── cost: 1142.70667
├── cost: 1141.70667
├── fd: (1,2)-->(6)
├── prune: (1,2,6)
├── interesting orderings: (+1)
├── select
│ ├── columns: a:1 b:2
│ ├── immutable
│ ├── stats: [rows=333.333333]
│ ├── cost: 1136.03
│ ├── cost: 1135.03
│ ├── interesting orderings: (+1)
│ ├── scan tc
│ │ ├── columns: a:1 b:2
│ │ ├── stats: [rows=1000]
│ │ ├── cost: 1126.01
│ │ ├── cost: 1125.01
│ │ ├── prune: (1,2)
│ │ └── interesting orderings: (+1)
│ └── filters
Expand All @@ -1261,7 +1261,7 @@ sort
├── columns: a:1(int) b:2(int) [hidden: column6:6(int)]
├── immutable
├── stats: [rows=333.333333]
├── cost: 1215.26951
├── cost: 1214.26951
├── fd: (1,2)-->(6)
├── ordering: +6
├── prune: (1,2,6)
Expand All @@ -1270,20 +1270,20 @@ sort
├── columns: column6:6(int) a:1(int) b:2(int)
├── immutable
├── stats: [rows=333.333333]
├── cost: 1142.70667
├── cost: 1141.70667
├── fd: (1,2)-->(6)
├── prune: (1,2,6)
├── interesting orderings: (+1)
├── select
│ ├── columns: a:1(int) b:2(int)
│ ├── immutable
│ ├── stats: [rows=333.333333]
│ ├── cost: 1136.03
│ ├── cost: 1135.03
│ ├── interesting orderings: (+1)
│ ├── scan tc
│ │ ├── columns: a:1(int) b:2(int)
│ │ ├── stats: [rows=1000]
│ │ ├── cost: 1126.01
│ │ ├── cost: 1125.01
│ │ ├── prune: (1,2)
│ │ └── interesting orderings: (+1)
│ └── filters
Expand Down Expand Up @@ -1598,31 +1598,31 @@ memo (optimized, ~14KB, required=[presentation: info:10])
├── G1: (explain G2 [presentation: a:1,b:2,k:6,v:7])
│ └── [presentation: info:10]
│ ├── best: (explain G2="[presentation: a:1,b:2,k:6,v:7]" [presentation: a:1,b:2,k:6,v:7])
│ └── cost: 2271.69
│ └── cost: 2269.89
├── G2: (inner-join G3 G4 G5) (inner-join G4 G3 G5) (merge-join G3 G4 G6 inner-join,+1,+6) (lookup-join G3 G6 t,keyCols=[1],outCols=(1,2,6,7)) (merge-join G4 G3 G6 inner-join,+6,+1) (lookup-join G7 G6 tc,keyCols=[3],outCols=(1,2,6,7))
│ └── [presentation: a:1,b:2,k:6,v:7]
│ ├── best: (inner-join G3 G4 G5)
│ └── cost: 2271.68
│ └── cost: 2269.88
├── G3: (scan tc,cols=(1,2))
│ ├── [ordering: +1]
│ │ ├── best: (sort G3)
│ │ └── cost: 1365.48
│ │ └── cost: 1364.48
│ └── []
│ ├── best: (scan tc,cols=(1,2))
│ └── cost: 1126.01
│ └── cost: 1125.01
├── G4: (scan t,cols=(6,7))
│ ├── [ordering: +6]
│ │ ├── best: (scan t,cols=(6,7))
│ │ └── cost: 1105.61
│ │ └── cost: 1104.81
│ └── []
│ ├── best: (scan t,cols=(6,7))
│ └── cost: 1105.61
│ └── cost: 1104.81
├── G5: (filters G8)
├── G6: (filters)
├── G7: (lookup-join G4 G6 tc@c,keyCols=[6],outCols=(1,3,6,7))
│ └── []
│ ├── best: (lookup-join G4 G6 tc@c,keyCols=[6],outCols=(1,3,6,7))
│ └── cost: 23174.72
│ └── cost: 23173.92
├── G8: (eq G9 G10)
├── G9: (variable k)
└── G10: (variable a)
Expand Down Expand Up @@ -1657,52 +1657,52 @@ memo (optimized, ~14KB, required=[presentation: info:10])
├── G1: (explain G2 [presentation: a:1,b:2,k:6,v:7])
│ └── [presentation: info:10]
│ ├── best: (explain G2="[presentation: a:1,b:2,k:6,v:7]" [presentation: a:1,b:2,k:6,v:7])
│ └── cost: 2271.69
│ └── cost: 2269.89
├── G2: (inner-join G3 G4 G5) (inner-join G4 G3 G5) (merge-join G3 G4 G6 inner-join,+1,+6) (lookup-join G3 G6 t,keyCols=[1],outCols=(1,2,6,7)) (merge-join G4 G3 G6 inner-join,+6,+1) (lookup-join G7 G6 tc,keyCols=[3],outCols=(1,2,6,7))
│ └── [presentation: a:1,b:2,k:6,v:7]
│ ├── best: (inner-join G3 G4 G5)
│ └── cost: 2271.68
│ └── cost: 2269.88
├── G3: (scan tc,cols=(1,2))
│ ├── [ordering: +1]
│ │ ├── best: (sort G3)
│ │ └── cost: 1365.48
│ │ └── cost: 1364.48
│ └── []
│ ├── best: (scan tc,cols=(1,2))
│ └── cost: 1126.01
│ └── cost: 1125.01
├── G4: (scan t,cols=(6,7))
│ ├── [ordering: +6]
│ │ ├── best: (scan t,cols=(6,7))
│ │ └── cost: 1105.61
│ │ └── cost: 1104.81
│ └── []
│ ├── best: (scan t,cols=(6,7))
│ └── cost: 1105.61
│ └── cost: 1104.81
├── G5: (filters G8)
├── G6: (filters)
├── G7: (lookup-join G4 G6 tc@c,keyCols=[6],outCols=(1,3,6,7))
│ └── []
│ ├── best: (lookup-join G4 G6 tc@c,keyCols=[6],outCols=(1,3,6,7))
│ └── cost: 23174.72
│ └── cost: 23173.92
├── G8: (eq G9 G10)
├── G9: (variable k)
└── G10: (variable a)
inner-join (hash)
├── columns: a:1 b:2 k:6 v:7
├── multiplicity: left-rows(zero-or-one), right-rows(zero-or-more)
├── stats: [rows=990, distinct(1)=99, null(1)=0, distinct(6)=99, null(6)=0]
├── cost: 2271.67625
├── cost: 2269.87625
├── fd: (6)-->(7), (1)==(6), (6)==(1)
├── prune: (2,7)
├── scan tc
│ ├── columns: a:1 b:2
│ ├── stats: [rows=1000, distinct(1)=100, null(1)=10]
│ ├── cost: 1126.01
│ ├── cost: 1125.01
│ ├── prune: (1,2)
│ ├── interesting orderings: (+1)
│ └── unfiltered-cols: (1-5)
├── scan t
│ ├── columns: k:6 v:7
│ ├── stats: [rows=1000, distinct(6)=1000, null(6)=0]
│ ├── cost: 1105.61
│ ├── cost: 1104.81
│ ├── key: (6)
│ ├── fd: (6)-->(7)
│ ├── prune: (6,7)
Expand Down
Loading

0 comments on commit 9a3d1ab

Please sign in to comment.