1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
--echo #
--echo # Delete with value from subquery on the same table
--echo #
analyze table t1 persistent for all;
let $c = c1=(select a.c3 from t1 a where a.c3 = t1.c3);
eval create table tmp as select * from t1 where $c;
let $q = delete from t1 where $c;
eval explain select * from t1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
--echo #
--echo # Delete with EXISTS subquery over the updated table
--echo # in WHERE + possibly sargable condition
--echo #
analyze table t1 persistent for all;
let $c = c1 = 1 and exists (select 'X' from t1 a where a.c1 = t1.c2);
eval create table tmp as select * from t1 where $c;
let $q = delete from t1 where $c;
eval explain select * from t1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
--echo #
--echo # Delete with EXISTS subquery over the updated table
--echo # in WHERE + non-sargable condition
--echo #
analyze table t1 persistent for all;
let $c = exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 = 3;
eval create table tmp as select * from t1 where $c;
let $q = delete from t1 where $c;
eval explain select * from t1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
--echo #
--echo # Delete with order by
--echo #
analyze table t1 persistent for all;
let $c = exists (select 'X' from t1 a where a.c2 = t1.c2)
and c2 >= 3 order by c2;
eval create table tmp as select * from t1 where $c;
let $q = delete from t1 where $c;
eval explain select * from t1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
--echo #
--echo # Delete with IN predicand over the updated table in WHERE
--echo #
let $c = c3 in (select distinct a.c1 from t1 a where t1.c2=a.c2);
eval create table tmp as select * from t1 where $c;
let $q = delete from t1 where $c;
eval explain select * from t1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
--echo #
--echo # Delete with a limit - can be deleted
--echo #
let $c = c1 in (select a.c2 from t1 a where a.c2 = t1.c3) limit 1;
eval create table tmp as select * from t1 where $c;
let $q = delete from t1 where $c;
eval explain select * from t1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
--echo #
--echo # Delete with a limit and an order by
--echo #
let $c = c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
order by c3 desc limit 1;
eval create table tmp as select * from t1 where $c;
let $q = delete from t1 where $c;
eval explain select * from t1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
--echo #
--echo # Delete: 2 execution of PS
--echo #
prepare create_tmp_stmt from
"create table tmp as select * from t1
where c2=(select a.c3 from t1 a where a.c3 = ?)";
prepare delete_t1_stmt from
"delete from t1 where c2=(select a.c3 from t1 a where a.c3 = ?)";
set @a:=5;
execute create_tmp_stmt using @a;
execute delete_t1_stmt using @a;
execute delete_t1_stmt using @a;
--sorted_result
select * from t1;
prepare insert_tmp_stmt from
"insert into tmp(c1,c2,c3) select * from t1
where c2=(select a.c3 from t1 a where a.c3 = ?)";
set @a:=2;
execute insert_tmp_stmt using @a;
execute delete_t1_stmt using @a;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
--sorted_result
select * from t1;
drop table tmp;
--echo #
--echo # Delete in stored procedure
--echo #
delimiter //;
create procedure sp()
begin
delete from t1 where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
order by c3 desc limit 1;
end
//
delimiter ;//
create table tmp as select * from t1
where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
order by c3 desc limit 1;
CALL sp;
insert into tmp(c1,c2,c3) select * from t1
where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
order by c3 desc limit 1;
CALL sp;
insert into tmp(c1,c2,c3) select * from t1
where c1 in (select a.c2 from t1 a where a.c3 = t1.c3)
order by c3 desc limit 1;
CALL sp;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop procedure sp;
drop table tmp;
--echo #
--echo # Delete in stored function
--echo #
delimiter //;
create function f1(IN a INT) returns int
begin
delete from t1 where c3 < a order by c3 limit 1;
return 1;
end;//
delimiter ;//
set @a:=7;
create table tmp as select * from t1 where c3 < @a
order by c3 limit 1;
--disable_ps2_protocol # because SELECT with side effects
select f1(@a);
insert into tmp(c1,c2,c3) select * from t1 where c3 < @a
order by c3 limit 1;
select f1(@a);
--enable_ps2_protocol
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop function f1;
drop table tmp;
--echo #
--echo # Delete in trigger
--echo #
create table t2 (c1 integer);
insert into t2(c1) values (1), (2), (3), (4), (5), (6), (7), (8);
CREATE TABLE cnt(del integer);
INSERT INTO cnt VALUES(0);
CREATE TRIGGER tr1 AFTER DELETE ON t1 FOR EACH ROW
UPDATE cnt SET del=del+1;
CREATE TRIGGER tr2 AFTER DELETE ON t1 FOR EACH ROW
DELETE FROM t2 WHERE c1> (SELECT count(*)-1 FROM t2);
CREATE TABLE tmp as SELECT * FROM t1 WHERE c2>=3;
--enable_info ONCE
DELETE FROM t1 WHERE c2>=3;
--sorted_result
select * from t1;
--sorted_result
SELECT * FROM t2;
SELECT * FROM cnt;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
DROP TRIGGER tr1;
DROP TRIGGER tr2;
drop table t2, cnt, tmp;
--echo #
--echo Delete with a reference to view in subquery
--echo #
let $c = t1.c2 in ( select max(a.c2) from v1 a
where a.c1 = t1.c1);
eval create table tmp as select * from t1 where $c;
let $q = delete from t1 where $c;
eval explain select * from t1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
--echo #
--echo # Delete from view
--echo #
analyze table t1 persistent for all;
let $c = v1.c1 in
(select max(a.c1) from t1 a where a.c2 = v1.c2) and c3 = 5;
eval create table tmp as select * from v1 where $c;
let $q = delete from v1 where $c;
eval explain select * from v1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
--echo #
--echo # Delete from view using reference
--echo # to the same view in subquery
--echo #
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 create table tmp as select * from v1 where $c;
let $q = delete from v1 where $c;
eval explain select * from v1 where $c;
eval explain $q;
--enable_info ONCE
eval $q;
--sorted_result
select * from t1;
insert into t1(c1,c2,c3) select c1,c2,c3 from tmp;
drop table tmp;
|