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)); set @@session.tidb_opt_agg_push_down = 1; set @@session.tidb_opt_insubq_to_join_and_agg=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; 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; 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; 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; # 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)