Skip to content

Commit 02c7233

Browse files
qw4990ti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#61604
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 17bb929 commit 02c7233

File tree

5 files changed

+5522
-1
lines changed

5 files changed

+5522
-1
lines changed

pkg/planner/core/stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (ds *DataSource) getGroupNDVs(colGroups [][]*expression.Column) []property.
200200
break
201201
}
202202
}
203-
if match {
203+
if match && idx.IsEssentialStatsLoaded() {
204204
ndv := property.GroupNDV{
205205
Cols: idxCols,
206206
NDV: float64(idx.NDV),
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
drop table if exists t;
2+
CREATE TABLE `t` (`col_tinyint_key_signed` tinyint(4) DEFAULT NULL,`col_year_key_signed` year(4) DEFAULT NULL,KEY `col_tinyint_key_signed` (`col_tinyint_key_signed`),KEY `col_year_key_signed` (`col_year_key_signed`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
3+
insert into t values(-100,NULL);
4+
select /*+ inl_merge_join(t1, t2) */ count(*) from t t1 right join t t2 on t1. `col_year_key_signed` = t2. `col_tinyint_key_signed`;
5+
count(*)
6+
1
7+
drop table if exists t1, t2;
8+
create table t1(a int, b int, c int, d int, primary key(a,b,c));
9+
create table t2(a int, b int, c int, d int, primary key(a,b,c));
10+
insert into t1 values(1,1,1,1),(2,2,2,2),(3,3,3,3);
11+
insert into t2 values(1,1,1,1),(2,2,2,2);
12+
explain format = 'brief' select /*+ inl_merge_join(t1,t2) */ * from t1 left join t2 on t1.a = t2.a and t1.c = t2.c and t1.b = t2.b order by t1.a desc;
13+
id estRows task access object operator info
14+
Sort 12500.00 root executor__index_lookup_merge_join.t1.a:desc
15+
└─HashJoin 12500.00 root left outer join, left side:TableReader, equal:[eq(executor__index_lookup_merge_join.t1.a, executor__index_lookup_merge_join.t2.a) eq(executor__index_lookup_merge_join.t1.c, executor__index_lookup_merge_join.t2.c) eq(executor__index_lookup_merge_join.t1.b, executor__index_lookup_merge_join.t2.b)]
16+
├─TableReader(Build) 10000.00 root data:TableFullScan
17+
│ └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo
18+
└─TableReader(Probe) 10000.00 root data:TableFullScan
19+
└─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
20+
select /*+ inl_merge_join(t1,t2) */ * from t1 left join t2 on t1.a = t2.a and t1.c = t2.c and t1.b = t2.b order by t1.a desc;
21+
a b c d a b c d
22+
3 3 3 3 NULL NULL NULL NULL
23+
2 2 2 2 2 2 2 2
24+
1 1 1 1 1 1 1 1
25+
drop table if exists t1, t2;
26+
create table t1 (c_int int, primary key(c_int));
27+
create table t2 (c_int int, unique key (c_int)) partition by hash (c_int) partitions 4;
28+
insert into t1 values (1), (2), (3), (4), (5);
29+
insert into t2 select * from t1;
30+
begin;
31+
delete from t1 where c_int = 1;
32+
select /*+ INL_MERGE_JOIN(t1,t2) */ * from t1, t2 where t1.c_int = t2.c_int;
33+
c_int c_int
34+
2 2
35+
3 3
36+
4 4
37+
5 5
38+
select /*+ INL_JOIN(t1,t2) */ * from t1, t2 where t1.c_int = t2.c_int;
39+
c_int c_int
40+
2 2
41+
3 3
42+
4 4
43+
5 5
44+
select /*+ INL_HASH_JOIN(t1,t2) */ * from t1, t2 where t1.c_int = t2.c_int;
45+
c_int c_int
46+
2 2
47+
3 3
48+
4 4
49+
5 5
50+
commit;
51+
drop table if exists t1, t2;
52+
create table t1 (id bigint(20) unsigned, primary key(id));
53+
create table t2 (id bigint(20) unsigned);
54+
insert into t1 values (8738875760185212610);
55+
insert into t1 values (9814441339970117597);
56+
insert into t2 values (8738875760185212610);
57+
insert into t2 values (9814441339970117597);
58+
select /*+ INL_MERGE_JOIN(t1, t2) */ * from t2 left join t1 on t1.id = t2.id order by t1.id;
59+
id id
60+
8738875760185212610 8738875760185212610
61+
9814441339970117597 9814441339970117597
62+
set @@tidb_opt_advanced_join_hint=0;
63+
set @@tidb_partition_prune_mode= 'static';
64+
drop table if exists t1, t2;
65+
create table t1 (c_int int, c_str varchar(40), primary key (c_int) ) partition by range (c_int) ( partition p0 values less than (10), partition p1 values less than maxvalue );
66+
create table t2 (c_int int, c_str varchar(40), primary key (c_int) ) partition by range (c_int) ( partition p0 values less than (10), partition p1 values less than maxvalue );
67+
insert into t1 values (1, 'Alice');
68+
insert into t2 values (1, 'Bob');
69+
analyze table t1, t2 all columns;
70+
select /*+ INL_MERGE_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
71+
c_int c_str c_int c_str
72+
1 Alice 1 Bob
73+
explain format = 'brief' select /*+ INL_MERGE_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
74+
id estRows task access object operator info
75+
Projection 1.25 root executor__index_lookup_merge_join.t1.c_int, executor__index_lookup_merge_join.t1.c_str, executor__index_lookup_merge_join.t2.c_int, executor__index_lookup_merge_join.t2.c_str
76+
└─HashJoin 1.25 root inner join, equal:[eq(executor__index_lookup_merge_join.t2.c_int, executor__index_lookup_merge_join.t1.c_int)], other cond:lt(executor__index_lookup_merge_join.t1.c_str, executor__index_lookup_merge_join.t2.c_str)
77+
├─TableReader(Build) 1.00 root data:Selection
78+
│ └─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t2.c_str))
79+
│ └─TableFullScan 1.00 cop[tikv] table:t2, partition:p0 keep order:false
80+
└─PartitionUnion(Probe) 9991.00 root
81+
├─TableReader 1.00 root data:Selection
82+
│ └─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t1.c_str))
83+
│ └─TableFullScan 1.00 cop[tikv] table:t1, partition:p0 keep order:false
84+
└─TableReader 9990.00 root data:Selection
85+
└─Selection 9990.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t1.c_str))
86+
└─TableFullScan 10000.00 cop[tikv] table:t1, partition:p1 keep order:false, stats:pseudo
87+
show warnings;
88+
Level Code Message
89+
Warning 1815 The INDEX MERGE JOIN hint is deprecated for usage, try other hints.
90+
select /*+ INL_HASH_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
91+
c_int c_str c_int c_str
92+
1 Alice 1 Bob
93+
explain format = 'brief' select /*+ INL_HASH_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
94+
id estRows task access object operator info
95+
IndexHashJoin 1.25 root inner join, inner:TableReader, outer key:executor__index_lookup_merge_join.t1.c_int, inner key:executor__index_lookup_merge_join.t2.c_int, equal cond:eq(executor__index_lookup_merge_join.t1.c_int, executor__index_lookup_merge_join.t2.c_int), other cond:lt(executor__index_lookup_merge_join.t1.c_str, executor__index_lookup_merge_join.t2.c_str)
96+
├─PartitionUnion(Build) 9991.00 root
97+
│ ├─TableReader 1.00 root data:Selection
98+
│ │ └─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t1.c_str))
99+
│ │ └─TableFullScan 1.00 cop[tikv] table:t1, partition:p0 keep order:false
100+
│ └─TableReader 9990.00 root data:Selection
101+
│ └─Selection 9990.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t1.c_str))
102+
│ └─TableFullScan 10000.00 cop[tikv] table:t1, partition:p1 keep order:false, stats:pseudo
103+
└─TableReader(Probe) 1.25 root data:Selection
104+
└─Selection 1.25 cop[tikv] not(isnull(executor__index_lookup_merge_join.t2.c_str))
105+
└─TableRangeScan 1.25 cop[tikv] table:t2, partition:p0 range: decided by [executor__index_lookup_merge_join.t1.c_int], keep order:false
106+
select /*+ INL_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
107+
c_int c_str c_int c_str
108+
1 Alice 1 Bob
109+
explain format = 'brief' select /*+ INL_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
110+
id estRows task access object operator info
111+
IndexJoin 1.25 root inner join, inner:TableReader, outer key:executor__index_lookup_merge_join.t1.c_int, inner key:executor__index_lookup_merge_join.t2.c_int, equal cond:eq(executor__index_lookup_merge_join.t1.c_int, executor__index_lookup_merge_join.t2.c_int), other cond:lt(executor__index_lookup_merge_join.t1.c_str, executor__index_lookup_merge_join.t2.c_str)
112+
├─PartitionUnion(Build) 9991.00 root
113+
│ ├─TableReader 1.00 root data:Selection
114+
│ │ └─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t1.c_str))
115+
│ │ └─TableFullScan 1.00 cop[tikv] table:t1, partition:p0 keep order:false
116+
│ └─TableReader 9990.00 root data:Selection
117+
│ └─Selection 9990.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t1.c_str))
118+
│ └─TableFullScan 10000.00 cop[tikv] table:t1, partition:p1 keep order:false, stats:pseudo
119+
└─TableReader(Probe) 1.25 root data:Selection
120+
└─Selection 1.25 cop[tikv] not(isnull(executor__index_lookup_merge_join.t2.c_str))
121+
└─TableRangeScan 1.25 cop[tikv] table:t2, partition:p0 range: decided by [executor__index_lookup_merge_join.t1.c_int], keep order:false
122+
set @@tidb_partition_prune_mode= 'dynamic';
123+
drop table if exists t1, t2;
124+
create table t1 (c_int int, c_str varchar(40), primary key (c_int) ) partition by range (c_int) ( partition p0 values less than (10), partition p1 values less than maxvalue );
125+
create table t2 (c_int int, c_str varchar(40), primary key (c_int) ) partition by range (c_int) ( partition p0 values less than (10), partition p1 values less than maxvalue );
126+
insert into t1 values (1, 'Alice');
127+
insert into t2 values (1, 'Bob');
128+
analyze table t1, t2 all columns;
129+
select /*+ INL_MERGE_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
130+
c_int c_str c_int c_str
131+
1 Alice 1 Bob
132+
explain format = 'brief' select /*+ INL_MERGE_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
133+
id estRows task access object operator info
134+
MergeJoin 1.00 root inner join, left key:executor__index_lookup_merge_join.t1.c_int, right key:executor__index_lookup_merge_join.t2.c_int, other cond:lt(executor__index_lookup_merge_join.t1.c_str, executor__index_lookup_merge_join.t2.c_str)
135+
├─TableReader(Build) 1.00 root partition:p0 data:Selection
136+
│ └─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t2.c_str))
137+
│ └─TableFullScan 1.00 cop[tikv] table:t2 keep order:true
138+
└─TableReader(Probe) 1.00 root partition:all data:Selection
139+
└─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t1.c_str))
140+
└─TableFullScan 1.00 cop[tikv] table:t1 keep order:true
141+
show warnings;
142+
Level Code Message
143+
Warning 1815 The INDEX MERGE JOIN hint is deprecated for usage, try other hints.
144+
select /*+ INL_HASH_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
145+
c_int c_str c_int c_str
146+
1 Alice 1 Bob
147+
explain format = 'brief' select /*+ INL_HASH_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
148+
id estRows task access object operator info
149+
IndexHashJoin 1.00 root inner join, inner:TableReader, outer key:executor__index_lookup_merge_join.t2.c_int, inner key:executor__index_lookup_merge_join.t1.c_int, equal cond:eq(executor__index_lookup_merge_join.t2.c_int, executor__index_lookup_merge_join.t1.c_int), other cond:lt(executor__index_lookup_merge_join.t1.c_str, executor__index_lookup_merge_join.t2.c_str)
150+
├─TableReader(Build) 1.00 root partition:p0 data:Selection
151+
│ └─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t2.c_str))
152+
│ └─TableFullScan 1.00 cop[tikv] table:t2 keep order:false
153+
└─TableReader(Probe) 1.00 root partition:all data:Selection
154+
└─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t1.c_str))
155+
└─TableRangeScan 1.00 cop[tikv] table:t1 range: decided by [executor__index_lookup_merge_join.t2.c_int], keep order:false
156+
select /*+ INL_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
157+
c_int c_str c_int c_str
158+
1 Alice 1 Bob
159+
explain format = 'brief' select /*+ INL_JOIN(t1,t2) */ * from t1 join t2 partition(p0) on t1.c_int = t2.c_int and t1.c_str < t2.c_str;
160+
id estRows task access object operator info
161+
IndexJoin 1.00 root inner join, inner:TableReader, outer key:executor__index_lookup_merge_join.t2.c_int, inner key:executor__index_lookup_merge_join.t1.c_int, equal cond:eq(executor__index_lookup_merge_join.t2.c_int, executor__index_lookup_merge_join.t1.c_int), other cond:lt(executor__index_lookup_merge_join.t1.c_str, executor__index_lookup_merge_join.t2.c_str)
162+
├─TableReader(Build) 1.00 root partition:p0 data:Selection
163+
│ └─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t2.c_str))
164+
│ └─TableFullScan 1.00 cop[tikv] table:t2 keep order:false
165+
└─TableReader(Probe) 1.00 root partition:all data:Selection
166+
└─Selection 1.00 cop[tikv] not(isnull(executor__index_lookup_merge_join.t1.c_str))
167+
└─TableRangeScan 1.00 cop[tikv] table:t1 range: decided by [executor__index_lookup_merge_join.t2.c_int], keep order:false
168+
set @@tidb_opt_advanced_join_hint=DEFAULT;
169+
set @@tidb_partition_prune_mode= DEFAULT;
170+
drop table if exists t, s;
171+
create table s(a int, index(a));
172+
create table t(a int);
173+
insert into t values(1);
174+
select /*+ hash_join(t,s)*/ * from t left join s on t.a=s.a and t.a>1;
175+
a a
176+
1 NULL
177+
select /*+ inl_merge_join(t,s)*/ * from t left join s on t.a=s.a and t.a>1;
178+
a a
179+
1 NULL
180+
drop table if exists t1, t2;
181+
CREATE TABLE `t1` (`id` bigint(20) NOT NULL AUTO_INCREMENT, `t2id` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`), KEY `t2id` (`t2id`));
182+
INSERT INTO `t1` VALUES (1,NULL);
183+
CREATE TABLE `t2` (`id` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));
184+
SELECT /*+ INL_MERGE_JOIN(t1,t2) */ 1 from t1 left outer join t2 on t1.t2id=t2.id;
185+
1
186+
1
187+
SELECT /*+ HASH_JOIN(t1,t2) */ 1 from t1 left outer join t2 on t1.t2id=t2.id;
188+
1
189+
1
190+
drop table if exists x;
191+
CREATE TABLE `x` ( `a` enum('y','b','1','x','0','null') DEFAULT NULL, KEY `a` (`a`));
192+
insert into x values("x"),("x"),("b"),("y");
193+
SELECT /*+ merge_join (t2,t3) */ t2.a,t3.a FROM x t2 inner join x t3 on t2.a = t3.a;
194+
a a
195+
b b
196+
x x
197+
x x
198+
x x
199+
x x
200+
y y
201+
SELECT /*+ inl_merge_join (t2,t3) */ t2.a,t3.a FROM x t2 inner join x t3 on t2.a = t3.a;
202+
a a
203+
b b
204+
x x
205+
x x
206+
x x
207+
x x
208+
y y
209+
drop table if exists x;
210+
CREATE TABLE `x` ( `a` set('y','b','1','x','0','null') DEFAULT NULL, KEY `a` (`a`));
211+
insert into x values("x"),("x"),("b"),("y");
212+
SELECT /*+ merge_join (t2,t3) */ t2.a,t3.a FROM x t2 inner join x t3 on t2.a = t3.a;
213+
a a
214+
b b
215+
x x
216+
x x
217+
x x
218+
x x
219+
y y
220+
SELECT /*+ inl_merge_join (t2,t3) */ t2.a,t3.a FROM x t2 inner join x t3 on t2.a = t3.a;
221+
a a
222+
b b
223+
x x
224+
x x
225+
x x
226+
x x
227+
y y

tests/integrationtest/r/explain_complex.result

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ Projection 1.00 root explain_complex.st.cm, explain_complex.st.p1, explain_comp
153153
└─TableRowIDScan 250.00 cop[tikv] table:st keep order:false, stats:pseudo
154154
explain format = 'brief' select dt.id as id, dt.aid as aid, dt.pt as pt, dt.dic as dic, dt.cm as cm, rr.gid as gid, rr.acd as acd, rr.t as t,dt.p1 as p1, dt.p2 as p2, dt.p3 as p3, dt.p4 as p4, dt.p5 as p5, dt.p6_md5 as p6, dt.p7_md5 as p7 from dt dt join rr rr on (rr.pt = 'ios' and rr.t > 1478185592 and dt.aid = rr.aid and dt.dic = rr.dic) where dt.pt = 'ios' and dt.t > 1478185592 and dt.bm = 0 limit 2000;
155155
id estRows task access object operator info
156+
<<<<<<< HEAD
156157
Projection 0.01 root explain_complex.dt.id, explain_complex.dt.aid, explain_complex.dt.pt, explain_complex.dt.dic, explain_complex.dt.cm, explain_complex.rr.gid, explain_complex.rr.acd, explain_complex.rr.t, explain_complex.dt.p1, explain_complex.dt.p2, explain_complex.dt.p3, explain_complex.dt.p4, explain_complex.dt.p5, explain_complex.dt.p6_md5, explain_complex.dt.p7_md5
157158
└─Limit 0.01 root offset:0, count:2000
158159
└─IndexJoin 0.01 root inner join, inner:IndexLookUp, outer key:explain_complex.rr.aid, explain_complex.rr.dic, inner key:explain_complex.dt.aid, explain_complex.dt.dic, equal cond:eq(explain_complex.rr.aid, explain_complex.dt.aid), eq(explain_complex.rr.dic, explain_complex.dt.dic)
@@ -163,6 +164,18 @@ Projection 0.01 root explain_complex.dt.id, explain_complex.dt.aid, explain_com
163164
├─Selection(Build) 3.33 cop[tikv] not(isnull(explain_complex.dt.dic))
164165
│ └─IndexRangeScan 3.33 cop[tikv] table:dt, index:aid(aid, dic) range: decided by [eq(explain_complex.dt.aid, explain_complex.rr.aid) eq(explain_complex.dt.dic, explain_complex.rr.dic)], keep order:false, stats:pseudo
165166
└─Selection(Probe) 0.01 cop[tikv] eq(explain_complex.dt.bm, 0), eq(explain_complex.dt.pt, "ios"), gt(explain_complex.dt.t, 1478185592)
167+
=======
168+
Projection 1.25 root explain_complex.dt.id, explain_complex.dt.aid, explain_complex.dt.pt, explain_complex.dt.dic, explain_complex.dt.cm, explain_complex.rr.gid, explain_complex.rr.acd, explain_complex.rr.t, explain_complex.dt.p1, explain_complex.dt.p2, explain_complex.dt.p3, explain_complex.dt.p4, explain_complex.dt.p5, explain_complex.dt.p6_md5, explain_complex.dt.p7_md5
169+
└─Limit 1.25 root offset:0, count:2000
170+
└─IndexJoin 1.25 root inner join, inner:IndexLookUp, outer key:explain_complex.rr.aid, explain_complex.rr.dic, inner key:explain_complex.dt.aid, explain_complex.dt.dic, equal cond:eq(explain_complex.rr.aid, explain_complex.dt.aid), eq(explain_complex.rr.dic, explain_complex.dt.dic)
171+
├─TableReader(Build) 3.33 root data:Selection
172+
│ └─Selection 3.33 cop[tikv] eq(explain_complex.rr.pt, "ios"), gt(explain_complex.rr.t, 1478185592)
173+
│ └─TableFullScan 10000.00 cop[tikv] table:rr keep order:false, stats:pseudo
174+
└─IndexLookUp(Probe) 1.25 root
175+
├─Selection(Build) 3.33 cop[tikv] not(isnull(explain_complex.dt.dic))
176+
│ └─IndexRangeScan 3.33 cop[tikv] table:dt, index:aid(aid, dic) range: decided by [eq(explain_complex.dt.aid, explain_complex.rr.aid) eq(explain_complex.dt.dic, explain_complex.rr.dic)], keep order:false, stats:pseudo
177+
└─Selection(Probe) 1.25 cop[tikv] eq(explain_complex.dt.bm, 0), eq(explain_complex.dt.pt, "ios"), gt(explain_complex.dt.t, 1478185592)
178+
>>>>>>> 8d02f1fc8a1 (planner: fix the wrong join estimation depending on missing or uninitialized stats (#61604))
166179
└─TableRowIDScan 3.33 cop[tikv] table:dt keep order:false, stats:pseudo
167180
explain format = 'brief' 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;
168181
id estRows task access object operator info

0 commit comments

Comments
 (0)