2009-06-03 15:10:45 +02:00
|
|
|
drop table if exists t0, t1, t2, t3;
|
|
|
|
create table t1 (a int);
|
|
|
|
insert into t1 values (0),(1),(2),(3);
|
|
|
|
create table t0 as select * from t1;
|
|
|
|
create table t2 (a int primary key, b int)
|
|
|
|
as select a, a as b from t1 where a in (1,2);
|
|
|
|
create table t3 (a int primary key, b int)
|
|
|
|
as select a, a as b from t1 where a in (1,3);
|
|
|
|
# This will be eliminated:
|
|
|
|
explain select t1.a from t1 left join t2 on t2.a=t1.a;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
|
2009-06-14 12:01:10 +02:00
|
|
|
explain extended select t1.a from t1 left join t2 on t2.a=t1.a;
|
|
|
|
id select_type table type possible_keys key key_len ref rows filtered Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00
|
|
|
|
Warnings:
|
|
|
|
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where 1
|
2009-06-03 15:10:45 +02:00
|
|
|
select t1.a from t1 left join t2 on t2.a=t1.a;
|
|
|
|
a
|
|
|
|
0
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
# This will not be eliminated as t2.b is in in select list:
|
|
|
|
explain select * from t1 left join t2 on t2.a=t1.a;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
|
|
|
|
1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1
|
|
|
|
# This will not be eliminated as t2.b is in in order list:
|
|
|
|
explain select t1.a from t1 left join t2 on t2.a=t1.a order by t2.b;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
|
|
|
|
1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1
|
|
|
|
# This will not be eliminated as t2.b is in group list:
|
|
|
|
explain select t1.a from t1 left join t2 on t2.a=t1.a group by t2.b;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
|
|
|
|
1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1
|
|
|
|
# This will not be eliminated as t2.b is in the WHERE
|
|
|
|
explain select t1.a from t1 left join t2 on t2.a=t1.a where t2.b < 3 or t2.b is null;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
|
|
|
|
1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using where
|
|
|
|
# Elimination of multiple tables:
|
|
|
|
explain select t1.a from t1 left join (t2 join t3) on t2.a=t1.a and t3.a=t1.a;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
|
|
|
|
# Elimination of multiple tables (2):
|
|
|
|
explain select t1.a from t1 left join (t2 join t3 on t2.b=t3.b) on t2.a=t1.a and t3.a=t1.a;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
|
|
|
|
# Elimination when done within an outer join nest:
|
2009-06-14 12:01:10 +02:00
|
|
|
explain extended
|
2009-06-03 15:10:45 +02:00
|
|
|
select t0.*
|
|
|
|
from
|
|
|
|
t0 left join (t1 left join (t2 join t3 on t2.b=t3.b) on t2.a=t1.a and
|
|
|
|
t3.a=t1.a) on t0.a=t1.a;
|
2009-06-14 12:01:10 +02:00
|
|
|
id select_type table type possible_keys key key_len ref rows filtered Extra
|
|
|
|
1 SIMPLE t0 ALL NULL NULL NULL NULL 4 100.00
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00
|
|
|
|
Warnings:
|
|
|
|
Note 1003 select `test`.`t0`.`a` AS `a` from `test`.`t0` left join (`test`.`t1`) on((`test`.`t0`.`a` = `test`.`t1`.`a`)) where 1
|
2009-06-09 23:11:33 +02:00
|
|
|
# Elimination with aggregate functions
|
|
|
|
explain select count(*) from t1 left join t2 on t2.a=t1.a;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
|
|
|
|
explain select count(1) from t1 left join t2 on t2.a=t1.a;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
|
|
|
|
explain select count(1) from t1 left join t2 on t2.a=t1.a group by t1.a;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
|
|
|
|
This must not use elimination:
|
|
|
|
explain select count(1) from t1 left join t2 on t2.a=t1.a group by t2.a;
|
|
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
|
|
|
|
1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
|
2009-06-03 15:10:45 +02:00
|
|
|
drop table t0, t1, t2, t3;
|