Home >Database >Mysql Tutorial >mysql delete千万数据操作_MySQL

mysql delete千万数据操作_MySQL

WBOY
WBOYOriginal
2016-06-01 13:33:02928browse

bitsCN.com

mysql delete千万数据操作

 

   在mysql中,delete掉上千万条数据时,会造成表被锁,甚至给mysql服务器带来很大压力。这是目前mysql无法避免的一个问题,可以说是在处理大数据量方面的不足。而在业务中,又无法避免这种delete需求,因此,借用oracle的思想,写存储过程,分而治之,批量删掉。

 

delimiter $$ 

create procedure sp_del_test()

begin 

declare done int default 0;

declare uid_1 int default 0;

declare i int default 0;

 

declare cur_test cursor for select uid from tmp_test_del;

declare continue handler for SQLSTATE '02000' set done=1;

open cur_test;

repeat  fetch cur_test into uid_1;

set autocommit=0;

delete from test_fenye where uid=uid_1;

set i=i+1;

if mod(i,1000)=0 then  

       commit;

end if;

until done=1 end repeat;

close cur_test;

commit;

end;

$$ 

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn