Skip to content

Commit 4238d74

Browse files
eurekakazz-jason
authored andcommitted
planner: fix wrong selectivity for inner selection in index join (#10633) (#10854)
1 parent 73f580f commit 4238d74

13 files changed

+87
-76
lines changed

cmd/explaintest/r/explain_complex.result

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ Projection_10 0.00 root test.dt.id, test.dt.aid, test.dt.pt, test.dt.dic, test.d
156156
├─TableReader_41 0.00 root data:Selection_40
157157
│ └─Selection_40 0.00 cop eq(test.dt.bm, 0), eq(test.dt.pt, "ios"), gt(test.dt.t, 1478185592), not(isnull(test.dt.dic))
158158
│ └─TableScan_39 10000.00 cop table:dt, range:[0,+inf], keep order:false, stats:pseudo
159-
└─IndexLookUp_18 3.33 root
159+
└─IndexLookUp_18 0.00 root
160160
├─IndexScan_15 10.00 cop table:rr, index:aid, dic, range: decided by [eq(test.rr.aid, test.dt.aid) eq(test.rr.dic, test.dt.dic)], keep order:false, stats:pseudo
161-
└─Selection_17 3.33 cop eq(test.rr.pt, "ios"), gt(test.rr.t, 1478185592)
161+
└─Selection_17 0.00 cop eq(test.rr.pt, "ios"), gt(test.rr.t, 1478185592)
162162
└─TableScan_16 10.00 cop table:rr, keep order:false, stats:pseudo
163163
explain select pc,cr,count(DISTINCT uid) as pay_users,count(oid) as pay_times,sum(am) as am from pp where ps=2 and ppt>=1478188800 and ppt<1478275200 and pi in ('510017','520017') and uid in ('18089709','18090780') group by pc,cr;
164164
id count task operator info

cmd/explaintest/r/explain_complex_stats.result

+6-6
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ Projection_13 424.00 root test.gad.id, test.dd.id, test.gad.aid, test.gad.cm, te
132132
├─TableReader_29 424.00 root data:Selection_28
133133
│ └─Selection_28 424.00 cop eq(test.gad.bm, 0), eq(test.gad.pt, "android"), gt(test.gad.t, 1478143908), not(isnull(test.gad.ip))
134134
│ └─TableScan_27 1999.00 cop table:gad, range:[0,+inf], keep order:false
135-
└─IndexLookUp_23 455.80 root
135+
└─IndexLookUp_23 0.23 root
136136
├─IndexScan_20 1.00 cop table:dd, index:aid, dic, range: decided by [eq(test.dd.aid, test.gad.aid)], keep order:false
137-
└─Selection_22 455.80 cop eq(test.dd.bm, 0), eq(test.dd.pt, "android"), gt(test.dd.t, 1478143908), not(isnull(test.dd.ip)), not(isnull(test.dd.t))
137+
└─Selection_22 0.23 cop eq(test.dd.bm, 0), eq(test.dd.pt, "android"), gt(test.dd.t, 1478143908), not(isnull(test.dd.ip)), not(isnull(test.dd.t))
138138
└─TableScan_21 1.00 cop table:dd, keep order:false
139139
explain select gad.id as gid,sdk.id as sid,gad.aid as aid,gad.cm as cm,sdk.dic as dic,sdk.ip as ip, sdk.t as t, gad.p1 as p1, gad.p2 as p2, gad.p3 as p3, gad.p4 as p4, gad.p5 as p5, gad.p6_md5 as p6, gad.p7_md5 as p7, gad.ext as ext from st gad join dd sdk on gad.aid = sdk.aid and gad.dic = sdk.mac and gad.t < sdk.t where gad.t > 1477971479 and gad.bm = 0 and gad.pt = 'ios' and gad.dit = 'mac' and sdk.t > 1477971479 and sdk.bm = 0 and sdk.pt = 'ios' limit 3000;
140140
id count task operator info
@@ -144,9 +144,9 @@ Projection_10 170.34 root test.gad.id, test.sdk.id, test.gad.aid, test.gad.cm, t
144144
├─TableReader_23 170.34 root data:Selection_22
145145
│ └─Selection_22 170.34 cop eq(test.gad.bm, 0), eq(test.gad.dit, "mac"), eq(test.gad.pt, "ios"), gt(test.gad.t, 1477971479), not(isnull(test.gad.dic))
146146
│ └─TableScan_21 1999.00 cop table:gad, range:[0,+inf], keep order:false
147-
└─IndexLookUp_17 509.04 root
147+
└─IndexLookUp_17 0.25 root
148148
├─IndexScan_14 1.00 cop table:sdk, index:aid, dic, range: decided by [eq(test.sdk.aid, test.gad.aid)], keep order:false
149-
└─Selection_16 509.04 cop eq(test.sdk.bm, 0), eq(test.sdk.pt, "ios"), gt(test.sdk.t, 1477971479), not(isnull(test.sdk.mac)), not(isnull(test.sdk.t))
149+
└─Selection_16 0.25 cop eq(test.sdk.bm, 0), eq(test.sdk.pt, "ios"), gt(test.sdk.t, 1477971479), not(isnull(test.sdk.mac)), not(isnull(test.sdk.t))
150150
└─TableScan_15 1.00 cop table:dd, keep order:false
151151
explain SELECT cm, p1, p2, p3, p4, p5, p6_md5, p7_md5, count(1) as click_pv, count(DISTINCT ip) as click_ip FROM st WHERE (t between 1478188800 and 1478275200) and aid='cn.sbkcq' and pt='android' GROUP BY cm, p1, p2, p3, p4, p5, p6_md5, p7_md5;
152152
id count task operator info
@@ -164,9 +164,9 @@ Projection_10 428.32 root test.dt.id, test.dt.aid, test.dt.pt, test.dt.dic, test
164164
├─TableReader_41 428.32 root data:Selection_40
165165
│ └─Selection_40 428.32 cop eq(test.dt.bm, 0), eq(test.dt.pt, "ios"), gt(test.dt.t, 1478185592), not(isnull(test.dt.dic))
166166
│ └─TableScan_39 2000.00 cop table:dt, range:[0,+inf], keep order:false
167-
└─IndexLookUp_18 970.00 root
167+
└─IndexLookUp_18 0.48 root
168168
├─IndexScan_15 1.00 cop table:rr, index:aid, dic, range: decided by [eq(test.rr.aid, test.dt.aid) eq(test.rr.dic, test.dt.dic)], keep order:false
169-
└─Selection_17 970.00 cop eq(test.rr.pt, "ios"), gt(test.rr.t, 1478185592)
169+
└─Selection_17 0.48 cop eq(test.rr.pt, "ios"), gt(test.rr.t, 1478185592)
170170
└─TableScan_16 1.00 cop table:rr, keep order:false
171171
explain select pc,cr,count(DISTINCT uid) as pay_users,count(oid) as pay_times,sum(am) as am from pp where ps=2 and ppt>=1478188800 and ppt<1478275200 and pi in ('510017','520017') and uid in ('18089709','18090780') group by pc,cr;
172172
id count task operator info

cmd/explaintest/r/explain_easy.result

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ id count task operator info
4545
IndexJoin_12 4166.67 root left outer join, inner:IndexLookUp_11, outer key:test.t1.c2, inner key:test.t2.c1
4646
├─TableReader_24 3333.33 root data:TableScan_23
4747
│ └─TableScan_23 3333.33 cop table:t1, range:(1,+inf], keep order:false, stats:pseudo
48-
└─IndexLookUp_11 0.00 root
49-
├─Selection_10 0.00 cop not(isnull(test.t2.c1))
48+
└─IndexLookUp_11 9.99 root
49+
├─Selection_10 9.99 cop not(isnull(test.t2.c1))
5050
│ └─IndexScan_8 10.00 cop table:t2, index:c1, range: decided by [eq(test.t2.c1, test.t1.c2)], keep order:false, stats:pseudo
51-
└─TableScan_9 0.00 cop table:t2, keep order:false, stats:pseudo
51+
└─TableScan_9 9.99 cop table:t2, keep order:false, stats:pseudo
5252
explain update t1 set t1.c2 = 2 where t1.c1 = 1;
5353
id count task operator info
5454
Point_Get_1 1.00 root table:t1, handle:1

cmd/explaintest/r/generated_columns.result

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ ANALYZE TABLE sgc1, sgc2;
7272
EXPLAIN SELECT /*+ TIDB_INLJ(sgc1, sgc2) */ * from sgc1 join sgc2 on sgc1.a=sgc2.a;
7373
id count task operator info
7474
IndexJoin_17 5.00 root inner join, inner:IndexLookUp_16, outer key:test.sgc2.a, inner key:test.sgc1.a
75-
├─IndexLookUp_16 0.00 root
76-
│ ├─Selection_15 0.00 cop not(isnull(test.sgc1.a))
75+
├─IndexLookUp_16 5.00 root
76+
│ ├─Selection_15 5.00 cop not(isnull(test.sgc1.a))
7777
│ │ └─IndexScan_13 5.00 cop table:sgc1, index:a, range: decided by [eq(test.sgc1.a, test.sgc2.a)], keep order:false
78-
│ └─TableScan_14 0.00 cop table:sgc1, keep order:false, stats:pseudo
78+
│ └─TableScan_14 5.00 cop table:sgc1, keep order:false, stats:pseudo
7979
└─TableReader_20 1.00 root data:Selection_19
8080
└─Selection_19 1.00 cop not(isnull(test.sgc2.a))
8181
└─TableScan_18 1.00 cop table:sgc2, range:[-inf,+inf], keep order:false
@@ -86,10 +86,10 @@ Projection_6 5.00 root test.sgc1.j1, test.sgc1.j2, test.sgc1.a, test.sgc1.b, tes
8686
├─TableReader_39 1.00 root data:Selection_38
8787
│ └─Selection_38 1.00 cop not(isnull(test.sgc2.a))
8888
│ └─TableScan_37 1.00 cop table:sgc2, range:[-inf,+inf], keep order:false
89-
└─IndexLookUp_12 0.00 root
90-
├─Selection_11 0.00 cop not(isnull(test.sgc1.a))
89+
└─IndexLookUp_12 5.00 root
90+
├─Selection_11 5.00 cop not(isnull(test.sgc1.a))
9191
│ └─IndexScan_9 5.00 cop table:sgc1, index:a, range: decided by [eq(test.sgc1.a, test.sgc2.a)], keep order:false
92-
└─TableScan_10 0.00 cop table:sgc1, keep order:false, stats:pseudo
92+
└─TableScan_10 5.00 cop table:sgc1, keep order:false, stats:pseudo
9393
DROP TABLE IF EXISTS sgc3;
9494
CREATE TABLE sgc3 (
9595
j JSON,

cmd/explaintest/r/index_join.result

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ analyze table t1, t2;
77
explain select /*+ TIDB_INLJ(t1, t2) */ * from t1 join t2 on t1.a=t2.a;
88
id count task operator info
99
IndexJoin_16 5.00 root inner join, inner:IndexLookUp_15, outer key:test.t2.a, inner key:test.t1.a
10-
├─IndexLookUp_15 0.00 root
11-
│ ├─Selection_14 0.00 cop not(isnull(test.t1.a))
10+
├─IndexLookUp_15 5.00 root
11+
│ ├─Selection_14 5.00 cop not(isnull(test.t1.a))
1212
│ │ └─IndexScan_12 5.00 cop table:t1, index:a, range: decided by [eq(test.t1.a, test.t2.a)], keep order:false
13-
│ └─TableScan_13 0.00 cop table:t1, keep order:false, stats:pseudo
13+
│ └─TableScan_13 5.00 cop table:t1, keep order:false, stats:pseudo
1414
└─TableReader_19 1.00 root data:Selection_18
1515
└─Selection_18 1.00 cop not(isnull(test.t2.a))
1616
└─TableScan_17 1.00 cop table:t2, range:[-inf,+inf], keep order:false
@@ -21,7 +21,7 @@ Projection_6 5.00 root test.t1.a, test.t1.b, test.t2.a, test.t2.b
2121
├─TableReader_30 1.00 root data:Selection_29
2222
│ └─Selection_29 1.00 cop not(isnull(test.t2.a))
2323
│ └─TableScan_28 1.00 cop table:t2, range:[-inf,+inf], keep order:false
24-
└─IndexLookUp_11 0.00 root
25-
├─Selection_10 0.00 cop not(isnull(test.t1.a))
24+
└─IndexLookUp_11 5.00 root
25+
├─Selection_10 5.00 cop not(isnull(test.t1.a))
2626
│ └─IndexScan_8 5.00 cop table:t1, index:a, range: decided by [eq(test.t1.a, test.t2.a)], keep order:false
27-
└─TableScan_9 0.00 cop table:t1, keep order:false, stats:pseudo
27+
└─TableScan_9 5.00 cop table:t1, keep order:false, stats:pseudo

cmd/explaintest/r/topn_push_down.result

+4-4
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ Projection_13 0.00 root test.te.expect_time
177177
│ │ │ └─IndexScan_70 10.00 cop table:tr, index:shop_identy, trade_status, business_type, trade_pay_status, trade_type, delivery_type, source, biz_date, range:[810094178,810094178], keep order:false, stats:pseudo
178178
│ │ └─Selection_73 0.00 cop eq(test.tr.brand_identy, 32314), eq(test.tr.domain_type, 2)
179179
│ │ └─TableScan_71 0.00 cop table:tr, keep order:false, stats:pseudo
180-
│ └─IndexLookUp_35 250.00 root
180+
│ └─IndexLookUp_35 0.25 root
181181
│ ├─IndexScan_32 10.00 cop table:te, index:trade_id, range: decided by [eq(test.te.trade_id, test.tr.id)], keep order:false, stats:pseudo
182-
│ └─Selection_34 250.00 cop ge(test.te.expect_time, 2018-04-23 00:00:00.000000), le(test.te.expect_time, 2018-04-23 23:59:59.000000)
182+
│ └─Selection_34 0.25 cop ge(test.te.expect_time, 2018-04-23 00:00:00.000000), le(test.te.expect_time, 2018-04-23 23:59:59.000000)
183183
│ └─TableScan_33 10.00 cop table:te, keep order:false, stats:pseudo
184-
└─IndexReader_91 0.00 root index:Selection_90
185-
└─Selection_90 0.00 cop not(isnull(test.p.relate_id))
184+
└─IndexReader_91 9.99 root index:Selection_90
185+
└─Selection_90 9.99 cop not(isnull(test.p.relate_id))
186186
└─IndexScan_89 10.00 cop table:p, index:relate_id, range: decided by [eq(test.p.relate_id, test.tr.id)], keep order:false, stats:pseudo
187187
desc select 1 as a from dual order by a limit 1;
188188
id count task operator info

cmd/explaintest/r/tpch.result

+8-8
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ Projection_14 10.00 root tpch.lineitem.l_orderkey, 7_col_0, tpch.orders.o_orderd
260260
│ └─TableReader_52 36870000.00 root data:Selection_51
261261
│ └─Selection_51 36870000.00 cop lt(tpch.orders.o_orderdate, 1995-03-13 00:00:00.000000)
262262
│ └─TableScan_50 75000000.00 cop table:orders, range:[-inf,+inf], keep order:false
263-
└─IndexLookUp_28 162945114.27 root
263+
└─IndexLookUp_28 0.54 root
264264
├─IndexScan_25 1.00 cop table:lineitem, index:L_ORDERKEY, L_LINENUMBER, range: decided by [eq(tpch.lineitem.l_orderkey, tpch.orders.o_orderkey)], keep order:false
265-
└─Selection_27 162945114.27 cop gt(tpch.lineitem.l_shipdate, 1995-03-13 00:00:00.000000)
265+
└─Selection_27 0.54 cop gt(tpch.lineitem.l_shipdate, 1995-03-13 00:00:00.000000)
266266
└─TableScan_26 1.00 cop table:lineitem, keep order:false
267267
/*
268268
Q4 Order Priority Checking Query
@@ -301,9 +301,9 @@ Sort_10 1.00 root tpch.orders.o_orderpriority:asc
301301
├─TableReader_33 2925937.50 root data:Selection_32
302302
│ └─Selection_32 2925937.50 cop ge(tpch.orders.o_orderdate, 1995-01-01 00:00:00.000000), lt(tpch.orders.o_orderdate, 1995-04-01)
303303
│ └─TableScan_31 75000000.00 cop table:orders, range:[-inf,+inf], keep order:false
304-
└─IndexLookUp_20 240004648.80 root
304+
└─IndexLookUp_20 0.80 root
305305
├─IndexScan_17 1.00 cop table:lineitem, index:L_ORDERKEY, L_LINENUMBER, range: decided by [eq(tpch.lineitem.l_orderkey, tpch.orders.o_orderkey)], keep order:false
306-
└─Selection_19 240004648.80 cop lt(tpch.lineitem.l_commitdate, tpch.lineitem.l_receiptdate)
306+
└─Selection_19 0.80 cop lt(tpch.lineitem.l_commitdate, tpch.lineitem.l_receiptdate)
307307
└─TableScan_18 1.00 cop table:lineitem, keep order:false
308308
/*
309309
Q5 Local Supplier Volume Query
@@ -672,9 +672,9 @@ Projection_17 20.00 root tpch.customer.c_custkey, tpch.customer.c_name, 9_col_0,
672672
│ └─TableReader_48 3017307.69 root data:Selection_47
673673
│ └─Selection_47 3017307.69 cop ge(tpch.orders.o_orderdate, 1993-08-01 00:00:00.000000), lt(tpch.orders.o_orderdate, 1993-11-01)
674674
│ └─TableScan_46 75000000.00 cop table:orders, range:[-inf,+inf], keep order:false
675-
└─IndexLookUp_31 73916005.00 root
675+
└─IndexLookUp_31 0.25 root
676676
├─IndexScan_28 1.00 cop table:lineitem, index:L_ORDERKEY, L_LINENUMBER, range: decided by [eq(tpch.lineitem.l_orderkey, tpch.orders.o_orderkey)], keep order:false
677-
└─Selection_30 73916005.00 cop eq(tpch.lineitem.l_returnflag, "R")
677+
└─Selection_30 0.25 cop eq(tpch.lineitem.l_returnflag, "R")
678678
└─TableScan_29 1.00 cop table:lineitem, keep order:false
679679
/*
680680
Q11 Important Stock Identification Query
@@ -1241,9 +1241,9 @@ Projection_25 1.00 root tpch.supplier.s_name, 17_col_0
12411241
│ └─IndexLookUp_55 1.00 root
12421242
│ ├─IndexScan_53 1.00 cop table:l2, index:L_ORDERKEY, L_LINENUMBER, range: decided by [eq(tpch.l2.l_orderkey, tpch.l1.l_orderkey)], keep order:false
12431243
│ └─TableScan_54 1.00 cop table:lineitem, keep order:false
1244-
└─IndexLookUp_39 240004648.80 root
1244+
└─IndexLookUp_39 0.80 root
12451245
├─IndexScan_36 1.00 cop table:l3, index:L_ORDERKEY, L_LINENUMBER, range: decided by [eq(tpch.l3.l_orderkey, tpch.l1.l_orderkey)], keep order:false
1246-
└─Selection_38 240004648.80 cop gt(tpch.l3.l_receiptdate, tpch.l3.l_commitdate)
1246+
└─Selection_38 0.80 cop gt(tpch.l3.l_receiptdate, tpch.l3.l_commitdate)
12471247
└─TableScan_37 1.00 cop table:lineitem, keep order:false
12481248
/*
12491249
Q22 Global Sales Opportunity Query

executor/index_lookup_join_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ func (s *testSuite1) TestIndexJoinUnionScan(c *C) {
6767
"│ └─TableReader_17 9990.00 root data:Selection_16",
6868
"│ └─Selection_16 9990.00 cop not(isnull(test.t1.a))",
6969
"│ └─TableScan_15 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
70-
"└─UnionScan_12 0.00 root not(isnull(test.t2.a))",
71-
" └─IndexLookUp_11 0.00 root ",
72-
" ├─Selection_10 0.00 cop not(isnull(test.t2.a))",
70+
"└─UnionScan_12 9.99 root not(isnull(test.t2.a))",
71+
" └─IndexLookUp_11 9.99 root ",
72+
" ├─Selection_10 9.99 cop not(isnull(test.t2.a))",
7373
" │ └─IndexScan_8 10.00 cop table:t2, index:a, range: decided by [eq(test.t2.a, test.t1.a)], keep order:false, stats:pseudo",
74-
" └─TableScan_9 0.00 cop table:t2, keep order:false, stats:pseudo",
74+
" └─TableScan_9 9.99 cop table:t2, keep order:false, stats:pseudo",
7575
))
7676
tk.MustQuery("select /*+ TIDB_INLJ(t1, t2)*/ * from t1 join t2 on t1.a = t2.a").Check(testkit.Rows(
7777
"2 2 2 2 2",
@@ -85,9 +85,9 @@ func (s *testSuite1) TestIndexJoinUnionScan(c *C) {
8585
" │ └─TableReader_16 9990.00 root data:Selection_15",
8686
" │ └─Selection_15 9990.00 cop not(isnull(test.t1.a))",
8787
" │ └─TableScan_14 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
88-
" └─UnionScan_11 0.00 root not(isnull(test.t2.a))",
89-
" └─IndexReader_10 0.00 root index:Selection_9",
90-
" └─Selection_9 0.00 cop not(isnull(test.t2.a))",
88+
" └─UnionScan_11 9.99 root not(isnull(test.t2.a))",
89+
" └─IndexReader_10 9.99 root index:Selection_9",
90+
" └─Selection_9 9.99 cop not(isnull(test.t2.a))",
9191
" └─IndexScan_8 10.00 cop table:t2, index:a, range: decided by [eq(test.t2.a, test.t1.a)], keep order:false, stats:pseudo",
9292
))
9393
tk.MustQuery("select /*+ TIDB_INLJ(t1, t2)*/ t1.a, t2.a from t1 join t2 on t1.a = t2.a").Check(testkit.Rows(
@@ -114,9 +114,9 @@ func (s *testSuite1) TestBatchIndexJoinUnionScan(c *C) {
114114
" │ └─TableReader_22 9990.00 root data:Selection_21",
115115
" │ └─Selection_21 9990.00 cop not(isnull(test.t1.a))",
116116
" │ └─TableScan_20 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
117-
" └─UnionScan_26 0.00 root not(isnull(test.t2.a))",
118-
" └─IndexReader_25 0.00 root index:Selection_24",
119-
" └─Selection_24 0.00 cop not(isnull(test.t2.a))",
117+
" └─UnionScan_26 9.99 root not(isnull(test.t2.a))",
118+
" └─IndexReader_25 9.99 root index:Selection_24",
119+
" └─Selection_24 9.99 cop not(isnull(test.t2.a))",
120120
" └─IndexScan_23 10.00 cop table:t2, index:a, range: decided by [eq(test.t2.a, test.t1.a)], keep order:false, stats:pseudo",
121121
))
122122
tk.MustQuery("select /*+ TIDB_INLJ(t1, t2)*/ count(*) from t1 join t2 on t1.a = t2.id").Check(testkit.Rows(

planner/core/cbo_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,8 @@ func (s *testAnalyzeSuite) TestIssue9562(c *C) {
906906
"├─TableReader_12 9980.01 root data:Selection_11",
907907
"│ └─Selection_11 9980.01 cop not(isnull(test.t1.a)), not(isnull(test.t1.c))",
908908
"│ └─TableScan_10 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
909-
"└─IndexReader_8 0.00 root index:Selection_7",
910-
" └─Selection_7 0.00 cop not(isnull(test.t2.a)), not(isnull(test.t2.c))",
909+
"└─IndexReader_8 9.98 root index:Selection_7",
910+
" └─Selection_7 9.98 cop not(isnull(test.t2.a)), not(isnull(test.t2.c))",
911911
" └─IndexScan_6 10.00 cop table:t2, index:a, b, c, range: decided by [eq(test.t2.a, test.t1.a) gt(test.t2.b, minus(test.t1.b, 1)) lt(test.t2.b, plus(test.t1.b, 1))], keep order:false, stats:pseudo",
912912
))
913913

planner/core/exhaust_physical_plans.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,23 @@ func (p *LogicalJoin) constructInnerIndexScan(ds *DataSource, idx *model.IndexIn
588588

589589
is.initSchema(ds.id, idx, cop.tablePlan != nil)
590590
indexConds, tblConds := splitIndexFilterConditions(filterConds, idx.Columns, ds.tableInfo)
591-
path := &accessPath{indexFilters: indexConds, tableFilters: tblConds, countAfterIndex: math.MaxFloat64}
592-
is.addPushedDownSelection(cop, ds, math.MaxFloat64, path)
591+
path := &accessPath{
592+
indexFilters: indexConds,
593+
tableFilters: tblConds,
594+
countAfterAccess: rowCount,
595+
}
596+
// Assume equal conditions used by index join and other conditions are independent.
597+
if len(indexConds) > 0 {
598+
selectivity, _, err := ds.tableStats.HistColl.Selectivity(ds.ctx, indexConds)
599+
if err != nil {
600+
logutil.Logger(context.Background()).Debug("calculate selectivity failed, use selection factor", zap.Error(err))
601+
selectivity = selectionFactor
602+
}
603+
path.countAfterIndex = rowCount * selectivity
604+
}
605+
selectivity := ds.stats.RowCount / ds.tableStats.RowCount
606+
finalStats := ds.stats.ScaleByExpectCnt(selectivity * rowCount)
607+
is.addPushedDownSelection(cop, ds, path, finalStats)
593608
t := finishCopTask(ds.ctx, cop)
594609
reader := t.plan()
595610
return p.constructInnerUnionScan(us, reader)

0 commit comments

Comments
 (0)