DROP PROCEDURE IF EXISTS prc_del_loop;
CREATE PROCEDURE prc_del_loop()
BEGIN
DECLARE v_count INT ;
DECLARE v_data_free INT;
my_del_loop:LOOP
delete from t_data where adress = '苏州日志分析苏州日志分析' limit 10000;
select count(1) into v_count from (
select * from t_data where adress = '苏州日志分析苏州日志分析' limit 1 ) d;
IF v_count=0 THEN
LEAVE my_del_loop;
END IF;
END LOOP my_del_loop;
SELECT "TABLE DATA HAD DELETED !!!";
#整理碎片,data_free大于等于100M进行整理
select round(sum(data_free/1024/1024),0) into v_data_free
from information_schema.tables where TABLE_SCHEMA= 'data' and table_name = 't';
IF v_data_free >=100 THEN
SELECT "TABLE SHRINK IS RUNNING!!!";
alter table t ENGINE=INNODB;
SELECT "TABLE SHRINK IS FINISHED!!!";
ELSE
SELECT "TABLE SHRINK IS NOT NEED SHRINK!!!";
END IF;
END;
使用游标来进行删除:
drop PROCEDURE if EXISTS prc_del_loop_cur;
CREATE PROCEDURE prc_del_loop_cur()
BEGIN
DECLARE v_name VARCHAR(2000);
DECLARE v_id BIGINT;
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT id,name from test where adress = '苏州日志分析苏州日志分析';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
open cur1;
my_del_loop:LOOP
fetch cur1 into v_id,v_name;
IF done=1 THEN
LEAVE my_del_loop;
END IF;
delete from test where id = v_id;
END LOOP my_del_loop;
CLOSE cur1;
SELECT "delete over !!!";
END;
call prc_del_loop_cur;