[MDEV-30873] Plans for SELECT and DELETE from view unexpectedly differ Created: 2023-03-17  Updated: 2023-03-17

Status: Open
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 11.1
Fix Version/s: None

Type: Bug Priority: Major
Reporter: Lena Startseva Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None


 Description   

In the case below, the plan for SELECT and DELETE is expected to be the same, but it is not

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);
insert into t1 select c1+10,c2,c3+10 from t1;
insert into t1 select c1+20,c2+1,c3+20 from t1;
 
create view v1 as select * from t1 where c2=2;
 
analyze table t1 persistent for all;
 
let $c = v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
          and c1 = 2
          and exists (select 'X' from v1 a where a.c1 = v1.c1);
 
eval explain select * from v1 where $c;
eval explain delete from v1 where $c;
 
drop view v1;
drop table t1;

Actual result:

explain select * from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	32	Using where
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	32	Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
2	DEPENDENT SUBQUERY	a	ALL	NULL	NULL	NULL	NULL	32	Using where
explain delete from v1 where v1.c2 in (select max(a.c2) from t1 a where a.c3 = v1.c3)
and c1 = 2
and exists (select 'X' from v1 a where a.c1 = v1.c1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	32	Using where
3	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	32	Using where
2	DEPENDENT SUBQUERY	a	ALL	NULL	NULL	NULL	NULL	32	Using wher


Generated at Thu Feb 08 10:19:32 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.