You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

212 lines
9.9 KiB

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 select * from t3 where exists (select s.a from t3 s having sum(s.a) = t3.a );
explain select * from t1;
explain select * from t1 order by c2;
explain select * from t2 order by c2;
explain select * from t1 where t1.c1 > 0;
explain select t1.c1, t1.c2 from t1 where t1.c2 = 1;
explain select * from t1 left join t2 on t1.c2 = t2.c1 where t1.c1 > 1;
explain update t1 set t1.c2 = 2 where t1.c1 = 1;
explain delete from t1 where t1.c2 = 1;
explain select count(b.c2) from t1 a, t2 b where a.c1 = b.c2 group by a.c1;
explain select * from t2 order by t2.c2 limit 0, 1;
explain select * from t1 where c1 > 1 and c2 = 1 and c3 < 1;
explain select * from t1 where c1 = 1 and c2 > 1;
explain select sum(t1.c1 in (select c1 from t2)) from t1;
explain select c1 from t1 where c1 in (select c2 from t2);
explain select (select count(1) k from t1 s where s.c1 = t1.c1 having k != 0) from t1;
explain select * from information_schema.columns;
explain select c2 = (select c2 from t2 where t1.c1 = t2.c1 order by c1 limit 1) from t1;
explain select * from t1 order by c1 desc limit 1;
explain select * from t4 use index(idx) where a > 1 and b > 1 and c > 1 limit 1;
explain select * from t4 where a > 1 and c > 1 limit 1;
explain select ifnull(null, t1.c1) from t1;
explain select if(10, t1.c1, t1.c2) from t1;
explain select c1 from t2 union select c1 from t2 union all select c1 from t2;
explain 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 select count(1) from (select count(1) from (select * from t1 where c3 = 100) k) k2;
explain select 1 from (select count(c2), count(c3) from t1) k;
explain select count(1) from (select max(c2), count(c3) as m from t1) k;
explain select count(1) from (select count(c2) from t1 group by c3) k;
set @@session.tidb_opt_insubq_to_join_and_agg=0;
explain select sum(t1.c1 in (select c1 from t2)) from t1;
explain select 1 in (select c2 from t2) from t1;
explain 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 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 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 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 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 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 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);
explain select t.a = '123455' from t;
explain select t.a > '123455' from t;
explain select t.a != '123455' from t;
explain select t.a = 12345678912345678998789678687678.111 from t;
drop table if exists t;
create table t(a bigint, b bigint, index idx(a, b));
explain select * from t where a in (1, 2) and a in (1, 3);
explain select * from t where b in (1, 2) and b in (1, 3);
explain select * from t where a = 1 and a = 1;
explain select * from t where a = 1 and a = 2;
explain select * from t where b = 1 and b = 2;
explain select * from t t1 join t t2 where t1.b = t2.b and t2.b is null;
explain 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 select * from t where a = 1 and a = 2;
explain select null or a > 1 from t;
explain 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 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 select t1.a, t1.b from t1 left outer join t2 on t1.a = t2.a;
explain 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 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 select ifnull(a, 0) from t;
explain select ifnull(nb, 0) from t;
explain select ifnull(nb, 0), ifnull(nc, 0) from t;
explain select ifnull(a, 0), ifnull(nb, 0) from t;
explain select ifnull(nb, 0), ifnull(nb, 0) from t;
explain select 1+ifnull(nb, 0) from t;
explain select 1+ifnull(a, 0) from t;
explain select 1+ifnull(nb, 0) from t where nb=1;
# ifnull can be eliminated
explain 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 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 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 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 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 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 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 select * from t where _tidb_rowid = 0;
explain select * from t where _tidb_rowid > 0;
explain select a, _tidb_rowid from t where a > 0;
explain 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 select * from (select * from t order by (select 2)) t order by a, b;
explain 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 SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a;
explain 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 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 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 select * from t where a = 1;
explain 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 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 select a, b from t where a in (1) order by b;
explain 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 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 select * from t where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
explain update t set c = 'ssss' where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
drop table if exists t;