Details
Description
For the following query with SELECT:
explain
|
select * |
from v1,t3 |
where
|
v1.c2 = t3.c2 and |
v1.c2 in (select max(c2) from t2 where c3 = v1.c3) |
and v1.c1 < 10 and |
exists (select 'X' from v2 where c1 = v1.c1); |
EXPLAIN produces:
MariaDB [test]> explain
|
-> select *
|
-> from v1,t3
|
-> where
|
-> v1.c2 = t3.c2 and
|
-> v1.c2 in (select max(c2) from t2 where c3 = v1.c3)
|
-> and v1.c1 < 10 and
|
-> exists (select 'X' from v2 where c1 = v1.c1);
|
+------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+
|
| 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where |
|
| 1 | PRIMARY | <subquery3> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | |
|
| 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where; Using join buffer (flat, BNL join) |
|
| 3 | MATERIALIZED | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | |
|
| 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where |
|
+------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+
|
For the queries with DELETE and UPDATE based on previous SELECT:
delete
|
from v1 |
using v1,t3
|
where
|
v1.c2 = t3.c2 and |
v1.c2 in (select max(c2) from t2 where c3 = v1.c3) |
and v1.c1 < 10 and |
exists (select 'X' from v2 where c1 = v1.c1); |
update v1,t3 |
set v1.c1 = 15 |
where
|
v1.c2 = t3.c2 and |
v1.c2 in (select max(c2) from t2 where c3 = v1.c3) |
and v1.c1 < 10 and |
exists (select 'X' from v2 where c1 = v1.c1); |
EXPLAIN produces:
MariaDB [test]> explain
|
-> delete
|
-> from v1
|
-> using v1,t3
|
-> where
|
-> v1.c2 = t3.c2 and
|
-> v1.c2 in (select max(c2) from t2 where c3 = v1.c3)
|
-> and v1.c1 < 10 and
|
-> exists (select 'X' from v2 where c1 = v1.c1);
|
+------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+
|
| 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where |
|
| 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where |
|
| 3 | DEPENDENT SUBQUERY | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | Using where |
|
| 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where |
|
+------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+
|
|
MariaDB [test]> explain
|
-> update v1,t3
|
-> set v1.c1 = 15
|
-> where
|
-> v1.c2 = t3.c2 and
|
-> v1.c2 in (select max(c2) from t2 where c3 = v1.c3)
|
-> and v1.c1 < 10 and
|
-> exists (select 'X' from v2 where c1 = v1.c1);
|
+------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+
|
| 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where |
|
| 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where |
|
| 3 | DEPENDENT SUBQUERY | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | Using where |
|
| 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where |
|
+------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+
|
|
but it is expected that the plan will be similar to the plan for SELECT
MTR test case:
|
create table t1 (c1 integer, c2 integer, c3 integer) ; |
insert into t1(c1,c2,c3) |
values (1,1,1),(1,2,2),(1,3,3), |
(2,1,4),(2,2,5),(2,3,6),
|
(2,4,7),(2,5,8);
|
create view v1 as select * from t1 where c2=2; |
create index t1_c2 on t1 (c2); |
analyze table t1 persistent for all; |
|
create table t2 (c1 integer, c2 integer, c3 integer) ; |
insert into t2(c1,c2,c3) |
values (1,1,1),(1,2,2),(1,3,3), |
(2,1,4),(2,2,5),(2,3,6),
|
(2,4,7),(2,5,8);
|
create view v2 as select * from t2 where c2=2; |
create index t2_c2 on t2 (c2); |
analyze table t2 persistent for all; |
|
create table t3 (c1 integer, c2 integer, c3 integer) ; |
insert into t3(c1,c2,c3) |
values (1,1,1),(1,2,2),(1,3,3), |
(2,1,4),(2,2,5),(2,3,6),
|
(2,4,7),(2,5,8);
|
analyze table t3 persistent for all; |
|
explain
|
select * |
from v1,t3 |
where
|
v1.c2 = t3.c2 and |
v1.c2 in (select max(c2) from t2 where c3 = v1.c3) |
and v1.c1 < 10 and |
exists (select 'X' from v2 where c1 = v1.c1); |
|
explain
|
delete
|
from v1 |
using v1,t3
|
where
|
v1.c2 = t3.c2 and |
v1.c2 in (select max(c2) from t2 where c3 = v1.c3) |
and v1.c1 < 10 and |
exists (select 'X' from v2 where c1 = v1.c1); |
|
explain
|
update v1,t3 |
set v1.c1 = 15 |
where
|
v1.c2 = t3.c2 and |
v1.c2 in (select max(c2) from t2 where c3 = v1.c3) |
and v1.c1 < 10 and |
exists (select 'X' from v2 where c1 = v1.c1); |
|
drop view v1, v2; |
drop table t1, t2, t3; |
Attachments
Activity
Field | Original Value | New Value |
---|---|---|
Description |
For the following query with SELECT:
{code:sql} select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func 1 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join) 3 MATERIALIZED t2 ref t2_c2 t2_c2 5 const 2 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} For the queries with DELETE and UPDATE based on previous SELECT: {code:sql} delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} {code:sql} update v1,t3 set v1.c1 = 15, t3.c1=21 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where 3 DEPENDENT SUBQUERY t2 ref t2_c2 t2_c2 5 const 2 Using where 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} *but it is expected that the plan will be the same as plan for select.* MTR test case: {code:sql} --source include/have_innodb.inc create table t1 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t1(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v1 as select * from t1 where c2=2; create index t1_c2 on t1 (c2); analyze table t1 persistent for all; create table t2 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t2(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v2 as select * from t2 where c2=2; create index t2_c2 on t2 (c2); analyze table t2 persistent for all; create table t3 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t3(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); analyze table t3 persistent for all; explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); explain delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain update v1,t3 set v1.c1 = 15, t3.c1=21 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); drop view v1, v2; drop table t1, t2, t3; {code} |
For the following query with SELECT:
{code:sql} select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func 1 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join) 3 MATERIALIZED t2 ref t2_c2 t2_c2 5 const 2 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} For the queries with DELETE and UPDATE based on previous SELECT: {code:sql} delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} {code:sql} update v1,t3 set v1.c1 = 15, t3.c1=21 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where 3 DEPENDENT SUBQUERY t2 ref t2_c2 t2_c2 5 const 2 Using where 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} *but it is expected that the plan will be the same as plan for SELECT* MTR test case: {code:sql} --source include/have_innodb.inc create table t1 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t1(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v1 as select * from t1 where c2=2; create index t1_c2 on t1 (c2); analyze table t1 persistent for all; create table t2 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t2(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v2 as select * from t2 where c2=2; create index t2_c2 on t2 (c2); analyze table t2 persistent for all; create table t3 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t3(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); analyze table t3 persistent for all; explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); explain delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain update v1,t3 set v1.c1 = 15, t3.c1=21 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); drop view v1, v2; drop table t1, t2, t3; {code} |
Assignee | Igor Babaev [ igor ] |
Component/s | Optimizer [ 10200 ] |
Summary | EXPLAIN: different plan for SELECT and DELETE/UPDATE | EXPLAIN: different plan for SELECT and multi-table DELETE/UPDATE |
Summary | EXPLAIN: different plan for SELECT and multi-table DELETE/UPDATE | EXPLAIN: different plans for SELECT and multi-table DELETE/UPDATE |
Fix Version/s | 10.4 [ 22408 ] |
Status | Open [ 1 ] | In Progress [ 3 ] |
Description |
For the following query with SELECT:
{code:sql} select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func 1 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join) 3 MATERIALIZED t2 ref t2_c2 t2_c2 5 const 2 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} For the queries with DELETE and UPDATE based on previous SELECT: {code:sql} delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} {code:sql} update v1,t3 set v1.c1 = 15, t3.c1=21 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where 3 DEPENDENT SUBQUERY t2 ref t2_c2 t2_c2 5 const 2 Using where 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} *but it is expected that the plan will be the same as plan for SELECT* MTR test case: {code:sql} --source include/have_innodb.inc create table t1 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t1(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v1 as select * from t1 where c2=2; create index t1_c2 on t1 (c2); analyze table t1 persistent for all; create table t2 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t2(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v2 as select * from t2 where c2=2; create index t2_c2 on t2 (c2); analyze table t2 persistent for all; create table t3 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t3(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); analyze table t3 persistent for all; explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); explain delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain update v1,t3 set v1.c1 = 15, t3.c1=21 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); drop view v1, v2; drop table t1, t2, t3; {code} |
For the following query with SELECT:
{code:sql} select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func 1 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join) 3 MATERIALIZED t2 ref t2_c2 t2_c2 5 const 2 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} For the queries with DELETE and UPDATE based on previous SELECT: {code:sql} delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} {code:sql} update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where 3 DEPENDENT SUBQUERY t2 ref t2_c2 t2_c2 5 const 2 Using where 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} *but it is expected that the plan will be the same as plan for SELECT* MTR test case: {code:sql} --source include/have_innodb.inc create table t1 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t1(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v1 as select * from t1 where c2=2; create index t1_c2 on t1 (c2); analyze table t1 persistent for all; create table t2 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t2(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v2 as select * from t2 where c2=2; create index t2_c2 on t2 (c2); analyze table t2 persistent for all; create table t3 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t3(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); analyze table t3 persistent for all; explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); explain delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); drop view v1, v2; drop table t1, t2, t3; {code} |
Summary | EXPLAIN: different plans for SELECT and multi-table DELETE/UPDATE | Unexpectedly different plans for SELECT and multi-table DELETE/UPDATE |
Summary | Unexpectedly different plans for SELECT and multi-table DELETE/UPDATE | Plans for SELECT and multi-table UPDATE/DELETE unexpectedly differ |
Assignee | Igor Babaev [ igor ] | Oleksandr Byelkin [ sanja ] |
Status | In Progress [ 3 ] | In Review [ 10002 ] |
Description |
For the following query with SELECT:
{code:sql} select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func 1 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join) 3 MATERIALIZED t2 ref t2_c2 t2_c2 5 const 2 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} For the queries with DELETE and UPDATE based on previous SELECT: {code:sql} delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} {code:sql} update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {code:sql} id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref t1_c2 t1_c2 5 const 2 Using where 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where 3 DEPENDENT SUBQUERY t2 ref t2_c2 t2_c2 5 const 2 Using where 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 8 Using where {code} *but it is expected that the plan will be the same as plan for SELECT* MTR test case: {code:sql} --source include/have_innodb.inc create table t1 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t1(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v1 as select * from t1 where c2=2; create index t1_c2 on t1 (c2); analyze table t1 persistent for all; create table t2 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t2(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v2 as select * from t2 where c2=2; create index t2_c2 on t2 (c2); analyze table t2 persistent for all; create table t3 (c1 integer, c2 integer, c3 integer) engine=innodb; insert into t3(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); analyze table t3 persistent for all; explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and v1.c1 in (select c1 from v2); explain delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); drop view v1, v2; drop table t1, t2, t3; {code} |
For the following query with SELECT:
{code:sql} explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {noformat} MariaDB [test]> explain -> select * -> from v1,t3 -> where -> v1.c2 = t3.c2 and -> v1.c2 in (select max(c2) from t2 where c3 = v1.c3) -> and v1.c1 < 10 and -> exists (select 'X' from v2 where c1 = v1.c1); +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+ | 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where | | 1 | PRIMARY | <subquery3> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | | | 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where; Using join buffer (flat, BNL join) | | 3 | MATERIALIZED | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | | | 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+ {noformat} For the queries with DELETE and UPDATE based on previous SELECT: {code:sql} delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} {code:sql} update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {noformat} MariaDB [test]> explain -> delete -> from v1 -> using v1,t3 -> where -> v1.c2 = t3.c2 and -> v1.c2 in (select max(c2) from t2 where c3 = v1.c3) -> and v1.c1 < 10 and -> exists (select 'X' from v2 where c1 = v1.c1); +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------+ | 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where | | 1 | PRIMARY | <subquery3> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | | | 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | | 3 | MATERIALIZED | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | | | 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------+ {noformat} *but it is expected that the plan will be similar to the plan for SELECT* MTR test case: {code:sql} create table t1 (c1 integer, c2 integer, c3 integer) ; insert into t1(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v1 as select * from t1 where c2=2; create index t1_c2 on t1 (c2); analyze table t1 persistent for all; create table t2 (c1 integer, c2 integer, c3 integer) ; insert into t2(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v2 as select * from t2 where c2=2; create index t2_c2 on t2 (c2); analyze table t2 persistent for all; create table t3 (c1 integer, c2 integer, c3 integer) ; insert into t3(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); analyze table t3 persistent for all; explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); drop view v1, v2; drop table t1, t2, t3; {code} |
Assignee | Oleksandr Byelkin [ sanja ] | Igor Babaev [ igor ] |
Status | In Review [ 10002 ] | Stalled [ 10000 ] |
Status | Stalled [ 10000 ] | In Progress [ 3 ] |
Fix Version/s | 10.4.29 [ 28510 ] | |
Fix Version/s | 10.5.20 [ 28512 ] | |
Fix Version/s | 10.6.13 [ 28514 ] | |
Fix Version/s | 10.7.9 [ 28516 ] | |
Fix Version/s | 10.8.8 [ 28518 ] | |
Fix Version/s | 10.9.6 [ 28520 ] | |
Fix Version/s | 10.4 [ 22408 ] | |
Resolution | Fixed [ 1 ] | |
Status | In Progress [ 3 ] | Closed [ 6 ] |
Description |
For the following query with SELECT:
{code:sql} explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {noformat} MariaDB [test]> explain -> select * -> from v1,t3 -> where -> v1.c2 = t3.c2 and -> v1.c2 in (select max(c2) from t2 where c3 = v1.c3) -> and v1.c1 < 10 and -> exists (select 'X' from v2 where c1 = v1.c1); +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+ | 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where | | 1 | PRIMARY | <subquery3> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | | | 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where; Using join buffer (flat, BNL join) | | 3 | MATERIALIZED | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | | | 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+ {noformat} For the queries with DELETE and UPDATE based on previous SELECT: {code:sql} delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} {code:sql} update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {noformat} MariaDB [test]> explain -> delete -> from v1 -> using v1,t3 -> where -> v1.c2 = t3.c2 and -> v1.c2 in (select max(c2) from t2 where c3 = v1.c3) -> and v1.c1 < 10 and -> exists (select 'X' from v2 where c1 = v1.c1); +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------+ | 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where | | 1 | PRIMARY | <subquery3> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | | | 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | | 3 | MATERIALIZED | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | | | 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------+ {noformat} *but it is expected that the plan will be similar to the plan for SELECT* MTR test case: {code:sql} create table t1 (c1 integer, c2 integer, c3 integer) ; insert into t1(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v1 as select * from t1 where c2=2; create index t1_c2 on t1 (c2); analyze table t1 persistent for all; create table t2 (c1 integer, c2 integer, c3 integer) ; insert into t2(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v2 as select * from t2 where c2=2; create index t2_c2 on t2 (c2); analyze table t2 persistent for all; create table t3 (c1 integer, c2 integer, c3 integer) ; insert into t3(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); analyze table t3 persistent for all; explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); drop view v1, v2; drop table t1, t2, t3; {code} |
For the following query with SELECT:
{code:sql} explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {noformat} MariaDB [test]> explain -> select * -> from v1,t3 -> where -> v1.c2 = t3.c2 and -> v1.c2 in (select max(c2) from t2 where c3 = v1.c3) -> and v1.c1 < 10 and -> exists (select 'X' from v2 where c1 = v1.c1); +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+ | 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where | | 1 | PRIMARY | <subquery3> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | | | 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where; Using join buffer (flat, BNL join) | | 3 | MATERIALIZED | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | | | 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | +------+--------------------+-------------+--------+---------------+--------------+---------+-------+------+-------------------------------------------------+ {noformat} For the queries with DELETE and UPDATE based on previous SELECT: {code:sql} delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} {code:sql} update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); {code} EXPLAIN produces: {noformat} MariaDB [test]> explain -> delete -> from v1 -> using v1,t3 -> where -> v1.c2 = t3.c2 and -> v1.c2 in (select max(c2) from t2 where c3 = v1.c3) -> and v1.c1 < 10 and -> exists (select 'X' from v2 where c1 = v1.c1); +------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+ | 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where | | 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | | 3 | DEPENDENT SUBQUERY | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | Using where | | 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | +------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+ MariaDB [test]> explain -> update v1,t3 -> set v1.c1 = 15 -> where -> v1.c2 = t3.c2 and -> v1.c2 in (select max(c2) from t2 where c3 = v1.c3) -> and v1.c1 < 10 and -> exists (select 'X' from v2 where c1 = v1.c1); +------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+ | 1 | PRIMARY | t1 | ref | t1_c2 | t1_c2 | 5 | const | 2 | Using where | | 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | | 3 | DEPENDENT SUBQUERY | t2 | ref | t2_c2 | t2_c2 | 5 | const | 2 | Using where | | 2 | DEPENDENT SUBQUERY | t2 | ALL | NULL | NULL | NULL | NULL | 8 | Using where | +------+--------------------+-------+------+---------------+-------+---------+-------+------+-------------+ {noformat} *but it is expected that the plan will be similar to the plan for SELECT* MTR test case: {code:sql} create table t1 (c1 integer, c2 integer, c3 integer) ; insert into t1(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v1 as select * from t1 where c2=2; create index t1_c2 on t1 (c2); analyze table t1 persistent for all; create table t2 (c1 integer, c2 integer, c3 integer) ; insert into t2(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); create view v2 as select * from t2 where c2=2; create index t2_c2 on t2 (c2); analyze table t2 persistent for all; create table t3 (c1 integer, c2 integer, c3 integer) ; insert into t3(c1,c2,c3) values (1,1,1),(1,2,2),(1,3,3), (2,1,4),(2,2,5),(2,3,6), (2,4,7),(2,5,8); analyze table t3 persistent for all; explain select * from v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain delete from v1 using v1,t3 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); explain update v1,t3 set v1.c1 = 15 where v1.c2 = t3.c2 and v1.c2 in (select max(c2) from t2 where c3 = v1.c3) and v1.c1 < 10 and exists (select 'X' from v2 where c1 = v1.c1); drop view v1, v2; drop table t1, t2, t3; {code} |
Fix Version/s | 10.7.9 [ 28516 ] |
Here's a simpler test case demonstrating this problem:
(1,1,1),(3,2,2),(1,3,3),
(2,1,4),(2,2,5),(4,3,6),
(2,4,7),(2,5,8);
(1,7,1),(1,8,2),(1,3,3),
(2,1,4),(2,2,5),(2,3,6),
(2,4,7),(2,5,8);
(1,1,1),(1,2,2),(1,3,3),
(2,1,4),(2,2,5),(2,3,6),
(2,4,7),(2,5,8);
explain
where
explain
where
explain
where
We have the following output for the above EXPLAIN commands:
MariaDB [test]> explain
-> select * from t1,t3
-> where
-> t1.c2 = t3.c2 and
-> t1.c1 > 1 and
-> exists (select 'X' from t2 where t2.c1 = t1.c1 and t2.c2 > 4);
+------+--------------+-------------+--------+---------------+--------------+---------+------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+-------------+--------+---------------+--------------+---------+------------+------+-------------+
| 1 | PRIMARY | t1 | ALL | idx | NULL | NULL | NULL | 8 | Using where |
| 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | |
| 1 | PRIMARY | t3 | ref | idx | idx | 5 | test.t1.c2 | 3 | |
| 2 | MATERIALIZED | t2 | ALL | idx | NULL | NULL | NULL | 8 | Using where |
+------+--------------+-------------+--------+---------------+--------------+---------+------------+------+-------------+
4 rows in set (0.003 sec)
MariaDB [test]>
MariaDB [test]> explain
-> delete from t1 using t1,t3
-> where
-> t1.c2 = t3.c2 and
-> t1.c1 > 1 and
-> exists (select 'X' from t2 where t2.c1 = t1.c1 and t2.c2 > 4);
+------+--------------------+-------+-------+---------------+------+---------+------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------------+-------+-------+---------------+------+---------+------------+------+-------------+
| 1 | PRIMARY | t1 | ALL | idx | NULL | NULL | NULL | 8 | Using where |
| 1 | PRIMARY | t3 | ref | idx | idx | 5 | test.t1.c2 | 3 | Using index |
| 2 | DEPENDENT SUBQUERY | t2 | range | idx | idx | 5 | NULL | 3 | Using where |
+------+--------------------+-------+-------+---------------+------+---------+------------+------+-------------+
3 rows in set (0.001 sec)
MariaDB [test]>
MariaDB [test]> explain
-> update t1,t3 set t1.c1 = t1.c1+10
-> where
-> t1.c2 = t3.c2 and
-> t1.c1 > 1 and
-> exists (select 'X' from t2 where t2.c1 = t1.c1 and t2.c2 > 4);
+------+--------------------+-------+-------+---------------+------+---------+------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------------+-------+-------+---------------+------+---------+------------+------+-------------+
| 1 | PRIMARY | t1 | ALL | idx | NULL | NULL | NULL | 8 | Using where |
| 1 | PRIMARY | t3 | ref | idx | idx | 5 | test.t1.c2 | 3 | Using index |
| 2 | DEPENDENT SUBQUERY | t2 | range | idx | idx | 5 | NULL | 3 | Using where |
+------+--------------------+-------+-------+---------------+------+---------+------------+------+-------------+
3 rows in set (0.001 sec)