forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplain_easy.test
241 lines (212 loc) · 13.3 KB
/
explain_easy.test
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
use test;
drop table if exists t1, t2, t3, t4;
create table t1 (c1 int primary key, c2 int, c3 int, index c2 (c2));
create table t2 (c1 int unique, c2 int);
insert into t2 values(1, 0), (2, 1);
create table t3 (a bigint, b bigint, c bigint, d bigint);
create table t4 (a int, b int, c int, index idx(a, b), primary key(a));
create index expr_idx on t4((a+b+1));
set @@session.tidb_opt_agg_push_down = 1;
set @@session.tidb_opt_insubq_to_join_and_agg=1;
set @@session.tidb_hashagg_partial_concurrency = 1;
set @@session.tidb_hashagg_final_concurrency = 1;
set @@session.tidb_window_concurrency = 1;
explain format = 'brief' select * from t3 where exists (select s.a from t3 s having sum(s.a) = t3.a );
explain format = 'brief' select * from t1;
explain format = 'brief' select * from t1 order by c2;
explain format = 'brief' select * from t2 order by c2;
explain format = 'brief' select * from t1 where t1.c1 > 0;
explain format = 'brief' select t1.c1, t1.c2 from t1 where t1.c2 = 1;
explain format = 'brief' select * from t1 left join t2 on t1.c2 = t2.c1 where t1.c1 > 1;
explain format = 'brief' update t1 set t1.c2 = 2 where t1.c1 = 1;
explain format = 'brief' delete from t1 where t1.c2 = 1;
explain format = 'brief' select count(b.c2) from t1 a, t2 b where a.c1 = b.c2 group by a.c1;
explain format = 'brief' select * from t2 order by t2.c2 limit 0, 1;
explain format = 'brief' select * from t1 where c1 > 1 and c2 = 1 and c3 < 1;
explain format = 'brief' select * from t1 where c1 = 1 and c2 > 1;
explain format = 'brief' select sum(t1.c1 in (select c1 from t2)) from t1;
explain format = 'brief' select c1 from t1 where c1 in (select c2 from t2);
explain format = 'brief' select (select count(1) k from t1 s where s.c1 = t1.c1 having k != 0) from t1;
explain format = 'brief' select * from information_schema.columns;
explain format = 'brief' select * from information_schema.columns where table_name = 'T1';
explain format = 'brief' select * from information_schema.columns where table_schema = 'TEST' and table_name = 'T1' and column_name = 'c1';
explain format = 'brief' select c2 = (select c2 from t2 where t1.c1 = t2.c1 order by c1 limit 1) from t1;
explain format = 'brief' select * from t1 order by c1 desc limit 1;
explain format = 'brief' select * from t4 use index(idx) where a > 1 and b > 1 and c > 1 limit 1;
explain format = 'brief' select * from t4 where a > 1 and c > 1 limit 1;
explain format = 'brief' select ifnull(null, t1.c1) from t1;
explain format = 'brief' select if(10, t1.c1, t1.c2) from t1;
explain format = 'brief' select c1 from t2 union select c1 from t2 union all select c1 from t2;
explain format = 'brief' select c1 from t2 union all select c1 from t2 union select c1 from t2;
select * from information_schema.tidb_indexes where table_name='t4';
# https://github.com/pingcap/tidb/issues/9125
explain format = 'brief' select count(1) from (select count(1) from (select * from t1 where c3 = 100) k) k2;
explain format = 'brief' select 1 from (select count(c2), count(c3) from t1) k;
explain format = 'brief' select count(1) from (select max(c2), count(c3) as m from t1) k;
explain format = 'brief' select count(1) from (select count(c2) from t1 group by c3) k;
set @@session.tidb_opt_insubq_to_join_and_agg=0;
explain format = 'brief' select sum(t1.c1 in (select c1 from t2)) from t1;
explain format = 'brief' select 1 in (select c2 from t2) from t1;
explain format = 'brief' select sum(6 in (select c2 from t2)) from t1;
explain format="dot" select sum(t1.c1 in (select c1 from t2)) from t1;
explain format="dot" select 1 in (select c2 from t2) from t1;
drop table if exists t1, t2, t3, t4;
drop table if exists t;
create table t(a int primary key, b int, c int, index idx(b));
explain format = 'brief' select t.c in (select count(*) from t s ignore index(idx), t t1 where s.a = t.a and s.a = t1.a) from t;
explain format = 'brief' 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;
explain format = 'brief' 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;
insert into t values(1, 1, 1), (2, 2 ,2), (3, 3, 3), (4, 3, 4),(5,3,5);
analyze table t;
explain format = 'brief' select t.c in (select count(*) from t s, t t1 where s.b = t.a and s.b = 3 and s.a = t1.a) from t;
explain format = 'brief' select t.c in (select count(*) from t s left join t t1 on s.a = t1.a where 3 = t.a and s.b = 3) from t;
explain format = 'brief' select t.c in (select count(*) from t s right join t t1 on s.a = t1.a where 3 = t.a and t1.b = 3) from t;
drop table if exists t;
create table t(a int unsigned not null);
explain format = 'brief' select t.a = '123455' from t;
explain format = 'brief' select t.a > '123455' from t;
explain format = 'brief' select t.a != '123455' from t;
explain format = 'brief' select t.a = 12345678912345678998789678687678.111 from t;
drop table if exists t;
create table t(a bigint, b bigint, index idx(a, b));
explain format = 'brief' select * from t where a in (1, 2) and a in (1, 3);
explain format = 'brief' select * from t where b in (1, 2) and b in (1, 3);
explain format = 'brief' select * from t where a = 1 and a = 1;
explain format = 'brief' select * from t where a = 1 and a = 2;
explain format = 'brief' select * from t where b = 1 and b = 2;
explain format = 'brief' select * from t t1 join t t2 where t1.b = t2.b and t2.b is null;
explain format = 'brief' select * from t t1 where not exists (select * from t t2 where t1.b = t2.b);
drop table if exists t;
create table t(a bigint primary key);
explain format = 'brief' select * from t where a = 1 and a = 2;
explain format = 'brief' select null or a > 1 from t;
explain format = 'brief' select * from t where a = 1 for update;
drop table if exists ta, tb;
create table ta (a varchar(20));
create table tb (a varchar(20));
begin;
insert tb values ('1');
explain format = 'brief' select * from ta where a = 1;
rollback;
# outer join elimination
drop table if exists t1, t2;
create table t1(a int, b int, c int, primary key(a, b));
create table t2(a int, b int, c int, primary key(a));
explain format = 'brief' select t1.a, t1.b from t1 left outer join t2 on t1.a = t2.a;
explain format = 'brief' select distinct t1.a, t1.b from t1 left outer join t2 on t1.a = t2.a;
CREATE TABLE `test01` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`stat_date` int(11) NOT NULL DEFAULT '0',
`show_date` varchar(20) NOT NULL DEFAULT '',
`region_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`period` tinyint(3) unsigned NOT NULL DEFAULT '0',
`registration_num` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `test02` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`region_name` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
explain format = 'brief' SELECT COUNT(1) FROM (SELECT COALESCE(b.region_name, '不详') region_name, SUM(a.registration_num) registration_num FROM (SELECT stat_date, show_date, region_id, 0 registration_num FROM test01 WHERE period = 1 AND stat_date >= 20191202 AND stat_date <= 20191202 UNION ALL SELECT stat_date, show_date, region_id, registration_num registration_num FROM test01 WHERE period = 1 AND stat_date >= 20191202 AND stat_date <= 20191202) a LEFT JOIN test02 b ON a.region_id = b.id WHERE registration_num > 0 AND a.stat_date >= '20191202' AND a.stat_date <= '20191202' GROUP BY a.stat_date , a.show_date , COALESCE(b.region_name, '不详') ) JLS;
# https://github.com/pingcap/tidb/issues/7918
drop table if exists t;
create table t(a int, nb int not null, nc int not null);
explain format = 'brief' select ifnull(a, 0) from t;
explain format = 'brief' select ifnull(nb, 0) from t;
explain format = 'brief' select ifnull(nb, 0), ifnull(nc, 0) from t;
explain format = 'brief' select ifnull(a, 0), ifnull(nb, 0) from t;
explain format = 'brief' select ifnull(nb, 0), ifnull(nb, 0) from t;
explain format = 'brief' select 1+ifnull(nb, 0) from t;
explain format = 'brief' select 1+ifnull(a, 0) from t;
explain format = 'brief' select 1+ifnull(nb, 0) from t where nb=1;
# ifnull can be eliminated
explain format = 'brief' select * from t ta left outer join t tb on ta.nb = tb.nb and ta.a > 1 where ifnull(ta.nb, 1) or ta.nb is null;
explain format = 'brief' select * from t ta right outer join t tb on ta.nb = tb.nb and ta.a > 1 where ifnull(tb.nb, 1) or tb.nb is null;
explain format = 'brief' select * from t ta inner join t tb on ta.nb = tb.nb and ta.a > 1 where ifnull(tb.nb, 1) or tb.nb is null;
explain format = 'brief' select ifnull(t.nc, 1) in (select count(*) from t s , t t1 where s.a = t.a and s.a = t1.a) from t;
# ifnull cannot be eliminated
explain format = 'brief' select * from t ta left outer join t tb on ta.nb = tb.nb and ta.a > 1 where ifnull(tb.a, 1) or tb.a is null;
explain format = 'brief' select * from t ta right outer join t tb on ta.nb = tb.nb and ta.a > 1 where ifnull(tb.a, 1) or tb.a is null;
# when it comes to inner join case, ifnull can always be eliminated on not null column
explain format = 'brief' select ifnull(t.a, 1) in (select count(*) from t s , t t1 where s.a = t.a and s.a = t1.a) from t;
drop table if exists t;
create table t(a int);
explain format = 'brief' select * from t where _tidb_rowid = 0;
explain format = 'brief' select * from t where _tidb_rowid > 0;
explain format = 'brief' select a, _tidb_rowid from t where a > 0;
explain format = 'brief' select * from t where _tidb_rowid > 0 and a > 0;
drop table if exists t;
create table t(a int, b int, c int);
explain format = 'brief' select * from (select * from t order by (select 2)) t order by a, b;
explain format = 'brief' select * from (select * from t order by c) t order by a, b;
drop table if exists t;
set @@session.tidb_opt_insubq_to_join_and_agg=1;
explain format = 'brief' SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a;
explain format = 'brief' SELECT 0 AS a FROM dual UNION (SELECT 1 AS a FROM dual ORDER BY a);
create table t (i int key, j int, unique key (i, j));
begin;
insert into t values (1, 1);
explain format = 'brief' update t set j = -j where i = 1 and j = 1;
rollback;
drop table if exists t;
# https://github.com/pingcap/tidb/issues/10344
create table t(a int);
begin;
insert into t values (1);
explain format = 'brief' select * from t left outer join t t1 on t.a = t1.a where t.a not between 1 and 2;
rollback;
drop table if exists t;
create table t(a time, b date);
insert into t values (1, "1000-01-01"), (2, "1000-01-02"), (3, "1000-01-03");
analyze table t;
explain format = 'brief' select * from t where a = 1;
explain format = 'brief' select * from t where b = "1000-01-01";
drop table t;
create table t(a int);
insert into t values (1),(2),(2),(2),(9),(9),(9),(10);
analyze table t with 1 buckets;
explain format = 'brief' select * from t where a >= 3 and a <= 8;
drop table t;
# https://github.com/pingcap/tidb/issues/10626
create table t(a int, b int, index idx_ab(a, b));
explain format = 'brief' select a, b from t where a in (1) order by b;
explain format = 'brief' select a, b from t where a = 1 order by b;
drop table if exists t;
# https://github.com/pingcap/tidb/issues/11903
create table t(a int, b int);
explain format = 'brief' select a, b from (select a, b, avg(b) over (partition by a)as avg_b from t) as tt where a > 10 and b < 10 and a > avg_b;
drop table if exists t;
create table t(a int, b int);
explain format="dot" select * from t where a < 2;
drop table if exists t;
# select / update should choose same access path for table t.
create table t(a binary(16) not null, b varchar(2) default null, c varchar(100) default 'aaaa', key (a,b));
explain format = 'brief' select * from t where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
explain format = 'brief' update t set c = 'ssss' where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
drop table if exists t;
create table t(a int, b int);
explain format = 'brief' select (select count(n.a) from t) from t n;
explain format = 'brief' select (select sum((select count(a)))) from t;
explain format = 'brief' select count(a) from t group by b order by (select count(a));
explain format = 'brief' select (select sum(count(a))) from t;
explain format = 'brief' select sum(a), (select sum(a)), count(a) from t group by b order by (select count(a));
drop table if exists t;
# lower precision for cast to decimal for integer type variables in sum function
create table t(a tinyint, b smallint, c mediumint, d int, e bigint);
insert into mysql.opt_rule_blacklist VALUES("aggregation_push_down");
admin reload opt_rule_blacklist;
explain format = 'brief' select sum(t1.a) from t t1 join t t2 on t1.a=t2.a;
explain format = 'brief' select sum(t1.b) from t t1 join t t2 on t1.b=t2.b;
explain format = 'brief' select sum(t1.c) from t t1 join t t2 on t1.c=t2.c;
explain format = 'brief' select sum(t1.d) from t t1 join t t2 on t1.d=t2.d;
explain format = 'brief' select sum(t1.e) from t t1 join t t2 on t1.e=t2.e;
# note that avg will be converted to count and sum, and .decimal field will be non-zero
explain format = 'brief' select avg(t1.a) from t t1 join t t2 on t1.a=t2.a;
explain format = 'brief' select avg(t1.b) from t t1 join t t2 on t1.b=t2.b;
explain format = 'brief' select avg(t1.c) from t t1 join t t2 on t1.c=t2.c;
explain format = 'brief' select avg(t1.d) from t t1 join t t2 on t1.d=t2.d;
explain format = 'brief' select avg(t1.e) from t t1 join t t2 on t1.e=t2.e;
drop table if exists t;
delete from mysql.opt_rule_blacklist where name="aggregation_push_down";
admin reload opt_rule_blacklist;