mirror of
https://github.com/MariaDB/server.git
synced 2025-07-30 16:24:05 +03:00
MWL#17: Table elimination
- More testcases - Let add_ft_key() set keyuse->usable mysql-test/r/table_elim.result: MWL#17: Table elimination - More testcases mysql-test/t/table_elim.test: MWL#17: Table elimination - More testcases sql/sql_select.cc: MWL#17: Table elimination - Let add_ft_key() set keyuse->usable
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
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;
|
||||
@ -76,3 +77,98 @@ 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;
|
||||
|
@ -3,6 +3,7 @@
|
||||
#
|
||||
--disable_warnings
|
||||
drop table if exists t0, t1, t2, t3;
|
||||
drop view if exists v1, v2;
|
||||
--enable_warnings
|
||||
|
||||
create table t1 (a int);
|
||||
@ -56,3 +57,71 @@ 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;
|
||||
|
@ -78,7 +78,7 @@ do_many($dbh,$server->create("elim_attr1",
|
||||
["primary key (id)",
|
||||
"key (attr1)"]));
|
||||
|
||||
# Attribute1, time-versioned
|
||||
# Attribute2, time-versioned
|
||||
do_many($dbh,$server->create("elim_attr2",
|
||||
["id integer",
|
||||
"attr2 integer",
|
||||
|
@ -4047,6 +4047,7 @@ add_ft_keys(DYNAMIC_ARRAY *keyuse_array,
|
||||
keyuse.used_tables=cond_func->key_item()->used_tables();
|
||||
keyuse.optimize= 0;
|
||||
keyuse.keypart_map= 0;
|
||||
keyuse.usable= TRUE;
|
||||
VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user