diff options
-rw-r--r-- | mysql-test/r/table_elim.result | 66 | ||||
-rw-r--r-- | mysql-test/t/table_elim.test | 58 | ||||
-rw-r--r-- | sql/opt_table_elimination.cc | 6 |
3 files changed, 129 insertions, 1 deletions
diff --git a/mysql-test/r/table_elim.result b/mysql-test/r/table_elim.result index 6d5a540fa04..ae117af3e32 100644 --- a/mysql-test/r/table_elim.result +++ b/mysql-test/r/table_elim.result @@ -352,3 +352,69 @@ explain select t1.a from t1 left join t3 on t3.pk1=t1.a and t3.pk2 IS NULL; 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,t3; +# +# Multi-equality tests +# +create table t1 (a int, b int, c int, d int); +insert into t1 values (0,0,0,0),(1,1,1,1),(2,2,2,2),(3,3,3,3); +create table t2 (pk int primary key, b int, c int); +insert into t2 select a,a,a from t1 where a in (1,2); +explain +select t1.* +from t1 left join t2 on t2.pk=t2.c and t2.b=t1.a and t1.a=t1.b and t2.c=t2.b +where t1.d=1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using where +explain +select t1.* +from +t1 +left join +t2 +on (t2.pk=t2.c and t2.b=t1.a and t1.a=t1.b and t2.c=t2.b) or +(t2.pk=t2.c and t2.b=t1.a and t1.a=t1.b and t2.c=t2.b) +where t1.d=1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using where +#This can't be eliminated: +explain +select t1.* +from +t1 +left join +t2 +on (t2.pk=t2.c and t2.b=t1.a and t2.c=t1.b) or +(t2.pk=t2.c and t1.a=t1.b and t2.c=t1.b) +where t1.d=1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using where +1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 +explain +select t1.* +from +t1 +left join +t2 +on (t2.pk=t2.c and t2.b=t1.a and t2.c=t1.b) or +(t2.pk=t2.c and t2.c=t1.b) +; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 4 +explain +select t1.* +from t1 left join t2 on t2.pk=3 or t2.pk= 4; +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 index PRIMARY PRIMARY 4 NULL 2 Using index +explain +select t1.* +from t1 left join t2 on t2.pk=3 or t2.pk= 3; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 4 +explain +select t1.* +from t1 left join t2 on (t2.pk=3 and t2.b=3) or (t2.pk= 4 and t2.b=3); +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 range PRIMARY PRIMARY 4 NULL 2 Using where +drop table t1, t2; diff --git a/mysql-test/t/table_elim.test b/mysql-test/t/table_elim.test index cab90cb5286..642c5d51d62 100644 --- a/mysql-test/t/table_elim.test +++ b/mysql-test/t/table_elim.test @@ -278,3 +278,61 @@ explain select t1.a from t1 left join t3 on t3.pk1=t1.a and t3.pk2 IS NULL; drop table t1,t2,t3; +--echo # +--echo # Multi-equality tests +--echo # +create table t1 (a int, b int, c int, d int); +insert into t1 values (0,0,0,0),(1,1,1,1),(2,2,2,2),(3,3,3,3); + +create table t2 (pk int primary key, b int, c int); +insert into t2 select a,a,a from t1 where a in (1,2); + +explain +select t1.* +from t1 left join t2 on t2.pk=t2.c and t2.b=t1.a and t1.a=t1.b and t2.c=t2.b +where t1.d=1; + +explain +select t1.* +from + t1 + left join + t2 + on (t2.pk=t2.c and t2.b=t1.a and t1.a=t1.b and t2.c=t2.b) or + (t2.pk=t2.c and t2.b=t1.a and t1.a=t1.b and t2.c=t2.b) +where t1.d=1; + +--echo #This can't be eliminated: +explain +select t1.* +from + t1 + left join + t2 + on (t2.pk=t2.c and t2.b=t1.a and t2.c=t1.b) or + (t2.pk=t2.c and t1.a=t1.b and t2.c=t1.b) +where t1.d=1; + +explain +select t1.* +from + t1 + left join + t2 + on (t2.pk=t2.c and t2.b=t1.a and t2.c=t1.b) or + (t2.pk=t2.c and t2.c=t1.b) +; + +explain +select t1.* +from t1 left join t2 on t2.pk=3 or t2.pk= 4; + +explain +select t1.* +from t1 left join t2 on t2.pk=3 or t2.pk= 3; + +explain +select t1.* +from t1 left join t2 on (t2.pk=3 and t2.b=3) or (t2.pk= 4 and t2.b=3); + +drop table t1, t2; diff --git a/sql/opt_table_elimination.cc b/sql/opt_table_elimination.cc index 1b916db2b25..6c517fa7842 100644 --- a/sql/opt_table_elimination.cc +++ b/sql/opt_table_elimination.cc @@ -654,7 +654,7 @@ Equality_module *merge_func_deps(Equality_module *start, Equality_module *new_fi List <Field_value> *fv; if (!(fv= new List<Field_value>)) - break; + break; /* purecov: inspected */ List_iterator<Field_value> it1(*old->mult_equal_fields); List_iterator<Field_value> it2(*new_fields->mult_equal_fields); @@ -664,7 +664,11 @@ Equality_module *merge_func_deps(Equality_module *start, Equality_module *new_fi while (lfield && rfield) { if (lfield == rfield) + { fv->push_back(lfield); + lfield=it1++; + rfield=it2++; + } else { uint left_ratio= lfield->field->table->tablenr*MAX_FIELDS + |