1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

MDEV-12137 DELETE statement with the same source and target

* various cleanups (mostly cosmetic)
* remove useless tests (that were tesing the error condition)
* optimize delete_use_source test (from 6 mins to 50 seconds,
  mainly by removing two huge rollbacks at the end).
This commit is contained in:
Sergei Golubchik
2017-07-07 19:55:31 +02:00
parent abf95afa2a
commit c65cce3698
11 changed files with 173 additions and 365 deletions

View File

@ -1,37 +1,12 @@
-- source include/have_innodb.inc
set sql_mode=oracle;
use test;
create or replace table tab_delete(c1 integer not null,c2 integer not null) engine=InnoDb;
create index tab_delete_c1 on tab_delete(c1);
create or replace view view_delete as select * from tab_delete where c1 in (0,1);
delimiter /;
CREATE or replace PROCEDURE gendata(a int, count int ) AS
i INT:=0;
BEGIN
FOR i IN 1 .. count
LOOP
insert into tab_delete values (a,i);
END LOOP;
END;
/
create or replace trigger trg after delete on tab_delete for each row
begin
declare c int;
begin
if old.c1 = 1 then
select count(*) into c from tab_delete where c1!=old.c1;
SIGNAL SQLSTATE '45000' set table_name=c;
end if;
end;
end;
/
delimiter ;/
set @count=500;
call gendata(0,@count);
call gendata(1,50);
call gendata(2,20);
call gendata(3,20);
commit;
--source include/have_sequence.inc
--source include/have_innodb.inc
create table t1(c1 integer not null,c2 integer not null, key (c1)) engine=InnoDb;
create view v1 as select * from t1 where c1 in (0,1);
insert t1 select 0,seq from seq_1_to_500;
insert t1 select 1,seq from seq_1_to_50;
insert t1 select 2,seq from seq_1_to_20;
insert t1 select 3,seq from seq_1_to_20;
--echo #
--echo # Delete with limit (quick select - range acces)
@ -39,10 +14,10 @@ commit;
start transaction;
--enable_info
delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1;
delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 limit 1;
delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 limit 1;
delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 limit 1;
--disable_info
select count(*) from view_delete where c1=0;
select count(*) from v1 where c1=0;
rollback;
--echo #
@ -50,9 +25,8 @@ rollback;
--echo #
start transaction;
--enable_info
delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 ;
--disable_info
--enable_info ONCE
delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 ;
rollback;
--echo #
@ -60,12 +34,12 @@ rollback;
--echo #
start transaction;
select count(*) from view_delete where c1=2;
select count(*) from v1 where c1=2;
--enable_info
delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10);
delete from tab_delete where c1=2 and exists(select 'x' from tab_delete b where b.c2<10);
delete from t1 where c1=2 and exists(select 'x' from t1 b where b.c2<10);
delete from t1 where c1=2 and exists(select 'x' from t1 b where b.c2<10);
--disable_info
select count(*) from view_delete where c1=2;
select count(*) from v1 where c1=2;
rollback;
--echo #
@ -73,19 +47,12 @@ rollback;
--echo #
start transaction;
--echo # Acces by range (quick_select), initied = INDEX
--echo # +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+
--echo # | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
--echo # +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+
--echo # | 1 | PRIMARY | tab_delete | range | tab_delete_c1 | tab_delete_c1 | 4 | NULL | 550 | Using where |
--echo # | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 73 | Using index |
--echo # +------+--------------------+------------+-------+---------------+---------------+---------+--------------------+------+-------------+
# explain delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1;
explain delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1;
--enable_info
delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1;
delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 limit 1;
delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1;
delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1;
--disable_info
select count(*) from view_delete where c1=0;
select count(*) from v1 where c1=0;
rollback;
--echo #
@ -93,80 +60,73 @@ rollback;
--echo #
start transaction;
--echo # Acces by pointer, initied = RND
--echo # +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+
--echo # | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
--echo # +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+
--echo # | 1 | PRIMARY | tab_delete | ALL | tab_delete_c1 | NULL | NULL | NULL | 589 | Using where |
--echo # | 2 | DEPENDENT SUBQUERY | b | ref | tab_delete_c1 | tab_delete_c1 | 4 | test.tab_delete.c1 | 295 | Using index |
--echo # +------+--------------------+------------+------+---------------+---------------+---------+--------------------+------+-------------+
# explain delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500;
--enable_info
delete from view_delete where (select count(*) from tab_delete b where b.c1=view_delete.c1) = 500 ;
--disable_info
select count(*) from view_delete where c1=0;
explain delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500;
--enable_info ONCE
delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 ;
select count(*) from v1 where c1=0;
rollback;
--echo #
--echo # Delete failed due to trigger
--echo #
delimiter /;
create trigger trg after delete on t1 for each row
begin
declare c int;
begin
if old.c1 = 1 then
select count(*) into c from t1 where c1!=old.c1;
SIGNAL SQLSTATE '45000' set table_name=c;
end if;
end;
end;
/
delimiter ;/
start transaction;
--enable_info
--error ER_SIGNAL_EXCEPTION
delete from tab_delete where c1=1 and (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c2 asc limit 10;
--disable_info
delete from t1 where c1=1 and (select count(*) from t1 b where b.c1=t1.c1) > 0 order by c2 asc limit 10;
rollback;
start transaction;
--enable_info
--error ER_SIGNAL_EXCEPTION
delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) > 0 order by c1 desc limit 100;
--disable_info
select c1,count(*) from tab_delete group by c1;
delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) > 0 order by c1 desc limit 100;
select c1,count(*) from t1 group by c1;
rollback;
drop trigger trg;
--echo #
--echo # Delete throw a view with returning
--echo #
start transaction;
delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 asc limit 10 returning c1,c2;
delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 order by c2 asc limit 10 returning c1,c2;
rollback;
start transaction;
delete from tab_delete where (select count(*) from tab_delete b where b.c1=tab_delete.c1) = 500 order by c2 desc limit 10 returning c1,c2;
delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 order by c2 desc limit 10 returning c1,c2;
rollback;
drop view v1;
drop table t1;
--echo #
--echo # Delete from table with more than 150000 rows
--echo #
insert into tab_delete select * from tab_delete;
insert into tab_delete select * from tab_delete;
insert into tab_delete select * from tab_delete;
insert into tab_delete select * from tab_delete;
insert into tab_delete select * from tab_delete;
insert into tab_delete select * from tab_delete;
insert into tab_delete select * from tab_delete;
insert into tab_delete select * from tab_delete;
select count(*) from tab_delete;
create table t1(c1 integer not null,c2 integer not null, key (c1));
insert t1 select 0,seq from seq_1_to_128000;
insert t1 select 1,seq from seq_1_to_25600;
select count(*) from t1;
--echo with high memory for sort_buffer_size
SET SESSION sort_buffer_size = 1024000;
start transaction;
--enable_info
delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10);
--disable_info
rollback;
--echo # with a lot of memory for sort_buffer_size
set session sort_buffer_size = 1024000;
--enable_info ONCE
delete from t1 where c1=0 and exists(select 'x' from t1 b where b.c1<10);
--echo with few memory for sort_buffer_size
SET SESSION sort_buffer_size = 1024;
start transaction;
--enable_info
delete from tab_delete where c1=0 and exists(select 'x' from tab_delete b where b.c1<10);
--disable_info
rollback;
--echo # with little memory for sort_buffer_size
insert t1 select 0,seq from seq_1_to_128000;
set session sort_buffer_size = 1024;
--enable_info ONCE
delete from t1 where c1=0 and exists(select 'x' from t1 b where b.c1<10);
drop procedure if exists gendata;
drop view if exists view_delete;
drop table if exists tab_delete;
drop table t1;