1.注意过滤字段添加索引,否则执行效率低
CREATE INDEX INX_xxx ON xxxtable (字段CREATED_AT );
2.存储过程,按照日期删除,每次删除1000条
--创建存储
drop procedure if exists p_delete_is_del;
delimiter $$
create procedure p_delete_is_del(in table_name varchar(64))
begin
set @sql = concat('delete from ', table_name, ' where CREATED_AT < ''2024-03-01'' LIMIT 1000');
prepare statement_remove_is_del from @sql;
-- select @sql;
r_loop: loop
execute statement_remove_is_del;
if row_count() = 0 then
leave r_loop;
end if;
commit;
end loop r_loop;
deallocate prepare statement_remove_is_del;
end$$
delimiter ;
3.执行存储过程,将对应表传入
call p_delete_is_del(‘abc’);