Skip to content

Commit 51cb84f

Browse files
eurekakazz-jason
authored andcommitted
planner: correct estimated row count for inner plan of index join (#10015)
1 parent e965367 commit 51cb84f

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

cmd/explaintest/r/explain_easy.result

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ explain select count(b.c2) from t1 a, t2 b where a.c1 = b.c2 group by a.c1;
6161
id count task operator info
6262
Projection_11 9990.00 root cast(join_agg_0)
6363
└─IndexJoin_14 9990.00 root inner join, inner:TableReader_13, outer key:b.c2, inner key:a.c1
64-
├─TableReader_13 10.00 root data:TableScan_12
65-
│ └─TableScan_12 10.00 cop table:a, range: decided by [b.c2], keep order:false, stats:pseudo
64+
├─TableReader_13 1.00 root data:TableScan_12
65+
│ └─TableScan_12 1.00 cop table:a, range: decided by [b.c2], keep order:false, stats:pseudo
6666
└─HashAgg_22 7992.00 root group by:col_2, funcs:count(col_0), firstrow(col_1)
6767
└─TableReader_23 7992.00 root data:HashAgg_17
6868
└─HashAgg_17 7992.00 cop group by:b.c2, funcs:count(b.c2), firstrow(b.c2)
@@ -99,8 +99,8 @@ explain select c1 from t1 where c1 in (select c2 from t2);
9999
id count task operator info
100100
Projection_9 9990.00 root test.t1.c1
101101
└─IndexJoin_12 9990.00 root inner join, inner:TableReader_11, outer key:test.t2.c2, inner key:test.t1.c1
102-
├─TableReader_11 10.00 root data:TableScan_10
103-
│ └─TableScan_10 10.00 cop table:t1, range: decided by [test.t2.c2], keep order:false, stats:pseudo
102+
├─TableReader_11 1.00 root data:TableScan_10
103+
│ └─TableScan_10 1.00 cop table:t1, range: decided by [test.t2.c2], keep order:false, stats:pseudo
104104
└─HashAgg_20 7992.00 root group by:col_1, funcs:firstrow(col_0)
105105
└─TableReader_21 7992.00 root data:HashAgg_15
106106
└─HashAgg_15 7992.00 cop group by:test.t2.c2, funcs:firstrow(test.t2.c2)
@@ -291,9 +291,9 @@ Projection_11 10000.00 root 9_aux_0
291291
└─IndexJoin_44 12.50 root inner join, inner:TableReader_43, outer key:s.a, inner key:t1.a
292292
├─TableReader_37 1.00 root data:TableScan_36
293293
│ └─TableScan_36 1.00 cop table:s, range: decided by [eq(s.a, test.t.a)], keep order:false, stats:pseudo
294-
└─TableReader_43 10.00 root data:Selection_42
295-
└─Selection_42 10.00 cop eq(t1.a, test.t.a)
296-
└─TableScan_41 10.00 cop table:t1, range: decided by [s.a], keep order:false, stats:pseudo
294+
└─TableReader_43 0.80 root data:Selection_42
295+
└─Selection_42 0.80 cop eq(t1.a, test.t.a)
296+
└─TableScan_41 1.00 cop table:t1, range: decided by [s.a], keep order:false, stats:pseudo
297297
explain select t.c in (select count(*) from t s use index(idx), t t1 where s.b = t.a and s.a = t1.a) from t;
298298
id count task operator info
299299
Projection_11 10000.00 root 9_aux_0
@@ -304,8 +304,8 @@ Projection_11 10000.00 root 9_aux_0
304304
└─IndexJoin_32 12.50 root inner join, inner:TableReader_31, outer key:s.a, inner key:t1.a
305305
├─IndexReader_27 10.00 root index:IndexScan_26
306306
│ └─IndexScan_26 10.00 cop table:s, index:b, range: decided by [eq(s.b, test.t.a)], keep order:false, stats:pseudo
307-
└─TableReader_31 10.00 root data:TableScan_30
308-
└─TableScan_30 10.00 cop table:t1, range: decided by [s.a], keep order:false, stats:pseudo
307+
└─TableReader_31 1.00 root data:TableScan_30
308+
└─TableScan_30 1.00 cop table:t1, range: decided by [s.a], keep order:false, stats:pseudo
309309
explain select t.c in (select count(*) from t s use index(idx), t t1 where s.b = t.a and s.c = t1.a) from t;
310310
id count task operator info
311311
Projection_11 10000.00 root 9_aux_0
@@ -318,8 +318,8 @@ Projection_11 10000.00 root 9_aux_0
318318
│ ├─IndexScan_26 10.00 cop table:s, index:b, range: decided by [eq(s.b, test.t.a)], keep order:false, stats:pseudo
319319
│ └─Selection_28 9.99 cop not(isnull(s.c))
320320
│ └─TableScan_27 10.00 cop table:t, keep order:false, stats:pseudo
321-
└─TableReader_33 10.00 root data:TableScan_32
322-
└─TableScan_32 10.00 cop table:t1, range: decided by [s.c], keep order:false, stats:pseudo
321+
└─TableReader_33 1.00 root data:TableScan_32
322+
└─TableScan_32 1.00 cop table:t1, range: decided by [s.c], keep order:false, stats:pseudo
323323
drop table if exists t;
324324
create table t(a int unsigned);
325325
explain select t.a = '123455' from t;

cmd/explaintest/r/tpch.result

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ Sort_23 5.00 root revenue:desc
360360
│ │ │ └─TableScan_56 500000.00 cop table:supplier, range:[-inf,+inf], keep order:false
361361
│ │ └─TableReader_59 300005811.00 root data:TableScan_58
362362
│ │ └─TableScan_58 300005811.00 cop table:lineitem, range:[-inf,+inf], keep order:false
363-
│ └─TableReader_37 11822812.50 root data:Selection_36
364-
│ └─Selection_36 11822812.50 cop ge(tpch.orders.o_orderdate, 1994-01-01 00:00:00.000000), lt(tpch.orders.o_orderdate, 1995-01-01)
363+
│ └─TableReader_37 0.80 root data:Selection_36
364+
│ └─Selection_36 0.80 cop ge(tpch.orders.o_orderdate, 1994-01-01 00:00:00.000000), lt(tpch.orders.o_orderdate, 1995-01-01)
365365
│ └─TableScan_35 1.00 cop table:orders, range: decided by [tpch.lineitem.l_orderkey], keep order:false
366366
└─TableReader_30 1.00 root data:TableScan_29
367367
└─TableScan_29 1.00 cop table:customer, range: decided by [tpch.supplier.s_nationkey tpch.orders.o_custkey], keep order:false
@@ -1096,8 +1096,8 @@ StreamAgg_13 1.00 root funcs:sum(col_0)
10961096
├─TableReader_22 6286493.79 root data:Selection_21
10971097
│ └─Selection_21 6286493.79 cop eq(tpch.lineitem.l_shipinstruct, "DELIVER IN PERSON"), in(tpch.lineitem.l_shipmode, "AIR", "AIR REG"), or(and(ge(tpch.lineitem.l_quantity, 4), le(tpch.lineitem.l_quantity, 14)), or(and(ge(tpch.lineitem.l_quantity, 18), le(tpch.lineitem.l_quantity, 28)), and(ge(tpch.lineitem.l_quantity, 29), le(tpch.lineitem.l_quantity, 39))))
10981098
│ └─TableScan_20 300005811.00 cop table:lineitem, range:[-inf,+inf], keep order:false
1099-
└─TableReader_28 8000000.00 root data:Selection_27
1100-
└─Selection_27 8000000.00 cop ge(tpch.part.p_size, 1), or(and(eq(tpch.part.p_brand, "Brand#52"), and(in(tpch.part.p_container, "SM CASE", "SM BOX", "SM PACK", "SM PKG"), le(tpch.part.p_size, 5))), or(and(eq(tpch.part.p_brand, "Brand#11"), and(in(tpch.part.p_container, "MED BAG", "MED BOX", "MED PKG", "MED PACK"), le(tpch.part.p_size, 10))), and(eq(tpch.part.p_brand, "Brand#51"), and(in(tpch.part.p_container, "LG CASE", "LG BOX", "LG PACK", "LG PKG"), le(tpch.part.p_size, 15)))))
1099+
└─TableReader_28 0.80 root data:Selection_27
1100+
└─Selection_27 0.80 cop ge(tpch.part.p_size, 1), or(and(eq(tpch.part.p_brand, "Brand#52"), and(in(tpch.part.p_container, "SM CASE", "SM BOX", "SM PACK", "SM PKG"), le(tpch.part.p_size, 5))), or(and(eq(tpch.part.p_brand, "Brand#11"), and(in(tpch.part.p_container, "MED BAG", "MED BOX", "MED PKG", "MED PACK"), le(tpch.part.p_size, 10))), and(eq(tpch.part.p_brand, "Brand#51"), and(in(tpch.part.p_container, "LG CASE", "LG BOX", "LG PACK", "LG PKG"), le(tpch.part.p_size, 15)))))
11011101
└─TableScan_26 1.00 cop table:part, range: decided by [tpch.lineitem.l_partkey], keep order:false
11021102
/*
11031103
Q20 Potential Part Promotion Query
@@ -1235,8 +1235,8 @@ Projection_25 1.00 root tpch.supplier.s_name, 17_col_0
12351235
│ │ │ └─TableReader_80 240004648.80 root data:Selection_79
12361236
│ │ │ └─Selection_79 240004648.80 cop gt(l1.l_receiptdate, l1.l_commitdate)
12371237
│ │ │ └─TableScan_78 300005811.00 cop table:l1, range:[-inf,+inf], keep order:false
1238-
│ │ └─TableReader_61 36517371.00 root data:Selection_60
1239-
│ │ └─Selection_60 36517371.00 cop eq(tpch.orders.o_orderstatus, "F")
1238+
│ │ └─TableReader_61 0.80 root data:Selection_60
1239+
│ │ └─Selection_60 0.80 cop eq(tpch.orders.o_orderstatus, "F")
12401240
│ │ └─TableScan_59 1.00 cop table:orders, range: decided by [l1.l_orderkey], keep order:false
12411241
│ └─IndexLookUp_55 1.00 root
12421242
│ ├─IndexScan_53 1.00 cop table:l2, index:L_ORDERKEY, L_LINENUMBER, range: decided by [l1.l_orderkey], keep order:false

executor/index_lookup_join_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func (s *testSuite1) TestIndexJoinUnionScan(c *C) {
5353
"│ └─TableReader_15 9990.00 root data:Selection_14",
5454
"│ └─Selection_14 9990.00 cop not(isnull(test.t1.a))",
5555
"│ └─TableScan_13 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
56-
"└─UnionScan_10 10.00 root ",
57-
" └─TableReader_9 10.00 root data:TableScan_8",
58-
" └─TableScan_8 10.00 cop table:t2, range: decided by [test.t1.a], keep order:false, stats:pseudo",
56+
"└─UnionScan_10 1.00 root ",
57+
" └─TableReader_9 1.00 root data:TableScan_8",
58+
" └─TableScan_8 1.00 cop table:t2, range: decided by [test.t1.a], keep order:false, stats:pseudo",
5959
))
6060
tk.MustQuery("select /*+ TIDB_INLJ(t1, t2)*/ * from t1 join t2 on t1.a = t2.id").Check(testkit.Rows(
6161
"2 2 2 2 2",

planner/core/exhaust_physical_plans.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -461,22 +461,15 @@ func (p *LogicalJoin) constructInnerTableScan(ds *DataSource, pk *expression.Col
461461
}.Init(ds.ctx)
462462
ts.SetSchema(ds.schema)
463463

464-
var rowCount float64
465-
pkHist, ok := ds.statisticTable.Columns[pk.ID]
466-
if ok && !ds.statisticTable.Pseudo {
467-
rowCount = pkHist.AvgCountPerNotNullValue(ds.statisticTable.Count)
468-
} else {
469-
rowCount = ds.statisticTable.PseudoAvgCountPerValue()
470-
}
471-
472-
ts.stats = property.NewSimpleStats(rowCount)
464+
ts.stats = property.NewSimpleStats(1)
473465
ts.stats.UsePseudoStats = ds.statisticTable.Pseudo
474466

475467
copTask := &copTask{
476468
tablePlan: ts,
477469
indexPlanFinished: true,
478470
}
479-
ts.addPushedDownSelection(copTask, ds.stats)
471+
selStats := ts.stats.Scale(selectionFactor)
472+
ts.addPushedDownSelection(copTask, selStats)
480473
t := finishCopTask(ds.ctx, copTask)
481474
reader := t.plan()
482475
return p.constructInnerUnionScan(us, reader)

0 commit comments

Comments
 (0)