2017-08-25 19:06:13 +02:00
|
|
|
create table t1 (a int, b int);
|
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
insert into t1
|
2017-08-25 19:06:13 +02:00
|
|
|
values (1,2), (4,6), (9,7),
|
|
|
|
(1,1), (2,5), (7,8);
|
|
|
|
|
|
|
|
create table t2 (a int, b int, c int);
|
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
insert into t2
|
|
|
|
values (1,2,3), (5,1,2), (4,3,7),
|
2017-08-25 19:06:13 +02:00
|
|
|
(8,9,0), (10,7,1), (5,5,1);
|
|
|
|
|
|
|
|
--echo # optimization is not used
|
|
|
|
|
|
|
|
let $query= select * from t1 where a in (1,2);
|
|
|
|
eval $query;
|
|
|
|
eval explain $query;
|
2017-08-29 02:32:39 +02:00
|
|
|
eval explain format=json $query;
|
2017-08-25 19:06:13 +02:00
|
|
|
|
|
|
|
--echo # set minimum number of values in VALUEs list when optimization works to 2
|
|
|
|
|
|
|
|
set @@in_subquery_conversion_threshold= 2;
|
|
|
|
|
|
|
|
--echo # single IN-predicate in WHERE-part
|
|
|
|
|
|
|
|
let $query= select * from t1 where a in (1,2);
|
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
let $optimized_query=
|
|
|
|
select * from t1
|
|
|
|
where a in
|
2017-08-25 19:06:13 +02:00
|
|
|
(
|
2017-08-29 02:32:39 +02:00
|
|
|
select *
|
|
|
|
from (values (1),(2)) as tvc_0
|
2017-08-25 19:06:13 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
eval $query;
|
|
|
|
eval $optimized_query;
|
2017-08-29 02:32:39 +02:00
|
|
|
eval explain extended $query;
|
|
|
|
eval explain extended $optimized_query;
|
2017-08-25 19:06:13 +02:00
|
|
|
|
|
|
|
--echo # AND-condition with IN-predicates in WHERE-part
|
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
let $query=
|
|
|
|
select * from t1
|
|
|
|
where a in (1,2) and
|
2017-08-25 19:06:13 +02:00
|
|
|
b in (1,5);
|
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
let $optimized_query=
|
|
|
|
select * from t1
|
|
|
|
where a in
|
2017-08-25 19:06:13 +02:00
|
|
|
(
|
2017-08-29 02:32:39 +02:00
|
|
|
select *
|
|
|
|
from (values (1),(2)) as tvc_0
|
|
|
|
)
|
2017-08-25 19:06:13 +02:00
|
|
|
and b in
|
|
|
|
(
|
2017-08-29 02:32:39 +02:00
|
|
|
select *
|
|
|
|
from (values (1),(5)) as tvc_1
|
2017-08-25 19:06:13 +02:00
|
|
|
);
|
2017-08-29 02:32:39 +02:00
|
|
|
|
2017-08-25 19:06:13 +02:00
|
|
|
eval $query;
|
|
|
|
eval $optimized_query;
|
2017-08-29 02:32:39 +02:00
|
|
|
eval explain extended $query;
|
|
|
|
eval explain extended $optimized_query;
|
2017-08-25 19:06:13 +02:00
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
--echo # subquery with IN-predicate
|
2017-08-25 19:06:13 +02:00
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
let $query=
|
|
|
|
select * from t1
|
|
|
|
where a in
|
2017-08-25 19:06:13 +02:00
|
|
|
(
|
2017-08-29 02:32:39 +02:00
|
|
|
select a
|
|
|
|
from t2 where b in (3,4)
|
|
|
|
);
|
|
|
|
|
|
|
|
let $optimized_query=
|
|
|
|
select * from t1
|
|
|
|
where a in
|
2017-08-25 19:06:13 +02:00
|
|
|
(
|
2017-08-29 02:32:39 +02:00
|
|
|
select a from t2
|
|
|
|
where b in
|
|
|
|
(
|
|
|
|
select *
|
|
|
|
from (values (3),(4)) as tvc_0
|
|
|
|
)
|
2017-08-25 19:06:13 +02:00
|
|
|
);
|
2017-08-29 02:32:39 +02:00
|
|
|
|
2017-08-25 19:06:13 +02:00
|
|
|
eval $query;
|
|
|
|
eval $optimized_query;
|
2017-08-29 02:32:39 +02:00
|
|
|
eval explain extended $query;
|
|
|
|
eval explain extended $optimized_query;
|
2017-08-25 19:06:13 +02:00
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
--echo # derived table with IN-predicate
|
|
|
|
|
|
|
|
let $query=
|
|
|
|
select * from
|
|
|
|
(
|
|
|
|
select *
|
|
|
|
from t1
|
|
|
|
where a in (1,2)
|
|
|
|
) as dr_table;
|
2017-08-25 19:06:13 +02:00
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
let $optimized_query=
|
|
|
|
select * from
|
|
|
|
(
|
|
|
|
select *
|
|
|
|
from t1
|
|
|
|
where a in
|
2017-08-25 19:06:13 +02:00
|
|
|
(
|
2017-08-29 02:32:39 +02:00
|
|
|
select *
|
|
|
|
from (values (1),(2))
|
|
|
|
as tvc_0
|
2017-08-25 19:06:13 +02:00
|
|
|
)
|
2017-08-29 02:32:39 +02:00
|
|
|
) as dr_table;
|
2017-08-25 19:06:13 +02:00
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
eval $query;
|
|
|
|
eval $optimized_query;
|
|
|
|
eval explain extended $query;
|
|
|
|
eval explain extended $optimized_query;
|
|
|
|
|
|
|
|
--echo # non-recursive CTE with IN-predicate
|
|
|
|
|
|
|
|
let $cte_query=
|
|
|
|
with tvc_0 as
|
|
|
|
(
|
|
|
|
select *
|
|
|
|
from t1
|
|
|
|
where a in (1,2)
|
|
|
|
)
|
|
|
|
select * from tvc_0;
|
|
|
|
|
|
|
|
eval $cte_query;
|
|
|
|
eval $optimized_query;
|
|
|
|
eval explain extended $cte_query;
|
|
|
|
eval explain extended $optimized_query;
|
|
|
|
|
|
|
|
--echo # VIEW with IN-predicate
|
|
|
|
|
|
|
|
create view v1 as
|
|
|
|
select *
|
|
|
|
from t1
|
|
|
|
where a in (1,2);
|
|
|
|
|
|
|
|
create view v2 as
|
|
|
|
select *
|
|
|
|
from t1
|
|
|
|
where a in
|
2017-08-25 19:06:13 +02:00
|
|
|
(
|
2017-08-29 02:32:39 +02:00
|
|
|
select *
|
|
|
|
from (values (1),(2))
|
|
|
|
as tvc_0
|
2017-08-25 19:06:13 +02:00
|
|
|
)
|
|
|
|
;
|
2017-08-29 02:32:39 +02:00
|
|
|
|
|
|
|
let $query= select * from v1;
|
|
|
|
let $optimized_query= select * from v2;
|
|
|
|
|
2017-08-25 19:06:13 +02:00
|
|
|
eval $query;
|
|
|
|
eval $optimized_query;
|
2017-08-29 02:32:39 +02:00
|
|
|
eval explain extended $query;
|
|
|
|
eval explain extended $optimized_query;
|
2017-08-25 19:06:13 +02:00
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
drop view v1,v2;
|
|
|
|
|
|
|
|
--echo # subselect defined by derived table with IN-predicate
|
|
|
|
|
|
|
|
let $query=
|
|
|
|
select * from t1
|
|
|
|
where a in
|
|
|
|
(
|
|
|
|
select 1
|
|
|
|
from
|
|
|
|
(
|
|
|
|
select *
|
|
|
|
from t1
|
|
|
|
where a in (1,2)
|
|
|
|
)
|
|
|
|
as dr_table
|
|
|
|
);
|
|
|
|
|
|
|
|
let $optimized_query=
|
|
|
|
select * from t1
|
|
|
|
where a in
|
|
|
|
(
|
|
|
|
select 1
|
|
|
|
from
|
|
|
|
(
|
|
|
|
select *
|
|
|
|
from t1
|
|
|
|
where a in
|
|
|
|
(
|
|
|
|
select *
|
|
|
|
from (values (1),(2))
|
|
|
|
as tvc_0
|
|
|
|
)
|
|
|
|
)
|
|
|
|
as dr_table
|
|
|
|
);
|
2017-08-25 19:06:13 +02:00
|
|
|
|
2017-08-29 02:32:39 +02:00
|
|
|
eval $query;
|
|
|
|
eval $optimized_query;
|
|
|
|
eval explain extended $query;
|
|
|
|
eval explain extended $optimized_query;
|
|
|
|
|
|
|
|
--echo # derived table with IN-predicate and group by
|
|
|
|
|
|
|
|
let $query=
|
|
|
|
select * from
|
|
|
|
(
|
|
|
|
select max(a),b
|
|
|
|
from t1
|
|
|
|
where b in (3,5)
|
|
|
|
group by b
|
|
|
|
) as dr_table;
|
|
|
|
|
|
|
|
let $optimized_query=
|
|
|
|
select * from
|
|
|
|
(
|
|
|
|
select max(a),b
|
|
|
|
from t1
|
|
|
|
where b in
|
|
|
|
(
|
|
|
|
select *
|
|
|
|
from (values (3),(5))
|
|
|
|
as tvc_0
|
|
|
|
)
|
|
|
|
group by b
|
|
|
|
) as dr_table;
|
|
|
|
|
|
|
|
eval $query;
|
|
|
|
eval $optimized_query;
|
|
|
|
eval explain extended $query;
|
|
|
|
eval explain extended $optimized_query;
|
|
|
|
|
|
|
|
drop table t1, t2;
|
|
|
|
set @@in_subquery_conversion_threshold= default;
|