You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.0 KiB

  1. create table t1(c1 integer not null,c2 integer not null, key (c1))
  2. ENGINE=InnoDB STATS_PERSISTENT=1;
  3. create view v1 as select * from t1 where c1 in (0,1);
  4. insert t1 select 0,seq from seq_1_to_500;
  5. insert t1 select 1,seq from seq_1_to_100;
  6. insert t1 select 2,seq from seq_1_to_50;
  7. insert t1 select 3,seq from seq_1_to_20;
  8. analyze table t1;
  9. Table Op Msg_type Msg_text
  10. test.t1 analyze status OK
  11. #
  12. # Delete with limit (quick select - range acces)
  13. #
  14. start transaction;
  15. delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 limit 1;
  16. affected rows: 1
  17. delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 limit 1;
  18. affected rows: 0
  19. select count(*) from v1 where c1=0;
  20. count(*)
  21. 499
  22. rollback;
  23. #
  24. # Delete
  25. #
  26. start transaction;
  27. delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 ;
  28. affected rows: 500
  29. rollback;
  30. #
  31. # Delete with exists
  32. #
  33. start transaction;
  34. select count(*) from v1 where c1=2;
  35. count(*)
  36. 0
  37. delete from t1 where c1=2 and exists(select 'x' from t1 b where b.c2<10);
  38. affected rows: 50
  39. delete from t1 where c1=2 and exists(select 'x' from t1 b where b.c2<10);
  40. affected rows: 0
  41. select count(*) from v1 where c1=2;
  42. count(*)
  43. 0
  44. rollback;
  45. #
  46. # Delete through a view with limit (range access)
  47. #
  48. start transaction;
  49. explain delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1;
  50. id select_type table type possible_keys key key_len ref rows Extra
  51. 1 PRIMARY t1 range c1 c1 4 NULL 600 Using where
  52. 2 DEPENDENT SUBQUERY b ref c1 c1 4 test.t1.c1 83 Using index
  53. delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1;
  54. affected rows: 1
  55. delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 limit 1;
  56. affected rows: 0
  57. select count(*) from v1 where c1=0;
  58. count(*)
  59. 499
  60. rollback;
  61. #
  62. # Delete through a view (ALL access)
  63. #
  64. start transaction;
  65. explain delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500;
  66. id select_type table type possible_keys key key_len ref rows Extra
  67. 1 PRIMARY t1 ALL c1 NULL NULL NULL 670 Using where
  68. 2 DEPENDENT SUBQUERY b ref c1 c1 4 test.t1.c1 83 Using index
  69. delete from v1 where (select count(*) from t1 b where b.c1=v1.c1) = 500 ;
  70. affected rows: 500
  71. select count(*) from v1 where c1=0;
  72. count(*)
  73. 0
  74. rollback;
  75. #
  76. # Delete failed due to trigger
  77. #
  78. create trigger trg after delete on t1 for each row
  79. begin
  80. declare c int;
  81. begin
  82. if old.c1 = 1 then
  83. select count(*) into c from t1 where c1!=old.c1;
  84. SIGNAL SQLSTATE '45000' set table_name=c;
  85. end if;
  86. end;
  87. end;
  88. /
  89. start transaction;
  90. delete from t1 where c1=1 and (select count(*) from t1 b where b.c1=t1.c1) > 0 order by c2 asc limit 10;
  91. ERROR 45000: Unhandled user-defined exception condition
  92. rollback;
  93. start transaction;
  94. delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) > 0 order by c1 desc limit 100;
  95. ERROR 45000: Unhandled user-defined exception condition
  96. select c1,count(*) from t1 group by c1;
  97. c1 count(*)
  98. 0 500
  99. 1 100
  100. 2 50
  101. 3 20
  102. rollback;
  103. drop trigger trg;
  104. #
  105. # Delete through a view with returning
  106. #
  107. start transaction;
  108. delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 order by c2 asc limit 10 returning c1,c2;
  109. c1 c2
  110. 0 1
  111. 0 2
  112. 0 3
  113. 0 4
  114. 0 5
  115. 0 6
  116. 0 7
  117. 0 8
  118. 0 9
  119. 0 10
  120. rollback;
  121. start transaction;
  122. delete from t1 where (select count(*) from t1 b where b.c1=t1.c1) = 500 order by c2 desc limit 10 returning c1,c2;
  123. c1 c2
  124. 0 491
  125. 0 492
  126. 0 493
  127. 0 494
  128. 0 495
  129. 0 496
  130. 0 497
  131. 0 498
  132. 0 499
  133. 0 500
  134. rollback;
  135. drop view v1;
  136. drop table t1;
  137. #
  138. # Delete from table with more than 150000 rows
  139. #
  140. create table t1(c1 integer not null,c2 integer not null, key (c1));
  141. insert t1 select 0,seq from seq_1_to_128000;
  142. insert t1 select 1,seq from seq_1_to_25600;
  143. select count(*) from t1;
  144. count(*)
  145. 153600
  146. # with a lot of memory for sort_buffer_size
  147. set session sort_buffer_size = 1024000;
  148. delete from t1 where c1=0 and exists(select 'x' from t1 b where b.c1<10);
  149. affected rows: 128000
  150. # with little memory for sort_buffer_size
  151. insert t1 select 0,seq from seq_1_to_128000;
  152. set session sort_buffer_size = 1024;
  153. delete from t1 where c1=0 and exists(select 'x' from t1 b where b.c1<10);
  154. affected rows: 128000
  155. drop table t1;