Commit 57c199d8 authored by Sergey Petrunya's avatar Sergey Petrunya

Merge maria-5.1 -> maria-5.1-table-elimination

parents 915a624c fef409cb
...@@ -1920,3 +1920,4 @@ sql/share/swedish ...@@ -1920,3 +1920,4 @@ sql/share/swedish
sql/share/ukrainian sql/share/ukrainian
libmysqld/examples/mysqltest.cc libmysqld/examples/mysqltest.cc
extra/libevent/event-config.h extra/libevent/event-config.h
libmysqld/opt_table_elimination.cc
...@@ -76,7 +76,7 @@ sqlsources = derror.cc field.cc field_conv.cc strfunc.cc filesort.cc \ ...@@ -76,7 +76,7 @@ sqlsources = derror.cc field.cc field_conv.cc strfunc.cc filesort.cc \
rpl_filter.cc sql_partition.cc sql_builtin.cc sql_plugin.cc \ rpl_filter.cc sql_partition.cc sql_builtin.cc sql_plugin.cc \
sql_tablespace.cc \ sql_tablespace.cc \
rpl_injector.cc my_user.c partition_info.cc \ rpl_injector.cc my_user.c partition_info.cc \
sql_servers.cc event_parse_data.cc sql_servers.cc event_parse_data.cc opt_table_elimination.cc
libmysqld_int_a_SOURCES= $(libmysqld_sources) libmysqld_int_a_SOURCES= $(libmysqld_sources)
nodist_libmysqld_int_a_SOURCES= $(libmysqlsources) $(sqlsources) nodist_libmysqld_int_a_SOURCES= $(libmysqlsources) $(sqlsources)
......
...@@ -3,6 +3,9 @@ SET @old_max_allowed_packet= @@global.max_allowed_packet; ...@@ -3,6 +3,9 @@ SET @old_max_allowed_packet= @@global.max_allowed_packet;
SET @@global.max_allowed_packet = 2 * 1024 * 1024 + 1024; SET @@global.max_allowed_packet = 2 * 1024 * 1024 + 1024;
CREATE TABLE t1(data LONGBLOB); CREATE TABLE t1(data LONGBLOB);
INSERT INTO t1 SELECT REPEAT('1', 2*1024*1024); INSERT INTO t1 SELECT REPEAT('1', 2*1024*1024);
SELECT COUNT(*) FROM t1;
COUNT(*)
1
SET @old_general_log = @@global.general_log; SET @old_general_log = @@global.general_log;
SET @@global.general_log = 0; SET @@global.general_log = 0;
SET @@global.general_log = @old_general_log; SET @@global.general_log = @old_general_log;
......
...@@ -121,8 +121,8 @@ insert into t1 values (1); ...@@ -121,8 +121,8 @@ insert into t1 values (1);
explain select * from t1 where 3 in (select (1+1) union select 1); explain select * from t1 where 3 in (select (1+1) union select 1);
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible HAVING
3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used 3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL Impossible HAVING
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
select * from t1 where 3 in (select (1+1) union select 1); select * from t1 where 3 in (select (1+1) union select 1);
a a
......
...@@ -3585,7 +3585,6 @@ INSERT INTO t2 VALUES (1,'a'),(2,'b'),(3,'c'); ...@@ -3585,7 +3585,6 @@ INSERT INTO t2 VALUES (1,'a'),(2,'b'),(3,'c');
EXPLAIN SELECT t1.a FROM t1 LEFT JOIN t2 ON t2.b=t1.b WHERE t1.a=3; EXPLAIN SELECT t1.a FROM t1 LEFT JOIN t2 ON t2.b=t1.b WHERE t1.a=3;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1 1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1
1 SIMPLE t2 const b b 22 const 1 Using index
DROP TABLE t1,t2; DROP TABLE t1,t2;
CREATE TABLE t1(id int PRIMARY KEY, b int, e int); CREATE TABLE t1(id int PRIMARY KEY, b int, e int);
CREATE TABLE t2(i int, a int, INDEX si(i), INDEX ai(a)); CREATE TABLE t2(i int, a int, INDEX si(i), INDEX ai(a));
......
...@@ -4353,13 +4353,13 @@ id select_type table type possible_keys key key_len ref rows filtered Extra ...@@ -4353,13 +4353,13 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
Warnings: Warnings:
Note 1003 select 1 AS `1` from `test`.`t1` where <in_optimizer>(1,<exists>(select 1 AS `1` from `test`.`t1` group by `test`.`t1`.`a` having (<cache>(1) = <ref_null_helper>(1)))) Note 1003 select 1 AS `1` from `test`.`t1` where <in_optimizer>(1,<exists>(select 1 AS `1` from `test`.`t1` group by `test`.`t1`.`a` having 1))
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 WHERE a > 3 GROUP BY a); EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 WHERE a > 3 GROUP BY a);
id select_type table type possible_keys key key_len ref rows filtered Extra id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Using temporary; Using filesort 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Using temporary; Using filesort
Warnings: Warnings:
Note 1003 select 1 AS `1` from `test`.`t1` where <in_optimizer>(1,<exists>(select 1 AS `1` from `test`.`t1` where (`test`.`t1`.`a` > 3) group by `test`.`t1`.`a` having (<cache>(1) = <ref_null_helper>(1)))) Note 1003 select 1 AS `1` from `test`.`t1` where <in_optimizer>(1,<exists>(select 1 AS `1` from `test`.`t1` where (`test`.`t1`.`a` > 3) group by `test`.`t1`.`a` having 1))
DROP TABLE t1; DROP TABLE t1;
End of 5.0 tests. End of 5.0 tests.
CREATE TABLE t1 (a INT, b INT); CREATE TABLE t1 (a INT, b INT);
......
drop table if exists t0, t1, t2, t3;
drop view if exists v1, v2;
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
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
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:
explain extended
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;
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
# 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
drop table t0, t1, t2, t3;
create table t0 ( id integer, primary key (id));
create table t1 (
id integer,
attr1 integer,
primary key (id),
key (attr1)
);
create table t2 (
id integer,
attr2 integer,
fromdate date,
primary key (id, fromdate),
key (attr2,fromdate)
);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
insert into t0 select A.id + 10*B.id from t0 A, t0 B where B.id > 0;
insert into t1 select id, id from t0;
insert into t2 select id, id, date_add('2009-06-22', interval id day) from t0;
insert into t2 select id, id+1, date_add('2008-06-22', interval id day) from t0;
create view v1 as
select
F.id, A1.attr1, A2.attr2
from
t0 F
left join t1 A1 on A1.id=F.id
left join t2 A2 on A2.id=F.id and
A2.fromdate=(select MAX(fromdate) from
t2 where id=A2.id);
create view v2 as
select
F.id, A1.attr1, A2.attr2
from
t0 F
left join t1 A1 on A1.id=F.id
left join t2 A2 on A2.id=F.id and
A2.fromdate=(select MAX(fromdate) from
t2 where id=F.id);
This should use one table:
explain select id from v1 where id=2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY F const PRIMARY PRIMARY 4 const 1 Using index
This should use one table:
explain extended select id from v1 where id in (1,2,3,4);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY F range PRIMARY PRIMARY 4 NULL 4 100.00 Using where; Using index
Warnings:
Note 1276 Field or reference 'test.A2.id' of SELECT #3 was resolved in SELECT #1
Note 1003 select `F`.`id` AS `id` from `test`.`t0` `F` where (`F`.`id` in (1,2,3,4))
This should use facts and A1 tables:
explain extended select id from v1 where attr1 between 12 and 14;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY A1 range PRIMARY,attr1 attr1 5 NULL 2 100.00 Using where
1 PRIMARY F eq_ref PRIMARY PRIMARY 4 test.A1.id 1 100.00 Using index
Warnings:
Note 1276 Field or reference 'test.A2.id' of SELECT #3 was resolved in SELECT #1
Note 1003 select `F`.`id` AS `id` from `test`.`t0` `F` join `test`.`t1` `A1` where ((`F`.`id` = `A1`.`id`) and (`A1`.`attr1` between 12 and 14))
This should use facts, A2 and its subquery:
explain extended select id from v1 where attr2 between 12 and 14;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY A2 range PRIMARY,attr2 attr2 5 NULL 5 100.00 Using where
1 PRIMARY F eq_ref PRIMARY PRIMARY 4 test.A2.id 1 100.00 Using index
3 DEPENDENT SUBQUERY t2 ref PRIMARY PRIMARY 4 test.A2.id 2 100.00 Using index
Warnings:
Note 1276 Field or reference 'test.A2.id' of SELECT #3 was resolved in SELECT #1
Note 1003 select `F`.`id` AS `id` from `test`.`t0` `F` join `test`.`t2` `A2` where ((`F`.`id` = `A2`.`id`) and (`A2`.`attr2` between 12 and 14) and (`A2`.`fromdate` = (select max(`test`.`t2`.`fromdate`) AS `MAX(fromdate)` from `test`.`t2` where (`test`.`t2`.`id` = `A2`.`id`))))
This should use one table:
explain select id from v2 where id=2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY F const PRIMARY PRIMARY 4 const 1 Using index
This should use one table:
explain extended select id from v2 where id in (1,2,3,4);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY F range PRIMARY PRIMARY 4 NULL 4 100.00 Using where; Using index
Warnings:
Note 1276 Field or reference 'test.F.id' of SELECT #3 was resolved in SELECT #1
Note 1003 select `F`.`id` AS `id` from `test`.`t0` `F` where (`F`.`id` in (1,2,3,4))
This should use facts and A1 tables:
explain extended select id from v2 where attr1 between 12 and 14;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY A1 range PRIMARY,attr1 attr1 5 NULL 2 100.00 Using where
1 PRIMARY F eq_ref PRIMARY PRIMARY 4 test.A1.id 1 100.00 Using index
Warnings:
Note 1276 Field or reference 'test.F.id' of SELECT #3 was resolved in SELECT #1
Note 1003 select `F`.`id` AS `id` from `test`.`t0` `F` join `test`.`t1` `A1` where ((`F`.`id` = `A1`.`id`) and (`A1`.`attr1` between 12 and 14))
This should use facts, A2 and its subquery:
explain extended select id from v2 where attr2 between 12 and 14;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY A2 range PRIMARY,attr2 attr2 5 NULL 5 100.00 Using where
1 PRIMARY F eq_ref PRIMARY PRIMARY 4 test.A2.id 1 100.00 Using where; Using index
3 DEPENDENT SUBQUERY t2 ref PRIMARY PRIMARY 4 test.F.id 2 100.00 Using index
Warnings:
Note 1276 Field or reference 'test.F.id' of SELECT #3 was resolved in SELECT #1
Note 1003 select `F`.`id` AS `id` from `test`.`t0` `F` join `test`.`t2` `A2` where ((`F`.`id` = `A2`.`id`) and (`A2`.`attr2` between 12 and 14) and (`A2`.`fromdate` = (select max(`test`.`t2`.`fromdate`) AS `MAX(fromdate)` from `test`.`t2` where (`test`.`t2`.`id` = `F`.`id`))))
drop view v1, v2;
drop table t0, t1, t2;
create table t1 (a int);
insert into t1 values (0),(1),(2),(3);
create table t2 (pk1 int, pk2 int, pk3 int, col int, primary key(pk1, pk2, pk3));
insert into t2 select a,a,a,a from t1;
This must use only t1:
explain select t1.* from t1 left join t2 on t2.pk1=t1.a and
t2.pk2=t2.pk1+1 and
t2.pk3=t2.pk2+1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
This must use only t1:
explain select t1.* from t1 left join t2 on t2.pk1=t1.a and
t2.pk3=t2.pk1+1 and
t2.pk2=t2.pk3+1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
This must use both:
explain select t1.* from t1 left join t2 on t2.pk1=t1.a and
t2.pk3=t2.pk1+1 and
t2.pk2=t2.pk3+t2.col;
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 ref PRIMARY PRIMARY 4 test.t1.a 1
This must use only t1:
explain select t1.* from t1 left join t2 on t2.pk2=t1.a and
t2.pk1=t2.pk2+1 and
t2.pk3=t2.pk1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
drop table t1, t2;
...@@ -522,7 +522,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra ...@@ -522,7 +522,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
2 UNION t2 const PRIMARY PRIMARY 4 const 1 100.00 2 UNION t2 const PRIMARY PRIMARY 4 const 1 100.00
NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL NULL NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL NULL
Warnings: Warnings:
Note 1003 (select '1' AS `a`,'1' AS `b` from `test`.`t1` where ('1' = 1)) union (select '1' AS `a`,'10' AS `b` from `test`.`t2` where ('1' = 1)) Note 1003 (select '1' AS `a`,'1' AS `b` from `test`.`t1` where 1) union (select '1' AS `a`,'10' AS `b` from `test`.`t2` where 1)
(select * from t1 where a=5) union (select * from t2 where a=1); (select * from t1 where a=5) union (select * from t2 where a=1);
a b a b
1 10 1 10
......
...@@ -27,7 +27,8 @@ connect (con1, localhost, root,,); ...@@ -27,7 +27,8 @@ connect (con1, localhost, root,,);
CREATE TABLE t1(data LONGBLOB); CREATE TABLE t1(data LONGBLOB);
INSERT INTO t1 SELECT REPEAT('1', 2*1024*1024); INSERT INTO t1 SELECT REPEAT('1', 2*1024*1024);
# The following is to remove the race between end of insert and start of MYSQL_DUMP:
SELECT COUNT(*) FROM t1;
let $outfile= $MYSQLTEST_VARDIR/tmp/bug41486.sql; let $outfile= $MYSQLTEST_VARDIR/tmp/bug41486.sql;
--error 0,1 --error 0,1
remove_file $outfile; remove_file $outfile;
......
#
# Table elimination (MWL#17) tests
#
--disable_warnings
drop table if exists t0, t1, t2, t3;
drop view if exists v1, v2;
--enable_warnings
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);
--echo # This will be eliminated:
explain select t1.a from t1 left join t2 on t2.a=t1.a;
explain extended select t1.a from t1 left join t2 on t2.a=t1.a;
select t1.a from t1 left join t2 on t2.a=t1.a;
--echo # 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;
--echo # 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;
--echo # 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;
--echo # 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;
--echo # 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;
--echo # 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;
--echo # Elimination when done within an outer join nest:
explain extended
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;
--echo # Elimination with aggregate functions
explain select count(*) from t1 left join t2 on t2.a=t1.a;
explain select count(1) from t1 left join t2 on t2.a=t1.a;
explain select count(1) from t1 left join t2 on t2.a=t1.a group by t1.a;
--echo This must not use elimination:
explain select count(1) from t1 left join t2 on t2.a=t1.a group by t2.a;
drop table t0, t1, t2, t3;
# This will stand for elim_facts
create table t0 ( id integer, primary key (id));
# Attribute1, non-versioned
create table t1 (
id integer,
attr1 integer,
primary key (id),
key (attr1)
);
# Attribute2, time-versioned
create table t2 (
id integer,
attr2 integer,
fromdate date,
primary key (id, fromdate),
key (attr2,fromdate)
);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
insert into t0 select A.id + 10*B.id from t0 A, t0 B where B.id > 0;
insert into t1 select id, id from t0;
insert into t2 select id, id, date_add('2009-06-22', interval id day) from t0;
insert into t2 select id, id+1, date_add('2008-06-22', interval id day) from t0;
create view v1 as
select
F.id, A1.attr1, A2.attr2
from
t0 F
left join t1 A1 on A1.id=F.id
left join t2 A2 on A2.id=F.id and
A2.fromdate=(select MAX(fromdate) from
t2 where id=A2.id);
create view v2 as
select
F.id, A1.attr1, A2.attr2
from
t0 F
left join t1 A1 on A1.id=F.id
left join t2 A2 on A2.id=F.id and
A2.fromdate=(select MAX(fromdate) from
t2 where id=F.id);
--echo This should use one table:
explain select id from v1 where id=2;
--echo This should use one table:
explain extended select id from v1 where id in (1,2,3,4);
--echo This should use facts and A1 tables:
explain extended select id from v1 where attr1 between 12 and 14;
--echo This should use facts, A2 and its subquery:
explain extended select id from v1 where attr2 between 12 and 14;
# Repeat for v2:
--echo This should use one table:
explain select id from v2 where id=2;
--echo This should use one table:
explain extended select id from v2 where id in (1,2,3,4);
--echo This should use facts and A1 tables:
explain extended select id from v2 where attr1 between 12 and 14;
--echo This should use facts, A2 and its subquery:
explain extended select id from v2 where attr2 between 12 and 14;
drop view v1, v2;
drop table t0, t1, t2;
#
# Tests for the code that uses t.keypartX=func(t.keypartY) equalities to
# make table elimination inferences
#
create table t1 (a int);
insert into t1 values (0),(1),(2),(3);
create table t2 (pk1 int, pk2 int, pk3 int, col int, primary key(pk1, pk2, pk3));
insert into t2 select a,a,a,a from t1;
--echo This must use only t1:
explain select t1.* from t1 left join t2 on t2.pk1=t1.a and
t2.pk2=t2.pk1+1 and
t2.pk3=t2.pk2+1;
--echo This must use only t1:
explain select t1.* from t1 left join t2 on t2.pk1=t1.a and
t2.pk3=t2.pk1+1 and
t2.pk2=t2.pk3+1;
--echo This must use both:
explain select t1.* from t1 left join t2 on t2.pk1=t1.a and
t2.pk3=t2.pk1+1 and
t2.pk2=t2.pk3+t2.col;
--echo This must use only t1:
explain select t1.* from t1 left join t2 on t2.pk2=t1.a and
t2.pk1=t2.pk2+1 and
t2.pk3=t2.pk1;
drop table t1, t2;
...@@ -703,3 +703,73 @@ ...@@ -703,3 +703,73 @@
fun:malloc fun:malloc
fun:inet_ntoa fun:inet_ntoa
} }
#
# Some problem inside glibc on Ubuntu 9.04, x86 (but not amd64):
#
# ==5985== 19 bytes in 1 blocks are still reachable in loss record 1 of 6
# ==5985== at 0x7AF3FDE: malloc (vg_replace_malloc.c:207)
# ... 11,12, or 13 functions w/o symbols ...
# ==5985== by 0x8717185: nptl_pthread_exit_hack_handler (my_thr_init.c:55)
#
# Since valgrind 3.3.0 doesn't support '...' multi-function pattern, using
# multiple suppressions:
#
{
Mem loss inside nptl_pthread_exit_hack_handler
Memcheck:Leak
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:nptl_pthread_exit_hack_handler
}
{
Mem loss inside nptl_pthread_exit_hack_handler
Memcheck:Leak
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:nptl_pthread_exit_hack_handler
}
{
Mem loss inside nptl_pthread_exit_hack_handler
Memcheck:Leak
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:*
fun:nptl_pthread_exit_hack_handler
}
#!@PERL@
# Test of table elimination feature
use Cwd;
use DBI;
use Getopt::Long;
use Benchmark;
$opt_loop_count=100000;
$opt_medium_loop_count=10000;
$opt_small_loop_count=100;
$pwd = cwd(); $pwd = "." if ($pwd eq '');
require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
if ($opt_small_test)
{
$opt_loop_count/=10;
$opt_medium_loop_count/=10;
$opt_small_loop_count/=10;
}
print "Testing table elimination feature\n";
print "The test table has $opt_loop_count rows.\n\n";
# A query to get the recent versions of all attributes:
$select_current_full_facts="
select
F.id, A1.attr1, A2.attr2
from
elim_facts F
left join elim_attr1 A1 on A1.id=F.id
left join elim_attr2 A2 on A2.id=F.id and
A2.fromdate=(select MAX(fromdate) from
elim_attr2 where id=A2.id);
";
$select_current_full_facts="
select
F.id, A1.attr1, A2.attr2
from
elim_facts F
left join elim_attr1 A1 on A1.id=F.id
left join elim_attr2 A2 on A2.id=F.id and
A2.fromdate=(select MAX(fromdate) from
elim_attr2 where id=F.id);
";
# TODO: same as above but for some given date also?
# TODO:
####
#### Connect and start timeing
####
$dbh = $server->connect();
$start_time=new Benchmark;
####
#### Create needed tables
####
goto select_test if ($opt_skip_create);
print "Creating tables\n";
$dbh->do("drop table elim_facts" . $server->{'drop_attr'});
$dbh->do("drop table elim_attr1" . $server->{'drop_attr'});
$dbh->do("drop table elim_attr2" . $server->{'drop_attr'});
# The facts table
do_many($dbh,$server->create("elim_facts",
["id integer"],
["primary key (id)"]));
# Attribute1, non-versioned
do_many($dbh,$server->create("elim_attr1",
["id integer",
"attr1 integer"],
["primary key (id)",
"key (attr1)"]));
# Attribute2, time-versioned
do_many($dbh,$server->create("elim_attr2",
["id integer",
"attr2 integer",
"fromdate date"],
["primary key (id, fromdate)",
"key (attr2,fromdate)"]));
#NOTE: ignoring: if ($limits->{'views'})
$dbh->do("drop view elim_current_facts");
$dbh->do("create view elim_current_facts as $select_current_full_facts");
if ($opt_lock_tables)
{
do_query($dbh,"LOCK TABLES elim_facts, elim_attr1, elim_attr2 WRITE");
}
if ($opt_fast && defined($server->{vacuum}))
{
$server->vacuum(1,\$dbh);
}
####
#### Fill the facts table
####
$n_facts= $opt_loop_count;
if ($opt_fast && $server->{transactions})
{
$dbh->{AutoCommit} = 0;
}
print "Inserting $n_facts rows into facts table\n";
$loop_time=new Benchmark;
$query="insert into elim_facts values (";
for ($id=0; $id < $n_facts ; $id++)
{
do_query($dbh,"$query $id)");
}
if ($opt_fast && $server->{transactions})
{
$dbh->commit;
$dbh->{AutoCommit} = 1;
}
$end_time=new Benchmark;
print "Time to insert ($n_facts): " .
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
####
#### Fill attr1 table
####
if ($opt_fast && $server->{transactions})
{
$dbh->{AutoCommit} = 0;
}
print "Inserting $n_facts rows into attr1 table\n";
$loop_time=new Benchmark;
$query="insert into elim_attr1 values (";
for ($id=0; $id < $n_facts ; $id++)
{
$attr1= ceil(rand($n_facts));
do_query($dbh,"$query $id, $attr1)");
}
if ($opt_fast && $server->{transactions})
{
$dbh->commit;
$dbh->{AutoCommit} = 1;
}
$end_time=new Benchmark;
print "Time to insert ($n_facts): " .
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
####
#### Fill attr2 table
####
if ($opt_fast && $server->{transactions})
{
$dbh->{AutoCommit} = 0;
}
print "Inserting $n_facts rows into attr2 table\n";
$loop_time=new Benchmark;
for ($id=0; $id < $n_facts ; $id++)
{
# Two values for each $id - current one and obsolete one.
$attr1= ceil(rand($n_facts));
$query="insert into elim_attr2 values ($id, $attr1, now())";
do_query($dbh,$query);
$query="insert into elim_attr2 values ($id, $attr1, '2009-01-01')";
do_query($dbh,$query);
}
if ($opt_fast && $server->{transactions})
{
$dbh->commit;
$dbh->{AutoCommit} = 1;
}
$end_time=new Benchmark;
print "Time to insert ($n_facts): " .
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
####
#### Finalize the database population
####
if ($opt_lock_tables)
{
do_query($dbh,"UNLOCK TABLES");
}
if ($opt_fast && defined($server->{vacuum}))
{
$server->vacuum(0,\$dbh,["elim_facts", "elim_attr1", "elim_attr2"]);
}
if ($opt_lock_tables)
{
do_query($dbh,"LOCK TABLES elim_facts, elim_attr1, elim_attr2 WRITE");
}
####
#### Do some selects on the table
####
select_test:
#
# The selects will be:
# - N pk-lookups with all attributes
# - pk-attribute-based lookup
# - latest-attribute value based lookup.
###
### Bare facts select:
###
print "testing bare facts facts table\n";
$loop_time=new Benchmark;
$rows=0;
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$val= ceil(rand($n_facts));
$rows+=fetch_all_rows($dbh,"select * from elim_facts where id=$val");
}
$count=$i;
$end_time=new Benchmark;
print "time for select_bare_facts ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
###
### Full facts select, no elimination:
###
print "testing full facts facts table\n";
$loop_time=new Benchmark;
$rows=0;
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$val= rand($n_facts);
$rows+=fetch_all_rows($dbh,"select * from elim_current_facts where id=$val");
}
$count=$i;
$end_time=new Benchmark;
print "time for select_two_attributes ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
###
### Now with elimination: select only only one fact
###
print "testing selection of one attribute\n";
$loop_time=new Benchmark;
$rows=0;
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$val= rand($n_facts);
$rows+=fetch_all_rows($dbh,"select id, attr1 from elim_current_facts where id=$val");
}
$count=$i;
$end_time=new Benchmark;
print "time for select_one_attribute ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
###
### Now with elimination: select only only one fact
###
print "testing selection of one attribute\n";
$loop_time=new Benchmark;
$rows=0;
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$val= rand($n_facts);
$rows+=fetch_all_rows($dbh,"select id, attr2 from elim_current_facts where id=$val");
}
$count=$i;
$end_time=new Benchmark;
print "time for select_one_attribute ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
###
### TODO...
###
;
####
#### End of benchmark
####
if ($opt_lock_tables)
{
do_query($dbh,"UNLOCK TABLES");
}
if (!$opt_skip_delete)
{
do_query($dbh,"drop table elim_facts, elim_attr1, elim_attr2" . $server->{'drop_attr'});
}
if ($opt_fast && defined($server->{vacuum}))
{
$server->vacuum(0,\$dbh);
}
$dbh->disconnect; # close connection
end_benchmark($start_time);
...@@ -73,7 +73,7 @@ ADD_EXECUTABLE(mysqld ...@@ -73,7 +73,7 @@ ADD_EXECUTABLE(mysqld
partition_info.cc rpl_utility.cc rpl_injector.cc sql_locale.cc partition_info.cc rpl_utility.cc rpl_injector.cc sql_locale.cc
rpl_rli.cc rpl_mi.cc sql_servers.cc rpl_rli.cc rpl_mi.cc sql_servers.cc
sql_connect.cc scheduler.cc sql_connect.cc scheduler.cc
sql_profile.cc event_parse_data.cc sql_profile.cc event_parse_data.cc opt_table_elimination.cc
${PROJECT_SOURCE_DIR}/sql/sql_yacc.cc ${PROJECT_SOURCE_DIR}/sql/sql_yacc.cc
${PROJECT_SOURCE_DIR}/sql/sql_yacc.h ${PROJECT_SOURCE_DIR}/sql/sql_yacc.h
${PROJECT_SOURCE_DIR}/include/mysqld_error.h ${PROJECT_SOURCE_DIR}/include/mysqld_error.h
......
...@@ -121,7 +121,8 @@ mysqld_SOURCES = sql_lex.cc sql_handler.cc sql_partition.cc \ ...@@ -121,7 +121,8 @@ mysqld_SOURCES = sql_lex.cc sql_handler.cc sql_partition.cc \
event_queue.cc event_db_repository.cc events.cc \ event_queue.cc event_db_repository.cc events.cc \
sql_plugin.cc sql_binlog.cc \ sql_plugin.cc sql_binlog.cc \
sql_builtin.cc sql_tablespace.cc partition_info.cc \ sql_builtin.cc sql_tablespace.cc partition_info.cc \
sql_servers.cc event_parse_data.cc sql_servers.cc event_parse_data.cc \
opt_table_elimination.cc
nodist_mysqld_SOURCES = mini_client_errors.c pack.c client.c my_time.c my_user.c nodist_mysqld_SOURCES = mini_client_errors.c pack.c client.c my_time.c my_user.c
......
...@@ -1915,6 +1915,15 @@ void Item_field::reset_field(Field *f) ...@@ -1915,6 +1915,15 @@ void Item_field::reset_field(Field *f)
name= (char*) f->field_name; name= (char*) f->field_name;
} }
bool Item_field::check_column_usage_processor(uchar *arg)
{
Field_enumerator *fe= (Field_enumerator*)arg;
fe->see_field(field);
return FALSE;
}
const char *Item_ident::full_name() const const char *Item_ident::full_name() const
{ {
char *tmp; char *tmp;
...@@ -3380,7 +3389,7 @@ static void mark_as_dependent(THD *thd, SELECT_LEX *last, SELECT_LEX *current, ...@@ -3380,7 +3389,7 @@ static void mark_as_dependent(THD *thd, SELECT_LEX *last, SELECT_LEX *current,
/* store pointer on SELECT_LEX from which item is dependent */ /* store pointer on SELECT_LEX from which item is dependent */
if (mark_item) if (mark_item)
mark_item->depended_from= last; mark_item->depended_from= last;
current->mark_as_dependent(last); current->mark_as_dependent(last, resolved_item);
if (thd->lex->describe & DESCRIBE_EXTENDED) if (thd->lex->describe & DESCRIBE_EXTENDED)
{ {
char warn_buff[MYSQL_ERRMSG_SIZE]; char warn_buff[MYSQL_ERRMSG_SIZE];
......
...@@ -731,7 +731,11 @@ public: ...@@ -731,7 +731,11 @@ public:
virtual bool val_bool_result() { return val_bool(); } virtual bool val_bool_result() { return val_bool(); }
virtual bool is_null_result() { return is_null(); } virtual bool is_null_result() { return is_null(); }
/* bit map of tables used by item */ /*
Bitmap of tables used by item
(note: if you need to check dependencies on individual columns, check out
check_column_usage_processor)
*/
virtual table_map used_tables() const { return (table_map) 0L; } virtual table_map used_tables() const { return (table_map) 0L; }
/* /*
Return table map of tables that can't be NULL tables (tables that are Return table map of tables that can't be NULL tables (tables that are
...@@ -888,6 +892,8 @@ public: ...@@ -888,6 +892,8 @@ public:
virtual bool reset_query_id_processor(uchar *query_id_arg) { return 0; } virtual bool reset_query_id_processor(uchar *query_id_arg) { return 0; }
virtual bool is_expensive_processor(uchar *arg) { return 0; } virtual bool is_expensive_processor(uchar *arg) { return 0; }
virtual bool register_field_in_read_map(uchar *arg) { return 0; } virtual bool register_field_in_read_map(uchar *arg) { return 0; }
virtual bool check_column_usage_processor(uchar *arg) { return 0; }
virtual bool mark_as_eliminated_processor(uchar *arg) { return 0; }
/* /*
Check if a partition function is allowed Check if a partition function is allowed
SYNOPSIS SYNOPSIS
...@@ -1012,6 +1018,14 @@ public: ...@@ -1012,6 +1018,14 @@ public:
}; };
/* Data for Item::check_column_usage_processor */
class Field_enumerator
{
public:
virtual void see_field(Field *field)= 0;
virtual ~Field_enumerator() {}; /* Shut up compiler warning */
};
class sp_head; class sp_head;
...@@ -1477,6 +1491,7 @@ public: ...@@ -1477,6 +1491,7 @@ public:
bool find_item_in_field_list_processor(uchar *arg); bool find_item_in_field_list_processor(uchar *arg);
bool register_field_in_read_map(uchar *arg); bool register_field_in_read_map(uchar *arg);
bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bool check_column_usage_processor(uchar *arg);
void cleanup(); void cleanup();
bool result_as_longlong() bool result_as_longlong()
{ {
...@@ -2203,6 +2218,10 @@ public: ...@@ -2203,6 +2218,10 @@ public:
if (!depended_from) if (!depended_from)
(*ref)->update_used_tables(); (*ref)->update_used_tables();
} }
bool const_item() const
{
return (*ref)->const_item();
}
table_map not_null_tables() const { return (*ref)->not_null_tables(); } table_map not_null_tables() const { return (*ref)->not_null_tables(); }
void set_result_field(Field *field) { result_field= field; } void set_result_field(Field *field) { result_field= field; }
bool is_result_field() { return 1; } bool is_result_field() { return 1; }
......
...@@ -39,7 +39,7 @@ inline Item * and_items(Item* cond, Item *item) ...@@ -39,7 +39,7 @@ inline Item * and_items(Item* cond, Item *item)
Item_subselect::Item_subselect(): Item_subselect::Item_subselect():
Item_result_field(), value_assigned(0), thd(0), substitution(0), Item_result_field(), value_assigned(0), thd(0), substitution(0),
engine(0), old_engine(0), used_tables_cache(0), have_to_be_excluded(0), engine(0), old_engine(0), used_tables_cache(0), have_to_be_excluded(0),
const_item_cache(1), engine_changed(0), changed(0), is_correlated(FALSE) const_item_cache(1), in_fix_fields(0), engine_changed(0), changed(0), is_correlated(FALSE)
{ {
with_subselect= 1; with_subselect= 1;
reset(); reset();
...@@ -151,10 +151,14 @@ bool Item_subselect::fix_fields(THD *thd_param, Item **ref) ...@@ -151,10 +151,14 @@ bool Item_subselect::fix_fields(THD *thd_param, Item **ref)
DBUG_ASSERT(fixed == 0); DBUG_ASSERT(fixed == 0);
engine->set_thd((thd= thd_param)); engine->set_thd((thd= thd_param));
if (!in_fix_fields)
refers_to.empty();
eliminated= FALSE;
if (check_stack_overrun(thd, STACK_MIN_SIZE, (uchar*)&res)) if (check_stack_overrun(thd, STACK_MIN_SIZE, (uchar*)&res))
return TRUE; return TRUE;
in_fix_fields++;
res= engine->prepare(); res= engine->prepare();
// all transformation is done (used by prepared statements) // all transformation is done (used by prepared statements)
...@@ -181,12 +185,14 @@ bool Item_subselect::fix_fields(THD *thd_param, Item **ref) ...@@ -181,12 +185,14 @@ bool Item_subselect::fix_fields(THD *thd_param, Item **ref)
if (!(*ref)->fixed) if (!(*ref)->fixed)
ret= (*ref)->fix_fields(thd, ref); ret= (*ref)->fix_fields(thd, ref);
thd->where= save_where; thd->where= save_where;
in_fix_fields--;
return ret; return ret;
} }
// Is it one field subselect? // Is it one field subselect?
if (engine->cols() > max_columns) if (engine->cols() > max_columns)
{ {
my_error(ER_OPERAND_COLUMNS, MYF(0), 1); my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
in_fix_fields--;
return TRUE; return TRUE;
} }
fix_length_and_dec(); fix_length_and_dec();
...@@ -203,11 +209,30 @@ bool Item_subselect::fix_fields(THD *thd_param, Item **ref) ...@@ -203,11 +209,30 @@ bool Item_subselect::fix_fields(THD *thd_param, Item **ref)
fixed= 1; fixed= 1;
err: err:
in_fix_fields--;
thd->where= save_where; thd->where= save_where;
return res; return res;
} }
bool Item_subselect::check_column_usage_processor(uchar *arg)
{
List_iterator<Item> it(refers_to);
Item *item;
while ((item= it++))
{
if (item->walk(&Item::check_column_usage_processor,FALSE, arg))
return TRUE;
}
return FALSE;
}
bool Item_subselect::mark_as_eliminated_processor(uchar *arg)
{
eliminated= TRUE;
return FALSE;
}
bool Item_subselect::walk(Item_processor processor, bool walk_subquery, bool Item_subselect::walk(Item_processor processor, bool walk_subquery,
uchar *argument) uchar *argument)
{ {
...@@ -225,6 +250,7 @@ bool Item_subselect::walk(Item_processor processor, bool walk_subquery, ...@@ -225,6 +250,7 @@ bool Item_subselect::walk(Item_processor processor, bool walk_subquery,
if (lex->having && (lex->having)->walk(processor, walk_subquery, if (lex->having && (lex->having)->walk(processor, walk_subquery,
argument)) argument))
return 1; return 1;
/* TODO: why does this walk WHERE/HAVING but not ON expressions of outer joins? */
while ((item=li++)) while ((item=li++))
{ {
......
...@@ -52,8 +52,16 @@ protected: ...@@ -52,8 +52,16 @@ protected:
bool have_to_be_excluded; bool have_to_be_excluded;
/* cache of constant state */ /* cache of constant state */
bool const_item_cache; bool const_item_cache;
public: public:
/*
References from inside the subquery to the select that this predicate is
in. References to parent selects not included.
*/
List<Item> refers_to;
int in_fix_fields;
bool eliminated;
/* changed engine indicator */ /* changed engine indicator */
bool engine_changed; bool engine_changed;
/* subquery is transformed */ /* subquery is transformed */
...@@ -126,6 +134,8 @@ public: ...@@ -126,6 +134,8 @@ public:
virtual void reset_value_registration() {} virtual void reset_value_registration() {}
enum_parsing_place place() { return parsing_place; } enum_parsing_place place() { return parsing_place; }
bool walk(Item_processor processor, bool walk_subquery, uchar *arg); bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
bool mark_as_eliminated_processor(uchar *arg);
bool check_column_usage_processor(uchar *arg);
/** /**
Get the SELECT_LEX structure associated with this Item. Get the SELECT_LEX structure associated with this Item.
......
...@@ -350,7 +350,7 @@ bool Item_sum::register_sum_func(THD *thd, Item **ref) ...@@ -350,7 +350,7 @@ bool Item_sum::register_sum_func(THD *thd, Item **ref)
sl= sl->master_unit()->outer_select() ) sl= sl->master_unit()->outer_select() )
sl->master_unit()->item->with_sum_func= 1; sl->master_unit()->item->with_sum_func= 1;
} }
thd->lex->current_select->mark_as_dependent(aggr_sel); thd->lex->current_select->mark_as_dependent(aggr_sel, NULL);
return FALSE; return FALSE;
} }
...@@ -542,11 +542,6 @@ void Item_sum::update_used_tables () ...@@ -542,11 +542,6 @@ void Item_sum::update_used_tables ()
args[i]->update_used_tables(); args[i]->update_used_tables();
used_tables_cache|= args[i]->used_tables(); used_tables_cache|= args[i]->used_tables();
} }
used_tables_cache&= PSEUDO_TABLE_BITS;
/* the aggregate function is aggregated into its local context */
used_tables_cache |= (1 << aggr_sel->join->tables) - 1;
} }
} }
......
...@@ -255,6 +255,12 @@ protected: ...@@ -255,6 +255,12 @@ protected:
*/ */
Item **orig_args, *tmp_orig_args[2]; Item **orig_args, *tmp_orig_args[2];
table_map used_tables_cache; table_map used_tables_cache;
/*
TRUE <=> We've managed to calculate the value of this Item in
opt_sum_query(), hence it can be considered constant at all subsequent
steps.
*/
bool forced_const; bool forced_const;
public: public:
...@@ -341,6 +347,15 @@ public: ...@@ -341,6 +347,15 @@ public:
virtual const char *func_name() const= 0; virtual const char *func_name() const= 0;
virtual Item *result_item(Field *field) virtual Item *result_item(Field *field)
{ return new Item_field(field); } { return new Item_field(field); }
/*
Return bitmap of tables that are needed to evaluate the item.
The implementation takes into account the used strategy: items resolved
at optimization phase will report 0.
Items that depend on the number of join output records, but not columns
of any particular table (like COUNT(*)) will report 0 from used_tables(),
but will still return false from const_item().
*/
table_map used_tables() const { return used_tables_cache; } table_map used_tables() const { return used_tables_cache; }
void update_used_tables (); void update_used_tables ();
void cleanup() void cleanup()
......
/**
@file
@brief
Table Elimination Module
@defgroup Table_Elimination Table Elimination Module
@{
*/
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation // gcc: Class implementation
#endif
#include "mysql_priv.h"
#include "my_bit.h"
#include "sql_select.h"
/*
OVERVIEW
The module has one entry point - eliminate_tables() function, which one
needs to call (once) at some point before the join optimization.
eliminate_tables() operates over the JOIN structures. Logically, it
removes the right sides of outer join nests. Physically, it changes the
following members:
* Eliminated tables are marked as constant and moved to the front of the
join order.
* In addition to this, they are recorded in JOIN::eliminated_tables bitmap.
* Items that became disused because they were in the ON expression of an
eliminated outer join are notified by means of the Item tree walk which
calls Item::mark_as_eliminated_processor for every item
- At the moment the only Item that cares whether it was eliminated is
Item_subselect with its Item_subselect::eliminated flag which is used
by EXPLAIN code to check if the subquery should be shown in EXPLAIN.
Table elimination is redone on every PS re-execution.
*/
/*
An abstract structure that represents some entity that's being dependent on
some other entity.
*/
class Func_dep : public Sql_alloc
{
public:
enum {
FD_INVALID,
FD_EXPRESSION,
FD_FIELD,
FD_MULTI_EQUALITY,
FD_UNIQUE_KEY,
FD_TABLE,
FD_OUTER_JOIN
} type; /* Type of the object */
/*
Used to make a linked list of elements that became bound and thus can
make elements that depend on them bound, too.
*/
Func_dep *next;
bool bound; /* TRUE<=> The entity is considered bound */
Func_dep() : next(NULL), bound(FALSE) {}
};
class Field_dep;
class Table_dep;
class Outer_join_dep;
/*
A "tbl.column= expr" equality dependency. tbl.column depends on fields
used in expr.
*/
class Equality_dep : public Func_dep
{
public:
Field_dep *field;
Item *val;
/* Used during condition analysis only, similar to KEYUSE::level */
uint level;
/* Number of fields referenced from *val that are not yet 'bound' */
uint unknown_args;
};
/*
A table field. There is only one such object for any tblX.fieldY
- the field epends on its table and equalities
- expressions that use the field are its dependencies
*/
class Field_dep : public Func_dep
{
public:
Field_dep(Table_dep *table_arg, Field *field_arg) :
table(table_arg), field(field_arg)
{
type= Func_dep::FD_FIELD;
}
Table_dep *table; /* Table this field is from */
Field *field;
/*
Field_deps that belong to one table form a linked list. list members are
ordered by field_index
*/
Field_dep *next_table_field;
uint bitmap_offset; /* Offset of our part of the bitmap */
};
/*
A Unique key.
- Unique key depends on all of its components
- Key's table is its dependency
*/
class Key_dep: public Func_dep
{
public:
Key_dep(Table_dep *table_arg, uint keyno_arg, uint n_parts_arg) :
table(table_arg), keyno(keyno_arg), n_missing_keyparts(n_parts_arg),
next_table_key(NULL)
{
type= Func_dep::FD_UNIQUE_KEY;
}
Table_dep *table; /* Table this key is from */
uint keyno;
uint n_missing_keyparts;
/* Unique keys form a linked list, ordered by keyno */
Key_dep *next_table_key;
};
/*
A table.
- table depends on any of its unique keys
- has its fields and embedding outer join as dependency.
*/
class Table_dep : public Func_dep
{
public:
Table_dep(TABLE *table_arg) :
table(table_arg), fields(NULL), keys(NULL), outer_join_dep(NULL)
{
type= Func_dep::FD_TABLE;
}
TABLE *table;
Field_dep *fields; /* Ordered list of fields that belong to this table */
Key_dep *keys; /* Ordered list of Unique keys in this table */
Outer_join_dep *outer_join_dep; /* Innermost eliminable outer join we're in */
};
/*
An outer join nest that is subject to elimination
- it depends on all tables inside it
- has its parent outer join as dependency
*/
class Outer_join_dep: public Func_dep
{
public:
Outer_join_dep(TABLE_LIST *table_list_arg, table_map missing_tables_arg) :
table_list(table_list_arg), missing_tables(missing_tables_arg),
all_tables(missing_tables_arg), parent(NULL)
{
type= Func_dep::FD_OUTER_JOIN;
}
/*
Outer join we're representing. This can be a join nest or a one table that
is outer join'ed.
*/
TABLE_LIST *table_list;
/*
Tables within this outer join (and its descendants) that are not yet known
to be functionally dependent.
*/
table_map missing_tables;
/* All tables within this outer join and its descendants */
table_map all_tables;
/* Parent eliminable outer join, if any */
Outer_join_dep *parent;
};
/*
Table elimination context
*/
class Table_elimination
{
public:
Table_elimination(JOIN *join_arg) : join(join_arg)
{
bzero(table_deps, sizeof(table_deps));
}
JOIN *join;
/* Array of equality dependencies */
Equality_dep *equality_deps;
uint n_equality_deps; /* Number of elements in the array */
/* tablenr -> Table_dep* mapping. */
Table_dep *table_deps[MAX_KEY];
/* Outer joins that are candidates for elimination */
List<Outer_join_dep> oj_deps;
/* Bitmap of how expressions depend on bits */
MY_BITMAP expr_deps;
};
static
void build_eq_deps_for_cond(Table_elimination *te, Equality_dep **fdeps,
uint *and_level, Item *cond,
table_map usable_tables);
static
void add_eq_dep(Table_elimination *te,
Equality_dep **eq_dep, uint and_level,
Item_func *cond, Field *field,
bool eq_func, Item **value,
uint num_values, table_map usable_tables);
static
Equality_dep *merge_func_deps(Equality_dep *start, Equality_dep *new_fields,
Equality_dep *end, uint and_level);
static Table_dep *get_table_dep(Table_elimination *te, TABLE *table);
static Field_dep *get_field_dep(Table_elimination *te, Field *field);
void eliminate_tables(JOIN *join);
static void mark_as_eliminated(JOIN *join, TABLE_LIST *tbl);
#ifndef DBUG_OFF
static void dbug_print_deps(Table_elimination *te);
#endif
/*******************************************************************************************/
/*
Produce Eq_dep elements for given condition.
SYNOPSIS
build_eq_deps_for_cond()
te Table elimination context
fdeps INOUT Put produced equality conditions here
and_level INOUT AND-level (like in add_key_fields)
cond Condition to process
usable_tables Tables which fields we're interested in. That is,
Equality_dep represent "tbl.col=expr" and we'll
produce them only if tbl is in usable_tables.
DESCRIPTION
This function is modeled after add_key_fields()
*/
static
void build_eq_deps_for_cond(Table_elimination *te, Equality_dep **fdeps,
uint *and_level, Item *cond,
table_map usable_tables)
{
if (cond->type() == Item_func::COND_ITEM)
{
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
Equality_dep *org_key_fields= *fdeps;
/* AND/OR */
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
{
Item *item;
while ((item=li++))
{
build_eq_deps_for_cond(te, fdeps, and_level, item, usable_tables);
}
/*
TODO: inject here a "if we have {t.col=const AND t.col=smth_else}, then
remove the second part" logic.
*/
for (; org_key_fields != *fdeps ; org_key_fields++)
org_key_fields->level= *and_level;
}
else
{
(*and_level)++;
build_eq_deps_for_cond(te, fdeps, and_level, li++, usable_tables);
Item *item;
while ((item=li++))
{
Equality_dep *start_key_fields= *fdeps;
(*and_level)++;
build_eq_deps_for_cond(te, fdeps, and_level, item, usable_tables);
*fdeps= merge_func_deps(org_key_fields, start_key_fields, *fdeps,
++(*and_level));
}
}
return;
}
if (cond->type() != Item::FUNC_ITEM)
return;
Item_func *cond_func= (Item_func*) cond;
switch (cond_func->select_optimize()) {
case Item_func::OPTIMIZE_NONE:
break;
case Item_func::OPTIMIZE_KEY:
{
Item **values;
// BETWEEN, IN, NE
if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
{
values= cond_func->arguments()+1;
if (cond_func->functype() == Item_func::NE_FUNC &&
cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
values--;
DBUG_ASSERT(cond_func->functype() != Item_func::IN_FUNC ||
cond_func->argument_count() != 2);
add_eq_dep(te, fdeps, *and_level, cond_func,
((Item_field*)(cond_func->key_item()->real_item()))->field,
0, values,
cond_func->argument_count()-1,
usable_tables);
}
if (cond_func->functype() == Item_func::BETWEEN)
{
values= cond_func->arguments();
for (uint i= 1 ; i < cond_func->argument_count() ; i++)
{
Item_field *field_item;
if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
&&
!(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
{
field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
add_eq_dep(te, fdeps, *and_level, cond_func,
field_item->field, 0, values, 1, usable_tables);
}
}
}
break;
}
case Item_func::OPTIMIZE_OP:
{
bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
cond_func->functype() == Item_func::EQUAL_FUNC);
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
{
add_eq_dep(te, fdeps, *and_level, cond_func,
((Item_field*)(cond_func->arguments()[0])->real_item())->field,
equal_func,
cond_func->arguments()+1, 1, usable_tables);
}
if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
cond_func->functype() != Item_func::LIKE_FUNC &&
!(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
{
add_eq_dep(te, fdeps, *and_level, cond_func,
((Item_field*)(cond_func->arguments()[1])->real_item())->field,
equal_func,
cond_func->arguments(),1,usable_tables);
}
break;
}
case Item_func::OPTIMIZE_NULL:
/* column_name IS [NOT] NULL */
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
{
Item *tmp=new Item_null;
if (unlikely(!tmp)) // Should never be true
return;
add_eq_dep(te, fdeps, *and_level, cond_func,
((Item_field*)(cond_func->arguments()[0])->real_item())->field,
cond_func->functype() == Item_func::ISNULL_FUNC,
&tmp, 1, usable_tables);
}
break;
case Item_func::OPTIMIZE_EQUAL:
Item_equal *item_equal= (Item_equal *) cond;
Item *const_item= item_equal->get_const();
Item_equal_iterator it(*item_equal);
Item_field *item;
if (const_item)
{
/*
For each field field1 from item_equal consider the equality
field1=const_item as a condition allowing an index access of the table
with field1 by the keys value of field1.
*/
while ((item= it++))
{
add_eq_dep(te, fdeps, *and_level, cond_func, item->field,
TRUE, &const_item, 1, usable_tables);
}
}
else
{
/*
Consider all pairs of different fields included into item_equal.
For each of them (field1, field1) consider the equality
field1=field2 as a condition allowing an index access of the table
with field1 by the keys value of field2.
*/
Item_equal_iterator fi(*item_equal);
while ((item= fi++))
{
Field *field= item->field;
while ((item= it++))
{
if (!field->eq(item->field))
{
add_eq_dep(te, fdeps, *and_level, cond_func, field,
TRUE, (Item **) &item, 1, usable_tables);
}
}
it.rewind();
}
}
break;
}
}
/*
Perform an OR operation on two (adjacent) Equality_dep arrays.
SYNOPSIS
merge_func_deps()
start Start of left OR-part
new_fields Start of right OR-part
end End of right OR-part
and_level AND-level.
DESCRIPTION
This function is invoked for two adjacent arrays of Equality_dep elements:
$LEFT_PART $RIGHT_PART
+-----------------------+-----------------------+
start new_fields end
The goal is to produce an array which would correspnd to the combined
$LEFT_PART OR $RIGHT_PART
condition. This is achieved as follows: First, we apply distrubutive law:
(fdep_A_1 AND fdep_A_2 AND ...) OR (fdep_B_1 AND fdep_B_2 AND ...) =
= AND_ij (fdep_A_[i] OR fdep_B_[j])
Then we walk over the obtained "fdep_A_[i] OR fdep_B_[j]" pairs, and
- Discard those that that have left and right part referring to different
columns. We can't infer anything useful from "col1=expr1 OR col2=expr2".
- When left and right parts refer to the same column, we check if they are
essentially the same.
= If they are the same, we keep one copy
"t.col=expr OR t.col=expr" -> "t.col=expr
= if they are different , then we discard both
"t.col=expr1 OR t.col=expr2" -> (nothing useful)
(no per-table or for-index FUNC_DEPS exist yet at this phase).
See also merge_key_fields().
RETURN
End of the result array
*/
static
Equality_dep *merge_func_deps(Equality_dep *start, Equality_dep *new_fields,
Equality_dep *end, uint and_level)
{
if (start == new_fields)
return start; // Impossible or
if (new_fields == end)
return start; // No new fields, skip all
Equality_dep *first_free=new_fields;
for (; new_fields != end ; new_fields++)
{
for (Equality_dep *old=start ; old != first_free ; old++)
{
/*
TODO: does it make sense to attempt to merging multiple-equalities?
A: YES.
(a=b=c) OR (a=b=d) produce "a=b".
QQ:
What to use for merging? Trivial N*M algorithm or pre-sort and then
merge ordered sequences?
*/
if (old->field == new_fields->field)
{
if (!new_fields->val->const_item())
{
/*
If the value matches, we can use the key reference.
If not, we keep it until we have examined all new values
*/
if (old->val->eq(new_fields->val, old->field->field->binary()))
{
old->level= and_level;
}
}
else if (old->val->eq_by_collation(new_fields->val,
old->field->field->binary(),
old->field->field->charset()))
{
old->level= and_level;
}
else
{
/* The expressions are different. */
if (old == --first_free) // If last item
break;
*old= *first_free; // Remove old value
old--; // Retry this value
}
}
}
}
/*
Ok, the results are within the [start, first_free) range, and the useful
elements have level==and_level. Now, lets remove all unusable elements:
*/
for (Equality_dep *old=start ; old != first_free ;)
{
if (old->level != and_level)
{ // Not used in all levels
if (old == --first_free)
break;
*old= *first_free; // Remove old value
continue;
}
old++;
}
return first_free;
}
/*
Add an Equality_dep element for a given predicate, if applicable
DESCRIPTION
This function is modeled after add_key_field().
*/
static
void add_eq_dep(Table_elimination *te, Equality_dep **eq_dep,
uint and_level, Item_func *cond, Field *field,
bool eq_func, Item **value, uint num_values,
table_map usable_tables)
{
if (!(field->table->map & usable_tables))
return;
for (uint i=0; i<num_values; i++)
{
if ((value[i])->used_tables() & RAND_TABLE_BIT)
return;
}
/*
Save the following cases:
Field op constant
Field LIKE constant where constant doesn't start with a wildcard
Field = field2 where field2 is in a different table
Field op formula
Field IS NULL
Field IS NOT NULL
Field BETWEEN ...
Field IN ...
*/
/*
We can't always use indexes when comparing a string index to a
number. cmp_type() is checked to allow compare of dates to numbers.
eq_func is NEVER true when num_values > 1
*/
if (!eq_func)
{
/*
Additional optimization: if we're processing "t.key BETWEEN c1 AND c1"
then proceed as if we were processing "t.key = c1".
*/
if ((cond->functype() != Item_func::BETWEEN) ||
((Item_func_between*) cond)->negated ||
!value[0]->eq(value[1], field->binary()))
return;
eq_func= TRUE;
}
if (field->result_type() == STRING_RESULT)
{
if ((*value)->result_type() != STRING_RESULT)
{
if (field->cmp_type() != (*value)->result_type())
return;
}
else
{
/*
We can't use indexes if the effective collation
of the operation differ from the field collation.
*/
if (field->cmp_type() == STRING_RESULT &&
((Field_str*)field)->charset() != cond->compare_collation())
return;
}
}
DBUG_ASSERT(eq_func);
/* Store possible eq field */
(*eq_dep)->type= Func_dep::FD_EXPRESSION; //psergey-todo;
(*eq_dep)->field= get_field_dep(te, field);
(*eq_dep)->val= *value;
(*eq_dep)->level= and_level;
(*eq_dep)++;
}
/*
Get a Table_dep object for the given table, creating it if necessary.
*/
static Table_dep *get_table_dep(Table_elimination *te, TABLE *table)
{
Table_dep *tbl_dep= new Table_dep(table);
Key_dep **key_list= &(tbl_dep->keys);
/* Add dependencies for unique keys */
for (uint i=0; i < table->s->keys; i++)
{
KEY *key= table->key_info + i;
if ((key->flags & (HA_NOSAME | HA_END_SPACE_KEY)) == HA_NOSAME)
{
Key_dep *key_dep= new Key_dep(tbl_dep, i, key->key_parts);
*key_list= key_dep;
key_list= &(key_dep->next_table_key);
}
}
return te->table_deps[table->tablenr] = tbl_dep;
}
/*
Get a Field_dep object for the given field, creating it if necessary
*/
static Field_dep *get_field_dep(Table_elimination *te, Field *field)
{
TABLE *table= field->table;
Table_dep *tbl_dep;
/* First, get the table*/
if (!(tbl_dep= te->table_deps[table->tablenr]))
tbl_dep= get_table_dep(te, table);
/* Try finding the field in field list */
Field_dep **pfield= &(tbl_dep->fields);
while (*pfield && (*pfield)->field->field_index < field->field_index)
{
pfield= &((*pfield)->next_table_field);
}
if (*pfield && (*pfield)->field->field_index == field->field_index)
return *pfield;
/* Create the field and insert it in the list */
Field_dep *new_field= new Field_dep(tbl_dep, field);
new_field->next_table_field= *pfield;
*pfield= new_field;
return new_field;
}
/*
Create an Outer_join_dep object for the given outer join
DESCRIPTION
Outer_join_dep objects for children (or further descendants) are always
created before the parents.
*/
static
Outer_join_dep *get_outer_join_dep(Table_elimination *te,
TABLE_LIST *outer_join, table_map deps_map)
{
Outer_join_dep *oj_dep;
oj_dep= new Outer_join_dep(outer_join, deps_map);
/*
Collect a bitmap fo tables that we depend on, and also set parent pointer
for descendant outer join elements.
*/
Table_map_iterator it(deps_map);
int idx;
while ((idx= it.next_bit()) != Table_map_iterator::BITMAP_END)
{
Table_dep *table_dep;
if (!(table_dep= te->table_deps[idx]))
{
/*
We get here only when ON expression had no references to inner tables
and Table_map objects weren't created for them. This is a rare/
unimportant case so it's ok to do not too efficient searches.
*/
TABLE *table= NULL;
for (TABLE_LIST *tlist= te->join->select_lex->leaf_tables; tlist;
tlist=tlist->next_leaf)
{
if (tlist->table->tablenr == (uint)idx)
{
table=tlist->table;
break;
}
}
DBUG_ASSERT(table);
table_dep= get_table_dep(te, table);
}
/*
Walk from the table up to its embedding outer joins. The goal is to
find the least embedded outer join nest and set its parent pointer to
point to the newly created Outer_join_dep.
to set the pointer of its near
*/
if (!table_dep->outer_join_dep)
table_dep->outer_join_dep= oj_dep;
else
{
Outer_join_dep *oj= table_dep->outer_join_dep;
while (oj->parent)
oj= oj->parent;
oj->parent=oj_dep;
}
}
return oj_dep;
}
/*
Build functional dependency graph for elements of given join list
SYNOPSIS
collect_funcdeps_for_join_list()
te Table elimination context.
join_list Join list to work on
build_eq_deps TRUE <=> build Equality_dep elements for all
members of the join list, even if they cannot
be individually eliminated
tables_used_elsewhere Bitmap of tables that are referred to from
somewhere outside of this join list (e.g.
select list, HAVING, ON expressions of parent
joins, etc).
eliminable_tables INOUT Tables that can potentially be eliminated
(needed so we know for which tables to build
dependencies for)
eq_dep INOUT End of array of equality dependencies.
DESCRIPTION
.
*/
static void
collect_funcdeps_for_join_list(Table_elimination *te,
List<TABLE_LIST> *join_list,
bool build_eq_deps,
table_map tables_used_elsewhere,
table_map *eliminable_tables,
Equality_dep **eq_dep)
{
TABLE_LIST *tbl;
List_iterator<TABLE_LIST> it(*join_list);
table_map tables_used_on_left= 0;
while ((tbl= it++))
{
if (tbl->on_expr)
{
table_map outside_used_tables= tables_used_elsewhere |
tables_used_on_left;
bool eliminable;
table_map cur_map;
if (tbl->nested_join)
{
/* This is "... LEFT JOIN (join_nest) ON cond" */
cur_map= tbl->nested_join->used_tables;
eliminable= !(cur_map & outside_used_tables);
if (eliminable)
*eliminable_tables |= cur_map;
collect_funcdeps_for_join_list(te, &tbl->nested_join->join_list,
eliminable || build_eq_deps,
outside_used_tables,
eliminable_tables,
eq_dep);
}
else
{
/* This is "... LEFT JOIN tbl ON cond" */
cur_map= tbl->table->map;
eliminable= !(tbl->table->map & outside_used_tables);
*eliminable_tables |= cur_map;
}
if (eliminable || build_eq_deps)
{
// build comp_cond from ON expression
uint and_level=0;
build_eq_deps_for_cond(te, eq_dep, &and_level, tbl->on_expr,
*eliminable_tables);
}
if (eliminable)
te->oj_deps.push_back(get_outer_join_dep(te, tbl, cur_map));
tables_used_on_left |= tbl->on_expr->used_tables();
}
}
return;
}
/*
This is used to analyse expressions in "tbl.col=expr" dependencies so
that we can figure out which fields the expression depends on.
*/
class Field_dependency_setter : public Field_enumerator
{
public:
Field_dependency_setter(Table_elimination *te_arg): te(te_arg)
{}
void see_field(Field *field)
{
Table_dep *tbl_dep;
if ((tbl_dep= te->table_deps[field->table->tablenr]))
{
for (Field_dep *field_dep= tbl_dep->fields; field_dep;
field_dep= field_dep->next_table_field)
{
if (field->field_index == field_dep->field->field_index)
{
uint offs= field_dep->bitmap_offset + expr_offset;
if (!bitmap_is_set(&te->expr_deps, offs))
te->equality_deps[expr_offset].unknown_args++;
bitmap_set_bit(&te->expr_deps, offs);
return;
}
}
/*
We got here if didn't find this field. It's not a part of
a unique key, and/or there is no field=expr element for it.
Bump the dependency anyway, this will signal that this dependency
cannot be satisfied.
*/
te->equality_deps[expr_offset].unknown_args++;
}
}
Table_elimination *te;
/* Offset of the expression we're processing in the dependency bitmap */
uint expr_offset;
};
/*
Setup equality dependencies
SYNOPSIS
setup_equality_deps()
te Table elimination context
bound_deps_list OUT Start of linked list of elements that were found to
be bound (caller will use this to see if that
allows to declare further elements bound)
*/
static
bool setup_equality_deps(Table_elimination *te, Func_dep **bound_deps_list)
{
DBUG_ENTER("setup_equality_deps");
/*
Count Field_dep objects and assign each of them a unique bitmap_offset.
*/
uint offset= 0;
for (Table_dep **tbl_dep=te->table_deps;
tbl_dep < te->table_deps + MAX_TABLES;
tbl_dep++)
{
if (*tbl_dep)
{
for (Field_dep *field_dep= (*tbl_dep)->fields;
field_dep;
field_dep= field_dep->next_table_field)
{
field_dep->bitmap_offset= offset;
offset += te->n_equality_deps;
}
}
}
void *buf;
if (!(buf= current_thd->alloc(bitmap_buffer_size(offset))) ||
bitmap_init(&te->expr_deps, (my_bitmap_map*)buf, offset, FALSE))
{
DBUG_RETURN(TRUE);
}
bitmap_clear_all(&te->expr_deps);
/*
Analyze all "field=expr" dependencies, and have te->expr_deps encode
dependencies of expressions from fields.
Also collect a linked list of equalities that are bound.
*/
Func_dep *bound_dep= NULL;
Field_dependency_setter deps_setter(te);
for (Equality_dep *eq_dep= te->equality_deps;
eq_dep < te->equality_deps + te->n_equality_deps;
eq_dep++)
{
deps_setter.expr_offset= eq_dep - te->equality_deps;
eq_dep->unknown_args= 0;
eq_dep->val->walk(&Item::check_column_usage_processor, FALSE,
(uchar*)&deps_setter);
if (!eq_dep->unknown_args)
{
eq_dep->next= bound_dep;
bound_dep= eq_dep;
eq_dep->bound= TRUE;
}
}
*bound_deps_list= bound_dep;
DBUG_EXECUTE("test", dbug_print_deps(te); );
DBUG_RETURN(FALSE);
}
/*
Perform table elimination
SYNOPSIS
eliminate_tables()
join Join to work on
const_tbl_count INOUT Number of constant tables (this includes
eliminated tables)
const_tables INOUT Bitmap of constant tables
DESCRIPTION
This function is the entry point for table elimination.
The idea behind table elimination is that if we have an outer join:
SELECT * FROM t1 LEFT JOIN
(t2 JOIN t3) ON t3.primary_key=t1.col AND
t4.primary_key=t2.col
such that
1. columns of the inner tables are not used anywhere ouside the outer
join (not in WHERE, not in GROUP/ORDER BY clause, not in select list
etc etc), and
2. inner side of the outer join is guaranteed to produce at most one
record combination for each record combination of outer tables.
then the inner side of the outer join can be removed from the query.
This is because it will always produce one matching record (either a
real match or a NULL-complemented record combination), and since there
are no references to columns of the inner tables anywhere, it doesn't
matter which record combination it was.
This function primary handles checking #1. It collects a bitmap of
tables that are not used in select list/GROUP BY/ORDER BY/HAVING/etc and
thus can possibly be eliminated.
SIDE EFFECTS
See the OVERVIEW section at the top of this file.
*/
void eliminate_tables(JOIN *join)
{
THD* thd= join->thd;
Item *item;
table_map used_tables;
DBUG_ENTER("eliminate_tables");
DBUG_ASSERT(join->eliminated_tables == 0);
/* If there are no outer joins, we have nothing to eliminate: */
if (!join->outer_join)
DBUG_VOID_RETURN;
/* Find the tables that are referred to from WHERE/HAVING */
used_tables= (join->conds? join->conds->used_tables() : 0) |
(join->having? join->having->used_tables() : 0);
/* Add tables referred to from the select list */
List_iterator<Item> it(join->fields_list);
while ((item= it++))
used_tables |= item->used_tables();
/* Add tables referred to from ORDER BY and GROUP BY lists */
ORDER *all_lists[]= { join->order, join->group_list};
for (int i=0; i < 2; i++)
{
for (ORDER *cur_list= all_lists[i]; cur_list; cur_list= cur_list->next)
used_tables |= (*(cur_list->item))->used_tables();
}
if (join->select_lex == &thd->lex->select_lex)
{
/* Multi-table UPDATE and DELETE: don't eliminate the tables we modify: */
used_tables |= thd->table_map_for_update;
/* Multi-table UPDATE: don't eliminate tables referred from SET statement */
if (thd->lex->sql_command == SQLCOM_UPDATE_MULTI)
{
List_iterator<Item> it2(thd->lex->value_list);
while ((item= it2++))
used_tables |= item->used_tables();
}
}
table_map all_tables= join->all_tables_map();
if (all_tables & ~used_tables)
{
/* There are some tables that we probably could eliminate. Try it. */
Table_elimination te(join);
uint m= max(thd->lex->current_select->max_equal_elems,1);
uint max_elems= ((thd->lex->current_select->cond_count+1)*2 +
thd->lex->current_select->between_count)*m + 1 + 10;
if (!(te.equality_deps= new Equality_dep[max_elems]))
DBUG_VOID_RETURN;
Equality_dep *eq_deps_end= te.equality_deps;
table_map eliminable_tables= 0;
collect_funcdeps_for_join_list(&te, join->join_list,
FALSE,
used_tables,
&eliminable_tables,
&eq_deps_end);
te.n_equality_deps= eq_deps_end - te.equality_deps;
Func_dep *bound_dep;
setup_equality_deps(&te, &bound_dep);
/*
Run the wave.
All Func_dep-derived objects are divided into three classes:
- Those that have bound=FALSE
- Those that have bound=TRUE
- Those that have bound=TRUE and are in the list..
*/
while (bound_dep)
{
Func_dep *next= bound_dep->next;
//e= list.remove_first();
switch (bound_dep->type)
{
case Func_dep::FD_EXPRESSION:
{
/* It's a field=expr and we got to know the expr, so we know the field */
Equality_dep *eq_dep= (Equality_dep*)bound_dep;
if (!eq_dep->field->bound)
{
/* Mark as bound and add to the list */
eq_dep->field->bound= TRUE;
eq_dep->field->next= next;
next= eq_dep->field;
}
break;
}
case Func_dep::FD_FIELD:
{
/*
Field became known. Check out
- unique keys we belong to
- expressions that depend on us.
*/
Field_dep *field_dep= (Field_dep*)bound_dep;
for (Key_dep *key_dep= field_dep->table->keys; key_dep;
key_dep= key_dep->next_table_key)
{
DBUG_PRINT("info", ("key %s.%s is now bound",
key_dep->table->table->alias,
key_dep->table->table->key_info[key_dep->keyno].name));
if (field_dep->field->part_of_key.is_set(key_dep->keyno) &&
!key_dep->bound)
{
if (!--key_dep->n_missing_keyparts)
{
/* Mark as bound and add to the list */
key_dep->bound= TRUE;
key_dep->next= next;
next= key_dep;
}
}
}
/* Now, expressions */
for (uint i=0; i < te.n_equality_deps; i++)
{
if (bitmap_is_set(&te.expr_deps, field_dep->bitmap_offset + i))
{
Equality_dep* eq_dep= &te.equality_deps[i];
if (!--eq_dep->unknown_args)
{
/* Mark as bound and add to the list */
eq_dep->bound= TRUE;
eq_dep->next= next;
next= eq_dep;
}
}
}
break;
}
case Func_dep::FD_UNIQUE_KEY:
{
/* Unique key is known means the table is known */
Table_dep *table_dep=((Key_dep*)bound_dep)->table;
if (!table_dep->bound)
{
/* Mark as bound and add to the list */
table_dep->bound= TRUE;
table_dep->next= next;
next= table_dep;
}
break;
}
case Func_dep::FD_TABLE:
{
Table_dep *table_dep=(Table_dep*)bound_dep;
DBUG_PRINT("info", ("table %s is now bound",
table_dep->table->alias));
/*
Table is known means
- all its fields are known
- one more element in outer join nest is known
*/
for (Field_dep *field_dep= table_dep->fields; field_dep;
field_dep= field_dep->next_table_field)
{
if (!field_dep->bound)
{
/* Mark as bound and add to the list */
field_dep->bound= TRUE;
field_dep->next= next;
next= field_dep;
}
}
Outer_join_dep *outer_join_dep= table_dep->outer_join_dep;
if (!(outer_join_dep->missing_tables &= ~table_dep->table->map))
{
/* Mark as bound and add to the list */
outer_join_dep->bound= TRUE;
outer_join_dep->next= next;
next= outer_join_dep;
}
break;
}
case Func_dep::FD_OUTER_JOIN:
{
Outer_join_dep *outer_join_dep= (Outer_join_dep*)bound_dep;
mark_as_eliminated(te.join, outer_join_dep->table_list);
Outer_join_dep *parent= outer_join_dep->parent;
if (parent &&
!(parent->missing_tables &= ~outer_join_dep->all_tables))
{
/* Mark as bound and add to the list */
parent->bound= TRUE;
parent->next= next;
next= parent;
}
break;
}
case Func_dep::FD_MULTI_EQUALITY:
default:
DBUG_ASSERT(0);
}
bound_dep= next;
}
}
DBUG_VOID_RETURN;
}
/*
Mark one table or the whole join nest as eliminated.
*/
static void mark_as_eliminated(JOIN *join, TABLE_LIST *tbl)
{
TABLE *table;
/*
NOTE: there are TABLE_LIST object that have
tbl->table!= NULL && tbl->nested_join!=NULL and
tbl->table == tbl->nested_join->join_list->element(..)->table
*/
if (tbl->nested_join)
{
TABLE_LIST *child;
List_iterator<TABLE_LIST> it(tbl->nested_join->join_list);
while ((child= it++))
mark_as_eliminated(join, child);
}
else if ((table= tbl->table))
{
JOIN_TAB *tab= tbl->table->reginfo.join_tab;
if (!(join->const_table_map & tab->table->map))
{
DBUG_PRINT("info", ("Eliminated table %s", table->alias));
tab->type= JT_CONST;
join->eliminated_tables |= table->map;
join->const_table_map|= table->map;
set_position(join, join->const_tables++, tab, (KEYUSE*)0);
}
}
if (tbl->on_expr)
tbl->on_expr->walk(&Item::mark_as_eliminated_processor, FALSE, NULL);
}
#ifndef DBUG_OFF
static
void dbug_print_deps(Table_elimination *te)
{
DBUG_ENTER("dbug_print_deps");
DBUG_LOCK_FILE;
fprintf(DBUG_FILE,"deps {\n");
/* Start with printing equalities */
for (Equality_dep *eq_dep= te->equality_deps;
eq_dep != te->equality_deps + te->n_equality_deps; eq_dep++)
{
char buf[128];
String str(buf, sizeof(buf), &my_charset_bin);
str.length(0);
eq_dep->val->print(&str, QT_ORDINARY);
fprintf(DBUG_FILE, " equality%d: %s -> %s.%s\n",
eq_dep - te->equality_deps,
str.c_ptr(),
eq_dep->field->table->table->alias,
eq_dep->field->field->field_name);
}
fprintf(DBUG_FILE,"\n");
/* Then tables and their fields */
for (uint i=0; i < MAX_TABLES; i++)
{
Table_dep *table_dep;
if ((table_dep= te->table_deps[i]))
{
/* Print table */
fprintf(DBUG_FILE, " table %s\n", table_dep->table->alias);
/* Print fields */
for (Field_dep *field_dep= table_dep->fields; field_dep;
field_dep= field_dep->next_table_field)
{
fprintf(DBUG_FILE, " field %s.%s ->", table_dep->table->alias,
field_dep->field->field_name);
uint ofs= field_dep->bitmap_offset;
for (uint bit= ofs; bit < ofs + te->n_equality_deps; bit++)
{
if (bitmap_is_set(&te->expr_deps, bit))
fprintf(DBUG_FILE, " equality%d ", bit - ofs);
}
fprintf(DBUG_FILE, "\n");
}
}
}
fprintf(DBUG_FILE,"\n}\n");
DBUG_UNLOCK_FILE;
DBUG_VOID_RETURN;
}
#endif
/**
@} (end of group Table_Elimination)
*/
...@@ -93,6 +93,34 @@ public: ...@@ -93,6 +93,34 @@ public:
} }
}; };
/* An iterator to quickly walk over bits in unlonglong bitmap. */
class Table_map_iterator
{
ulonglong bmp;
uint no;
public:
Table_map_iterator(ulonglong t) : bmp(t), no(0) {}
int next_bit()
{
static const char last_bit[16]= {32, 0, 1, 0,
2, 0, 1, 0,
3, 0, 1, 0,
2, 0, 1, 0};
uint bit;
while ((bit= last_bit[bmp & 0xF]) == 32)
{
no += 4;
bmp= bmp >> 4;
if (!bmp)
return BITMAP_END;
}
bmp &= ~(1LL << bit);
return no + bit;
}
int operator++(int) { return next_bit(); }
enum { BITMAP_END= 64 };
};
template <> class Bitmap<64> template <> class Bitmap<64>
{ {
ulonglong map; ulonglong map;
...@@ -136,5 +164,10 @@ public: ...@@ -136,5 +164,10 @@ public:
my_bool operator==(const Bitmap<64>& map2) const { return map == map2.map; } my_bool operator==(const Bitmap<64>& map2) const { return map == map2.map; }
char *print(char *buf) const { longlong2str(map,buf,16); return buf; } char *print(char *buf) const { longlong2str(map,buf,16); return buf; }
ulonglong to_ulonglong() const { return map; } ulonglong to_ulonglong() const { return map; }
class Iterator : public Table_map_iterator
{
public:
Iterator(Bitmap<64> &bmp) : Table_map_iterator(bmp.map) {}
};
}; };
...@@ -1778,8 +1778,9 @@ void st_select_lex_unit::exclude_tree() ...@@ -1778,8 +1778,9 @@ void st_select_lex_unit::exclude_tree()
'last' should be reachable from this st_select_lex_node 'last' should be reachable from this st_select_lex_node
*/ */
void st_select_lex::mark_as_dependent(st_select_lex *last) void st_select_lex::mark_as_dependent(st_select_lex *last, Item *dependency)
{ {
SELECT_LEX *next_to_last;
/* /*
Mark all selects from resolved to 1 before select where was Mark all selects from resolved to 1 before select where was
found table as depended (of select where was found table) found table as depended (of select where was found table)
...@@ -1787,6 +1788,7 @@ void st_select_lex::mark_as_dependent(st_select_lex *last) ...@@ -1787,6 +1788,7 @@ void st_select_lex::mark_as_dependent(st_select_lex *last)
for (SELECT_LEX *s= this; for (SELECT_LEX *s= this;
s && s != last; s && s != last;
s= s->outer_select()) s= s->outer_select())
{
if (!(s->uncacheable & UNCACHEABLE_DEPENDENT)) if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
{ {
// Select is dependent of outer select // Select is dependent of outer select
...@@ -1802,8 +1804,12 @@ void st_select_lex::mark_as_dependent(st_select_lex *last) ...@@ -1802,8 +1804,12 @@ void st_select_lex::mark_as_dependent(st_select_lex *last)
sl->uncacheable|= UNCACHEABLE_UNITED; sl->uncacheable|= UNCACHEABLE_UNITED;
} }
} }
next_to_last= s;
}
is_correlated= TRUE; is_correlated= TRUE;
this->master_unit()->item->is_correlated= TRUE; this->master_unit()->item->is_correlated= TRUE;
if (dependency)
next_to_last->master_unit()->item->refers_to.push_back(dependency);
} }
bool st_select_lex_node::set_braces(bool value) { return 1; } bool st_select_lex_node::set_braces(bool value) { return 1; }
......
...@@ -743,7 +743,7 @@ public: ...@@ -743,7 +743,7 @@ public:
return master_unit()->return_after_parsing(); return master_unit()->return_after_parsing();
} }
void mark_as_dependent(st_select_lex *last); void mark_as_dependent(st_select_lex *last, Item *dependency);
bool set_braces(bool value); bool set_braces(bool value);
bool inc_in_sum_expr(); bool inc_in_sum_expr();
......
...@@ -60,7 +60,6 @@ static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse, ...@@ -60,7 +60,6 @@ static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
table_map table_map, SELECT_LEX *select_lex, table_map table_map, SELECT_LEX *select_lex,
st_sargable_param **sargables); st_sargable_param **sargables);
static int sort_keyuse(KEYUSE *a,KEYUSE *b); static int sort_keyuse(KEYUSE *a,KEYUSE *b);
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse, static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
table_map used_tables); table_map used_tables);
static bool choose_plan(JOIN *join,table_map join_tables); static bool choose_plan(JOIN *join,table_map join_tables);
...@@ -115,7 +114,7 @@ static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, ...@@ -115,7 +114,7 @@ static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
COND *conds, bool top); COND *conds, bool top);
static bool check_interleaving_with_nj(JOIN_TAB *next); static bool check_interleaving_with_nj(JOIN_TAB *next);
static void restore_prev_nj_state(JOIN_TAB *last); static void restore_prev_nj_state(JOIN_TAB *last);
static void reset_nj_counters(List<TABLE_LIST> *join_list); static uint reset_nj_counters(JOIN *join, List<TABLE_LIST> *join_list);
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
uint first_unused); uint first_unused);
...@@ -1012,7 +1011,7 @@ JOIN::optimize() ...@@ -1012,7 +1011,7 @@ JOIN::optimize()
DBUG_RETURN(1); DBUG_RETURN(1);
} }
reset_nj_counters(join_list); reset_nj_counters(this, join_list);
make_outerjoin_info(this); make_outerjoin_info(this);
/* /*
...@@ -2381,6 +2380,13 @@ mysql_select(THD *thd, Item ***rref_pointer_array, ...@@ -2381,6 +2380,13 @@ mysql_select(THD *thd, Item ***rref_pointer_array,
} }
else else
{ {
/*
When in EXPLAIN, delay deleting the joins so that they are still
available when we're producing EXPLAIN EXTENDED warning text.
*/
if (select_options & SELECT_DESCRIBE)
free_join= 0;
if (!(join= new JOIN(thd, fields, select_options, result))) if (!(join= new JOIN(thd, fields, select_options, result)))
DBUG_RETURN(TRUE); DBUG_RETURN(TRUE);
thd_proc_info(thd, "init"); thd_proc_info(thd, "init");
...@@ -2646,24 +2652,31 @@ make_join_statistics(JOIN *join, TABLE_LIST *tables_arg, COND *conds, ...@@ -2646,24 +2652,31 @@ make_join_statistics(JOIN *join, TABLE_LIST *tables_arg, COND *conds,
~outer_join, join->select_lex, &sargables)) ~outer_join, join->select_lex, &sargables))
goto error; goto error;
/* Read tables with 0 or 1 rows (system tables) */
join->const_table_map= 0; join->const_table_map= 0;
join->const_tables= const_count;
eliminate_tables(join);
const_count= join->const_tables;
found_const_table_map= join->const_table_map;
/* Read tables with 0 or 1 rows (system tables) */
for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count; for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
p_pos < p_end ; p_pos < p_end ;
p_pos++) p_pos++)
{ {
int tmp;
s= p_pos->table; s= p_pos->table;
s->type=JT_SYSTEM; if (! (s->table->map & join->eliminated_tables))
join->const_table_map|=s->table->map;
if ((tmp=join_read_const_table(s, p_pos)))
{ {
if (tmp > 0) int tmp;
goto error; // Fatal error s->type=JT_SYSTEM;
join->const_table_map|=s->table->map;
if ((tmp=join_read_const_table(s, p_pos)))
{
if (tmp > 0)
goto error; // Fatal error
}
else
found_const_table_map|= s->table->map;
} }
else
found_const_table_map|= s->table->map;
} }
/* loop until no more const tables are found */ /* loop until no more const tables are found */
...@@ -2688,7 +2701,8 @@ make_join_statistics(JOIN *join, TABLE_LIST *tables_arg, COND *conds, ...@@ -2688,7 +2701,8 @@ make_join_statistics(JOIN *join, TABLE_LIST *tables_arg, COND *conds,
substitution of a const table the key value happens to be null substitution of a const table the key value happens to be null
then we can state that there are no matches for this equi-join. then we can state that there are no matches for this equi-join.
*/ */
if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map) if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map &&
!(table->map & join->eliminated_tables))
{ {
/* /*
When performing an outer join operation if there are no matching rows When performing an outer join operation if there are no matching rows
...@@ -2965,6 +2979,26 @@ typedef struct key_field_t { ...@@ -2965,6 +2979,26 @@ typedef struct key_field_t {
This is called for OR between different levels. This is called for OR between different levels.
That is, the function operates on an array of KEY_FIELD elements which has
two parts:
$LEFT_PART $RIGHT_PART
+-----------------------+-----------------------+
start new_fields end
$LEFT_PART and $RIGHT_PART are arrays that have KEY_FIELD elements for two
parts of the OR condition. Our task is to produce an array of KEY_FIELD
elements that would correspond to "$LEFT_PART OR $RIGHT_PART".
The rules for combining elements are as follows:
(keyfieldA1 AND keyfieldA2 AND ...) OR (keyfieldB1 AND keyfieldB2 AND ...)=
AND_ij (keyfieldA_i OR keyfieldB_j)
We discard all (keyfieldA_i OR keyfieldB_j) that refer to different
fields. For those referring to the same field, the logic is as follows:
t.keycol=
To be able to do 'ref_or_null' we merge a comparison of a column To be able to do 'ref_or_null' we merge a comparison of a column
and 'column IS NULL' to one test. This is useful for sub select queries and 'column IS NULL' to one test. This is useful for sub select queries
that are internally transformed to something like:. that are internally transformed to something like:.
...@@ -3990,8 +4024,7 @@ add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab) ...@@ -3990,8 +4024,7 @@ add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
/** Save const tables first as used tables. */ /** Save const tables first as used tables. */
static void void set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
{ {
join->positions[idx].table= table; join->positions[idx].table= table;
join->positions[idx].key=key; join->positions[idx].key=key;
...@@ -4592,7 +4625,7 @@ choose_plan(JOIN *join, table_map join_tables) ...@@ -4592,7 +4625,7 @@ choose_plan(JOIN *join, table_map join_tables)
DBUG_ENTER("choose_plan"); DBUG_ENTER("choose_plan");
join->cur_embedding_map= 0; join->cur_embedding_map= 0;
reset_nj_counters(join->join_list); reset_nj_counters(join, join->join_list);
/* /*
if (SELECT_STRAIGHT_JOIN option is set) if (SELECT_STRAIGHT_JOIN option is set)
reorder tables so dependent tables come after tables they depend reorder tables so dependent tables come after tables they depend
...@@ -5757,6 +5790,7 @@ JOIN::make_simple_join(JOIN *parent, TABLE *tmp_table) ...@@ -5757,6 +5790,7 @@ JOIN::make_simple_join(JOIN *parent, TABLE *tmp_table)
tables= 1; tables= 1;
const_tables= 0; const_tables= 0;
const_table_map= 0; const_table_map= 0;
eliminated_tables= 0;
tmp_table_param.field_count= tmp_table_param.sum_func_count= tmp_table_param.field_count= tmp_table_param.sum_func_count=
tmp_table_param.func_count= 0; tmp_table_param.func_count= 0;
tmp_table_param.copy_field= tmp_table_param.copy_field_end=0; tmp_table_param.copy_field= tmp_table_param.copy_field_end=0;
...@@ -6021,7 +6055,7 @@ make_outerjoin_info(JOIN *join) ...@@ -6021,7 +6055,7 @@ make_outerjoin_info(JOIN *join)
} }
if (!tab->first_inner) if (!tab->first_inner)
tab->first_inner= nested_join->first_nested; tab->first_inner= nested_join->first_nested;
if (++nested_join->counter < nested_join->join_list.elements) if (++nested_join->counter < nested_join->n_tables)
break; break;
/* Table tab is the last inner table for nested join. */ /* Table tab is the last inner table for nested join. */
nested_join->first_nested->last_inner= tab; nested_join->first_nested->last_inner= tab;
...@@ -8575,6 +8609,8 @@ simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top) ...@@ -8575,6 +8609,8 @@ simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top)
conds= simplify_joins(join, &nested_join->join_list, conds, top); conds= simplify_joins(join, &nested_join->join_list, conds, top);
used_tables= nested_join->used_tables; used_tables= nested_join->used_tables;
not_null_tables= nested_join->not_null_tables; not_null_tables= nested_join->not_null_tables;
/* The following two might become unequal after table elimination: */
nested_join->n_tables= nested_join->join_list.elements;
} }
else else
{ {
...@@ -8733,7 +8769,7 @@ static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, ...@@ -8733,7 +8769,7 @@ static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
with anything) with anything)
2. we could run out bits in nested_join_map otherwise. 2. we could run out bits in nested_join_map otherwise.
*/ */
if (nested_join->join_list.elements != 1) if (nested_join->n_tables != 1)
{ {
nested_join->nj_map= (nested_join_map) 1 << first_unused++; nested_join->nj_map= (nested_join_map) 1 << first_unused++;
first_unused= build_bitmap_for_nested_joins(&nested_join->join_list, first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
...@@ -8755,21 +8791,26 @@ static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, ...@@ -8755,21 +8791,26 @@ static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
tables which will be ignored. tables which will be ignored.
*/ */
static void reset_nj_counters(List<TABLE_LIST> *join_list) static uint reset_nj_counters(JOIN *join, List<TABLE_LIST> *join_list)
{ {
List_iterator<TABLE_LIST> li(*join_list); List_iterator<TABLE_LIST> li(*join_list);
TABLE_LIST *table; TABLE_LIST *table;
DBUG_ENTER("reset_nj_counters"); DBUG_ENTER("reset_nj_counters");
uint n=0;
while ((table= li++)) while ((table= li++))
{ {
NESTED_JOIN *nested_join; NESTED_JOIN *nested_join;
if ((nested_join= table->nested_join)) if ((nested_join= table->nested_join))
{ {
nested_join->counter= 0; nested_join->counter= 0;
reset_nj_counters(&nested_join->join_list); //nested_join->n_tables= my_count_bits(nested_join->used_tables &
// ~join->eliminated_tables);
nested_join->n_tables= reset_nj_counters(join, &nested_join->join_list);
} }
if (table->table && (table->table->map & ~join->eliminated_tables))
n++;
} }
DBUG_VOID_RETURN; DBUG_RETURN(n);
} }
...@@ -8894,7 +8935,7 @@ static bool check_interleaving_with_nj(JOIN_TAB *next_tab) ...@@ -8894,7 +8935,7 @@ static bool check_interleaving_with_nj(JOIN_TAB *next_tab)
join->cur_embedding_map |= next_emb->nested_join->nj_map; join->cur_embedding_map |= next_emb->nested_join->nj_map;
} }
if (next_emb->nested_join->join_list.elements != if (next_emb->nested_join->n_tables !=
next_emb->nested_join->counter) next_emb->nested_join->counter)
break; break;
...@@ -8926,9 +8967,23 @@ static void restore_prev_nj_state(JOIN_TAB *last) ...@@ -8926,9 +8967,23 @@ static void restore_prev_nj_state(JOIN_TAB *last)
JOIN *join= last->join; JOIN *join= last->join;
while (last_emb) while (last_emb)
{ {
/*
psergey-elim: (nevermind)
new_prefix= cur_prefix & ~last;
if (!(new_prefix & cur_table_map)) // removed last inner table
{
join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
}
else (current)
{
// Won't hurt doing it all the time:
join->cur_embedding_map |= ...;
}
else
*/
if (!(--last_emb->nested_join->counter)) if (!(--last_emb->nested_join->counter))
join->cur_embedding_map&= ~last_emb->nested_join->nj_map; join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
else if (last_emb->nested_join->join_list.elements-1 == else if (last_emb->nested_join->n_tables-1 ==
last_emb->nested_join->counter) last_emb->nested_join->counter)
join->cur_embedding_map|= last_emb->nested_join->nj_map; join->cur_embedding_map|= last_emb->nested_join->nj_map;
else else
...@@ -16238,6 +16293,14 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order, ...@@ -16238,6 +16293,14 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
tmp3.length(0); tmp3.length(0);
quick_type= -1; quick_type= -1;
/* Don't show eliminated tables */
if (table->map & join->eliminated_tables)
{
used_tables|=table->map;
continue;
}
item_list.empty(); item_list.empty();
/* id */ /* id */
item_list.push_back(new Item_uint((uint32) item_list.push_back(new Item_uint((uint32)
...@@ -16560,8 +16623,11 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order, ...@@ -16560,8 +16623,11 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
unit; unit;
unit= unit->next_unit()) unit= unit->next_unit())
{ {
if (mysql_explain_union(thd, unit, result)) if (!(unit->item && unit->item->eliminated))
DBUG_VOID_RETURN; {
if (mysql_explain_union(thd, unit, result))
DBUG_VOID_RETURN;
}
} }
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
} }
...@@ -16602,7 +16668,6 @@ bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result) ...@@ -16602,7 +16668,6 @@ bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
unit->fake_select_lex->options|= SELECT_DESCRIBE; unit->fake_select_lex->options|= SELECT_DESCRIBE;
if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE))) if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
res= unit->exec(); res= unit->exec();
res|= unit->cleanup();
} }
else else
{ {
...@@ -16635,6 +16700,7 @@ bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result) ...@@ -16635,6 +16700,7 @@ bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
*/ */
static void print_join(THD *thd, static void print_join(THD *thd,
table_map eliminated_tables,
String *str, String *str,
List<TABLE_LIST> *tables, List<TABLE_LIST> *tables,
enum_query_type query_type) enum_query_type query_type)
...@@ -16650,12 +16716,33 @@ static void print_join(THD *thd, ...@@ -16650,12 +16716,33 @@ static void print_join(THD *thd,
*t= ti++; *t= ti++;
DBUG_ASSERT(tables->elements >= 1); DBUG_ASSERT(tables->elements >= 1);
(*table)->print(thd, str, query_type); /*
Assert that the first table in the list isn't eliminated. This comes from
the fact that the first table can't be inner table of an outer join.
*/
DBUG_ASSERT(!eliminated_tables ||
!(((*table)->table && ((*table)->table->map & eliminated_tables)) ||
((*table)->nested_join && !((*table)->nested_join->used_tables &
~eliminated_tables))));
(*table)->print(thd, eliminated_tables, str, query_type);
TABLE_LIST **end= table + tables->elements; TABLE_LIST **end= table + tables->elements;
for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++) for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
{ {
TABLE_LIST *curr= *tbl; TABLE_LIST *curr= *tbl;
/*
The "eliminated_tables &&" check guards againist the case of
printing the query for CREATE VIEW. We do that without having run
JOIN::optimize() and so will have nested_join->used_tables==0.
*/
if (eliminated_tables &&
((curr->table && (curr->table->map & eliminated_tables)) ||
(curr->nested_join && !(curr->nested_join->used_tables &
~eliminated_tables))))
{
continue;
}
if (curr->outer_join) if (curr->outer_join)
{ {
/* MySQL converts right to left joins */ /* MySQL converts right to left joins */
...@@ -16665,7 +16752,7 @@ static void print_join(THD *thd, ...@@ -16665,7 +16752,7 @@ static void print_join(THD *thd,
str->append(STRING_WITH_LEN(" straight_join ")); str->append(STRING_WITH_LEN(" straight_join "));
else else
str->append(STRING_WITH_LEN(" join ")); str->append(STRING_WITH_LEN(" join "));
curr->print(thd, str, query_type); curr->print(thd, eliminated_tables, str, query_type);
if (curr->on_expr) if (curr->on_expr)
{ {
str->append(STRING_WITH_LEN(" on(")); str->append(STRING_WITH_LEN(" on("));
...@@ -16719,12 +16806,13 @@ Index_hint::print(THD *thd, String *str) ...@@ -16719,12 +16806,13 @@ Index_hint::print(THD *thd, String *str)
@param str string where table should be printed @param str string where table should be printed
*/ */
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type) void TABLE_LIST::print(THD *thd, table_map eliminated_tables, String *str,
enum_query_type query_type)
{ {
if (nested_join) if (nested_join)
{ {
str->append('('); str->append('(');
print_join(thd, str, &nested_join->join_list, query_type); print_join(thd, eliminated_tables, str, &nested_join->join_list, query_type);
str->append(')'); str->append(')');
} }
else else
...@@ -16866,7 +16954,7 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type) ...@@ -16866,7 +16954,7 @@ void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
{ {
str->append(STRING_WITH_LEN(" from ")); str->append(STRING_WITH_LEN(" from "));
/* go through join tree */ /* go through join tree */
print_join(thd, str, &top_join_list, query_type); print_join(thd, join? join->eliminated_tables: 0, str, &top_join_list, query_type);
} }
else if (where) else if (where)
{ {
......
...@@ -285,7 +285,15 @@ public: ...@@ -285,7 +285,15 @@ public:
fetching data from a cursor fetching data from a cursor
*/ */
bool resume_nested_loop; bool resume_nested_loop;
table_map const_table_map,found_const_table_map; table_map const_table_map;
/*
Constant tables for which we have found a row (as opposed to those for
which we didn't).
*/
table_map found_const_table_map;
/* Tables removed by table elimination. Set to 0 before the elimination. */
table_map eliminated_tables;
/* /*
Bitmap of all inner tables from outer joins Bitmap of all inner tables from outer joins
*/ */
...@@ -425,6 +433,7 @@ public: ...@@ -425,6 +433,7 @@ public:
table= 0; table= 0;
tables= 0; tables= 0;
const_tables= 0; const_tables= 0;
eliminated_tables= 0;
join_list= 0; join_list= 0;
sort_and_group= 0; sort_and_group= 0;
first_record= 0; first_record= 0;
...@@ -530,6 +539,10 @@ public: ...@@ -530,6 +539,10 @@ public:
return (unit == &thd->lex->unit && (unit->fake_select_lex == 0 || return (unit == &thd->lex->unit && (unit->fake_select_lex == 0 ||
select_lex == unit->fake_select_lex)); select_lex == unit->fake_select_lex));
} }
inline table_map all_tables_map()
{
return (table_map(1) << tables) - 1;
}
private: private:
bool make_simple_join(JOIN *join, TABLE *tmp_table); bool make_simple_join(JOIN *join, TABLE *tmp_table);
}; };
...@@ -730,9 +743,12 @@ bool error_if_full_join(JOIN *join); ...@@ -730,9 +743,12 @@ bool error_if_full_join(JOIN *join);
int report_error(TABLE *table, int error); int report_error(TABLE *table, int error);
int safe_index_read(JOIN_TAB *tab); int safe_index_read(JOIN_TAB *tab);
COND *remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value); COND *remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value);
void set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key);
inline bool optimizer_flag(THD *thd, uint flag) inline bool optimizer_flag(THD *thd, uint flag)
{ {
return (thd->variables.optimizer_switch & flag); return (thd->variables.optimizer_switch & flag);
} }
void eliminate_tables(JOIN *join);
...@@ -1366,7 +1366,8 @@ struct TABLE_LIST ...@@ -1366,7 +1366,8 @@ struct TABLE_LIST
return (derived || view || schema_table || (create && !table->db_stat) || return (derived || view || schema_table || (create && !table->db_stat) ||
!table); !table);
} }
void print(THD *thd, String *str, enum_query_type query_type); void print(THD *thd, table_map eliminated_tables, String *str,
enum_query_type query_type);
bool check_single_table(TABLE_LIST **table, table_map map, bool check_single_table(TABLE_LIST **table, table_map map,
TABLE_LIST *view); TABLE_LIST *view);
bool set_insert_values(MEM_ROOT *mem_root); bool set_insert_values(MEM_ROOT *mem_root);
...@@ -1615,7 +1616,11 @@ public: ...@@ -1615,7 +1616,11 @@ public:
typedef struct st_nested_join typedef struct st_nested_join
{ {
List<TABLE_LIST> join_list; /* list of elements in the nested join */ List<TABLE_LIST> join_list; /* list of elements in the nested join */
table_map used_tables; /* bitmap of tables in the nested join */ /*
Bitmap of tables within this nested join (including those embedded within
its children), including tables removed by table elimination.
*/
table_map used_tables;
table_map not_null_tables; /* tables that rejects nulls */ table_map not_null_tables; /* tables that rejects nulls */
struct st_join_table *first_nested;/* the first nested table in the plan */ struct st_join_table *first_nested;/* the first nested table in the plan */
/* /*
...@@ -1626,6 +1631,11 @@ typedef struct st_nested_join ...@@ -1626,6 +1631,11 @@ typedef struct st_nested_join
Before each use the counters are zeroed by reset_nj_counters. Before each use the counters are zeroed by reset_nj_counters.
*/ */
uint counter; uint counter;
/*
Number of elements in join_list that were not (or contain table(s) that
weren't) removed by table elimination.
*/
uint n_tables;
nested_join_map nj_map; /* Bit used to identify this nested join*/ nested_join_map nj_map; /* Bit used to identify this nested join*/
} NESTED_JOIN; } NESTED_JOIN;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment