Skip to content

Commit 71208af

Browse files
committed
DB-500 allow simple deletes to use bulk fetch
1 parent 667401c commit 71208af

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
set default_storage_engine='tokudb';
2+
drop table if exists t;
3+
CREATE TABLE `t` (id bigint not null auto_increment primary key, val bigint not null default 0);
4+
INSERT INTO t (id) values (null);
5+
INSERT INTO t SELECT null,val FROM t;
6+
INSERT INTO t SELECT null,val FROM t;
7+
INSERT INTO t SELECT null,val FROM t;
8+
INSERT INTO t SELECT null,val FROM t;
9+
INSERT INTO t SELECT null,val FROM t;
10+
INSERT INTO t SELECT null,val FROM t;
11+
INSERT INTO t SELECT null,val FROM t;
12+
INSERT INTO t SELECT null,val FROM t;
13+
INSERT INTO t SELECT null,val FROM t;
14+
INSERT INTO t SELECT null,val FROM t;
15+
INSERT INTO t SELECT null,val FROM t;
16+
INSERT INTO t SELECT null,val FROM t;
17+
INSERT INTO t SELECT null,val FROM t;
18+
INSERT INTO t SELECT null,val FROM t;
19+
INSERT INTO t SELECT null,val FROM t;
20+
INSERT INTO t SELECT null,val FROM t;
21+
INSERT INTO t SELECT null,val FROM t;
22+
INSERT INTO t SELECT null,val FROM t;
23+
INSERT INTO t SELECT null,val FROM t;
24+
INSERT INTO t SELECT null,val FROM t;
25+
INSERT INTO t SELECT null,val FROM t;
26+
INSERT INTO t SELECT null,val FROM t;
27+
INSERT INTO t SELECT null,val FROM t;
28+
SELECT count(*) FROM t;
29+
count(*)
30+
8388608
31+
set tokudb_bulk_fetch = ON;
32+
delete from t where val > 0;
33+
delete from t where val > 0;
34+
delete from t where val > 0;
35+
delete from t where val > 0;
36+
delete from t where val > 0;
37+
delete from t where val > 0;
38+
delete from t where val > 0;
39+
delete from t where val > 0;
40+
delete from t where val > 0;
41+
delete from t where val > 0;
42+
set tokudb_bulk_fetch = OFF;
43+
delete from t where val > 0;
44+
delete from t where val > 0;
45+
delete from t where val > 0;
46+
delete from t where val > 0;
47+
delete from t where val > 0;
48+
delete from t where val > 0;
49+
delete from t where val > 0;
50+
delete from t where val > 0;
51+
delete from t where val > 0;
52+
delete from t where val > 0;
53+
1
54+
drop table t;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
set default_storage_engine='tokudb';
2+
drop table if exists t;
3+
create table t (id bigint not null primary key, x bigint not null);
4+
insert into t values (1,0),(2,0),(3,0),(4,0);
5+
create trigger t_delete before delete on t for each row insert into t values (1000000,0);
6+
begin;
7+
delete from t where x=0;
8+
ERROR HY000: Can't update table 't' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
9+
rollback;
10+
drop trigger t_delete;
11+
create trigger t_delete after delete on t for each row insert into t values (1000000,0);
12+
begin;
13+
delete from t where x=0;
14+
ERROR HY000: Can't update table 't' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
15+
rollback;
16+
drop trigger t_delete;
17+
create trigger t_delete before delete on t for each row delete from t where id=1000000;
18+
begin;
19+
delete from t where x=0;
20+
ERROR HY000: Can't update table 't' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
21+
rollback;
22+
drop trigger t_delete;
23+
create trigger t_delete after delete on t for each row delete from t where id=1000000;
24+
begin;
25+
delete from t where x=0;
26+
ERROR HY000: Can't update table 't' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
27+
rollback;
28+
drop trigger t_delete;
29+
create trigger t_delete before delete on t for each row update t set x=x+1 where id=1000000;
30+
begin;
31+
delete from t where x=0;
32+
ERROR HY000: Can't update table 't' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
33+
rollback;
34+
drop trigger t_delete;
35+
create trigger t_delete after delete on t for each row update t set x=x+1 where id=10000000;
36+
begin;
37+
delete from t where x=0;
38+
ERROR HY000: Can't update table 't' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
39+
rollback;
40+
drop trigger t_delete;
41+
create table count (count bigint not null);
42+
create trigger t_delete before delete on t for each row insert into count select count(*) from t;
43+
begin;
44+
delete from t where x=0;
45+
select * from count;
46+
count
47+
4
48+
3
49+
2
50+
1
51+
rollback;
52+
drop trigger t_delete;
53+
drop table count;
54+
drop table t;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Verify that index scans for delete statements use bulk fetch and are
2+
# at least twice as fast
3+
4+
source include/have_tokudb.inc;
5+
set default_storage_engine='tokudb';
6+
disable_warnings;
7+
drop table if exists t;
8+
enable_warnings;
9+
10+
CREATE TABLE `t` (id bigint not null auto_increment primary key, val bigint not null default 0);
11+
12+
# put 8M rows into t
13+
INSERT INTO t (id) values (null);
14+
INSERT INTO t SELECT null,val FROM t;
15+
INSERT INTO t SELECT null,val FROM t;
16+
INSERT INTO t SELECT null,val FROM t;
17+
INSERT INTO t SELECT null,val FROM t;
18+
INSERT INTO t SELECT null,val FROM t;
19+
INSERT INTO t SELECT null,val FROM t;
20+
INSERT INTO t SELECT null,val FROM t;
21+
INSERT INTO t SELECT null,val FROM t;
22+
INSERT INTO t SELECT null,val FROM t;
23+
INSERT INTO t SELECT null,val FROM t;
24+
INSERT INTO t SELECT null,val FROM t;
25+
INSERT INTO t SELECT null,val FROM t;
26+
INSERT INTO t SELECT null,val FROM t;
27+
INSERT INTO t SELECT null,val FROM t;
28+
INSERT INTO t SELECT null,val FROM t;
29+
INSERT INTO t SELECT null,val FROM t;
30+
INSERT INTO t SELECT null,val FROM t;
31+
INSERT INTO t SELECT null,val FROM t;
32+
INSERT INTO t SELECT null,val FROM t;
33+
INSERT INTO t SELECT null,val FROM t;
34+
INSERT INTO t SELECT null,val FROM t;
35+
INSERT INTO t SELECT null,val FROM t;
36+
INSERT INTO t SELECT null,val FROM t;
37+
SELECT count(*) FROM t;
38+
39+
# run $maxq measurements
40+
let $maxq = 10;
41+
42+
# measure the time to do $maxq deletes from t that affect no rows with bulk fetch ON
43+
set tokudb_bulk_fetch = ON;
44+
let $s = `select to_seconds(now())`;
45+
let $i = 0;
46+
while ($i < $maxq) {
47+
delete from t where val > 0;
48+
inc $i;
49+
}
50+
let $time_elapsed_bf_on = `select to_seconds(now()) - $s`;
51+
52+
# measure the time to do $maxq deletes from t that affect no rows with bulk fetch OFF
53+
set tokudb_bulk_fetch = OFF;
54+
let $s = `select to_seconds(now())`;
55+
let $i = 0;
56+
while ($i < $maxq) {
57+
delete from t where val > 0;
58+
inc $i;
59+
}
60+
let $time_elapsed_bf_off = `select to_seconds(now()) - $s`;
61+
62+
# verify that a delete scan with bulk fetch ON is at least 2 times faster than with bulk fetch OFF
63+
let $verdict = `select $time_elapsed_bf_off > $time_elapsed_bf_on && ($time_elapsed_bf_off - $time_elapsed_bf_on) / $time_elapsed_bf_on >= 2`;
64+
echo $verdict;
65+
if (!$verdict) { echo $time_elapsed_bf_on $time_elapsed_bf_off; }
66+
67+
drop table t;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# verify that delete triggers can not insert, delete, or update rows in the target table
2+
3+
source include/have_tokudb.inc;
4+
set default_storage_engine='tokudb';
5+
disable_warnings;
6+
drop table if exists t;
7+
enable_warnings;
8+
9+
create table t (id bigint not null primary key, x bigint not null);
10+
insert into t values (1,0),(2,0),(3,0),(4,0);
11+
12+
# verify that a before delete trigger can not insert into the target table
13+
create trigger t_delete before delete on t for each row insert into t values (1000000,0);
14+
begin;
15+
error 1442;
16+
delete from t where x=0;
17+
rollback;
18+
drop trigger t_delete;
19+
20+
# verify that an after delete trigger can not insert into the target table
21+
create trigger t_delete after delete on t for each row insert into t values (1000000,0);
22+
begin;
23+
error 1442;
24+
delete from t where x=0;
25+
rollback;
26+
drop trigger t_delete;
27+
28+
# verify that a before delete trigger can not delete from the target table
29+
create trigger t_delete before delete on t for each row delete from t where id=1000000;
30+
begin;
31+
error 1442;
32+
delete from t where x=0;
33+
rollback;
34+
drop trigger t_delete;
35+
36+
# verify that an after delete trigger can not delete from the target table
37+
create trigger t_delete after delete on t for each row delete from t where id=1000000;
38+
begin;
39+
error 1442;
40+
delete from t where x=0;
41+
rollback;
42+
drop trigger t_delete;
43+
44+
# verify that a before delete trigger can not update the target table
45+
create trigger t_delete before delete on t for each row update t set x=x+1 where id=1000000;
46+
begin;
47+
error 1442;
48+
delete from t where x=0;
49+
rollback;
50+
drop trigger t_delete;
51+
52+
# verify that an after delete trigger can not update the target table
53+
create trigger t_delete after delete on t for each row update t set x=x+1 where id=10000000;
54+
begin;
55+
error 1442;
56+
delete from t where x=0;
57+
rollback;
58+
drop trigger t_delete;
59+
60+
# can execute select on the target table in a delete trigger. it better use a different handler.
61+
create table count (count bigint not null);
62+
create trigger t_delete before delete on t for each row insert into count select count(*) from t;
63+
begin;
64+
delete from t where x=0;
65+
select * from count;
66+
rollback;
67+
drop trigger t_delete;
68+
drop table count;
69+
70+
drop table t;

storage/tokudb/ha_tokudb.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4370,6 +4370,7 @@ static bool tokudb_do_bulk_fetch(THD *thd) {
43704370
case SQLCOM_CREATE_TABLE:
43714371
case SQLCOM_INSERT_SELECT:
43724372
case SQLCOM_REPLACE_SELECT:
4373+
case SQLCOM_DELETE:
43734374
return THDVAR(thd, bulk_fetch) != 0;
43744375
default:
43754376
return false;

0 commit comments

Comments
 (0)