您好,登录后才能下订单哦!
本文主要给大家介绍mysql数据批量删除并且整理表碎片方法,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下mysql数据批量删除并且整理表碎片方法吧。
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;
看完以上关于mysql数据批量删除并且整理表碎片方法,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。