您好,欢迎访问一九零五行业门户网

mysql高效删除大数据量表中的重复数据_MySQL

boss_t_tour表目前有150w数据,其中出现了15000多条有重复记录的数据,需要删除其中的8000多条多余的记录。
如果删除小表,不担心效率,可以用下面方式删除,
http://jimmy9495.iteye.com/admin/blogs/2072785
但是用上面的sql如果想在大表操作删除,肯定是不行的。
mysql大数据表中的快速删除部分数据办法:
1.创建删除重复的存储过程
delimiter $$use `bossdb` $$drop procedure if exists `del` $$create definer = `root` @`localhost` procedure `del` () begin/** 定义后面循环用到的变量*/declare coun1 int ;declare count2 int ;drop table if exists tmp_imsi;drop table if exists tmp_all;drop table if exists tmp_keep;drop table if exists tmp_delete;/** 创建临时表*/create table tmp_imsi as select t.imsi as imsi from boss_t_tour t where t.imsi '' group by t.imsi having count(t.imsi) >1; create table tmp_all as select t.id,t.imsi as imsi from boss_t_tour t where t.imsi in (select imsi from tmp_imsi); create table tmp_keep as select min(a.id) as id from tmp_all a group by a.imsi; create table tmp_delete as select a.id as id from tmp_all a where a.id not in (select id from tmp_keep) ;/** 先删除索引提高删除速度*/alter table `bossdb`.`boss_t_tour` drop index `imsi_index`,drop index `syncstatusinded`;/** 循环删除开始*//** 原计划用此子查询删除,delete from boss_t_tour where exists (select 1 from tmp_delete where boss_t_tour.id = tmp_delete.id);*//** 但是发现mysql子查询删除效率奇慢,本机测试主表150w数据 临时表8000条数据 删除3000条用了一个小时,效率太差不敢在生产环境使用。*//** 用下面的循环删除效率高很多,删除8000多条数据5分钟 */select count(*) into coun1 from tmp_delete;while coun1 > 0 do select id into count2 from tmp_delete limit 1 ;delete from boss_t_tour where id = count2 ;delete from tmp_delete where id = count2 ;commit ;set coun1 = coun1 - 1 ;end while ;/** 循环删除结束*//** 重建索引*/alter table `bossdb`.`boss_t_tour` addindex `imsi_index` (`imsi`),addindex `syncstatusinded` (`sync_status`);/** 删除临时表*/drop table if exists tmp_imsi;drop table if exists tmp_all;drop table if exists tmp_keep;drop table if exists tmp_delete;end $$delimiter ;
2.执行存储过程
call del();
本机执行5分钟完成。
写的过程中还发现个问题mysql的delete操作居然不能给 表定义别名。。。
其它类似信息

推荐信息