MySQL 存储过程删除大表
1、权限问题
alter routine 编辑或删除存储过程
create routine 建立存储过程
execute 创建存储过程
2、存储过程相关的一些命令
show procedure status\G 查看数据库中有哪些存储过程
show procedure status where db = 'db_name'\G 查看指定数据库(db_name)中有哪些存储过程
select name from mysql.proc where db = 'db_name'; 查看指定数据库(db_name)中有哪些存储过程
select routine_name from information_schema.routines where routine_schema = 'db_name'; 查看指定数据库(db_name)中有哪些存储过程
show create procedure dbname.sp_delete_bakhistory_new \G 查看存储过程的详细内容
3、MySQL 存储过程删除大表
delimiter //
CREATE PROCEDURE `sp_delete_bigtable_60`( )
BEGIN
declare v_id int;
declare stopflag int;
declare v_num int;
declare cursor_id cursor for select id from bigtable where gmt_created<now()-interval 60 day;
declare continue handler for not found set stopflag=1;
open cursor_id;
repeat
fetch cursor_id into v_id;
begin
delete from bigtable where id =v_id;
end;
set v_num=v_num+1;
if mod(v_num,100)=0 then
commit;
BEGIN
declare v_id int;
declare stopflag int;
declare v_num int;
declare cursor_id cursor for select id from bigtable where gmt_created<now()-interval 60 day;
declare continue handler for not found set stopflag=1;
open cursor_id;
repeat
fetch cursor_id into v_id;
begin
delete from bigtable where id =v_id;
end;
set v_num=v_num+1;
if mod(v_num,100)=0 then
commit;
end if;
until stopflag = 1
end repeat;
close cursor_id;
END;
until stopflag = 1
end repeat;
close cursor_id;
END;
//
delimiter ;
delimiter ;
需要注意的是DELIMITER //和DELIMITER ;两句,DELIMITER是分割符的意思,因为MySQL默认以";"为分隔符,如果我们没有声明分割符,那么编译器会把存储过程当成SQL语句进行处理,则存储过程的编译过程会报错,所以要事先用DELIMITER关键字申明当前段分隔符,这样MySQL才会将";"当做存储过程中的代码,不会执行这些代码,用完了之后要把分隔符还原。
如果是用MySQL的Administrator管理工具时,可以直接创建,不再需要声明。