1. 问题描述:现在存在两个表,具体表结构及记录数如下所示:

mysql> desc user_mapping;
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| open_id | varchar() | NO | PRI | NULL | |
| platform | tinyint() | NO | PRI | NULL | |
| serverid | int() unsigned | NO | PRI | | |
| uid | int() unsigned | NO | | NULL | |
| updatetime | int() | YES | | NULL | |
| lastlogin | int() | YES | | NULL | |
| via | varchar() | YES | | NULL | |
+------------+------------------+------+-----+---------+-------+
rows in set (0.00 sec)
mysql> select count(*) from user_mapping;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (2.49 sec)
mysql> desc uid1202;
+-----------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+----------------------+------+-----+---------+-------+
| uid | int() unsigned | NO | | NULL | |
| last_login_time | int() unsigned | YES | | NULL | |
| accCharge | int() unsigned | YES | | | |
| level | smallint() unsigned | YES | | NULL | |
+-----------------+----------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql> select count(*) from uid1202;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (3.25 sec)

目的是将user_mapping表中的uid在uid1202表中存在的记录删除,mysql处理大数据时,多表连接会直接将服务器单核卡死,而且还不知道猴年马月才能处理完。

解决方案:

step1. 将user_mapping表和uid1202表dump到内网,建新库导入

step2. 去掉内网user_mapping表中的主键

alter table user_mapping drop primary key;

step3. 删除内网user_mapping表中uid重复的值删除保留一条

mysql -uusername -ppassword -e"select platform,uid from user_mapping group by uid having count(*) > 1 ;" > uid_double.txt
awk '{print "delete from user_mapping where platform="$1" and uid="$2";"}' uid_double.txt > del_double.sql
mysql -uusername -ppassword user_del < del_double.sql

step4. 修改user_mapping表,重新建立以uid为主键

alter table user_mapping add primary key(uid);

step4. 构造查询user_mapping表中uid在uid1202表中的语句

mysql -uusername -ppassword user_del -e"select uid from uid1202" > uid.txt
awk '{print "select open_id,platform,serverid from user_mapping where uid="$1"; "}' uid.txt > del_uid.sql

step5. 构造删除user_mapping表中以open_id,platform,serverid为条件的语句

mysql -uroot -p1234 user_del < del_uid.sql > del_usermapping.txt
sed -i '/open_id/d' del_usermapping.txt #删除奇数行table头
awk '{print "delete from user_mapping where open_id=\""$1"\" and platform="$2" and uid="$3" ;"}' del_usermapping.txt > del_usermapping.txt

step6. 分解查询语句到多个sql文件,在外网同时运行

#!/bin/bash
for i in $(seq )
do
cat del_usermapping.sql | head -n > del_usermapping_$i.sql
sed -i '1,1300000d' del_usermapping.sql
cat del_usermapping_$i.sql | wc -l
done for i in $(seq )
do
mysql -uroot -p1234 user_del < del_usermapping_$i.sql &
done

mysql海量数据条件删除的更多相关文章

  1. 在SQLSERVER中快速有条件删除海量数据技巧推荐

    解释: 如果你的硬盘空间小,并且不想设置数据库的日志为最小(因为希望其他正常的日志希望仍然记录),而且对速度要求比较高,并清除所有的数据建议你用turncate table1,因为truncate 是 ...

  2. MYSQL中delete删除多表数据

    MYSQL中delete删除多表数据 DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释: 1. delete from t1 where 条件 2.delete t1 ...

  3. 第二百八十七节,MySQL数据库-条件语句、循环语句、动态执行SQL语句

    MySQL数据库-条件语句.循环语句.动态执行SQL语句 1.if条件语句 delimiter \\ CREATE PROCEDURE proc_if () BEGIN ; THEN ; ELSEIF ...

  4. mysql 清空或删除表数据后,控制表自增列值的方法

    http://blog.sina.com.cn/s/blog_68431a3b0100y04v.html 方法1: truncate table 你的表名 //这样不但将数据全部删除,而且重新定位自增 ...

  5. 关于MySQL数据被删除后空间重用的问题实验

    以前知道,MySQL在通过delete语句删除数据后,空间并不会被腾出,而只是在数据文件中被标记为已删除,除非执行optimize table.前两天听说,虽然delete数据后硬盘空间不会被腾出,但 ...

  6. Java根据条件删除Map中元素

    今天在写程序过程中,需要根据判断条件删除一个Map中的相应数据,我自然而然想到可以通过调用Map中的remove(Object key)函数进行删除:代码如下: public Map<Doubl ...

  7. oracle 快速删除大批量数据方法(全部删除,条件删除,删除大量重复记录)

    oracle 快速删除大批量数据方法(全部删除,条件删除,删除大量重复记录) 分类: ORACLE 数据库 2011-05-24 16:39 8427人阅读 评论(2) 收藏 举报 oracledel ...

  8. Mysql创建、删除用户

    1.新建用户 //登录MYSQL@>mysql -u root -p@>密码//创建用户mysql> insert into mysql.user(Host,User,Passwor ...

  9. paip.提升性能--- mysql 建立索引 删除索引 很慢的解决.

    paip.提升性能--- mysql 建立索引 删除索引 很慢的解决. 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blo ...

随机推荐

  1. C语言的运算符、位操作

    + - * / (加 减 乘 除) > >= < <= (大于 大于等于 小于 小于等于) == != (测试等于 测试不等于) && || ! (逻辑与 逻辑 ...

  2. 命令行解析函数:getopt/getopt_long

    参考: http://blog.csdn.net/zhangyang0402/article/details/5671410 http://www.cnblogs.com/gnuhpc/archive ...

  3. VBA 时间相关的函数

    DateSerial DateADD Datediff http://www.yiibai.com/vba/vba_datediff_function.html https://www.techont ...

  4. java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpg、png等结尾的图片)

    Java代码: File filePic = new File(path+"1-ab1.png"); if(filePic.exists()){ FileInputStream i ...

  5. 使用Spring的AbstractRoutingDataSource实现多数据源切换

    https://www.cnblogs.com/softidea/p/7127874.html?utm_source=itdadao&utm_medium=referral https://b ...

  6. Android Scroll具体解释(二):OverScroller实战

    作者: ztelur 联系方式:segmentfault,csdn.github 本文仅供个人学习,不用于不论什么形式商业目的,转载请注明原作者.文章来源.链接,版权归原文作者全部.  本文是andr ...

  7. 《大话操作系统——做坚实的project实践派》(3)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG1ub3M=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  8. Android中IntentService详解

    简单说,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service ...

  9. 【JavaScript】【PPT】继承的本质

    ※文件引自OneDrive,有些人可能看不到

  10. Makefile.am, Makefile.in 与 Makefile的关系(转)

    文章出处:http://blog.mcuol.com/User/wangguangdong/Article/17384_1.htm Makefile.am, Makefile.in, Makefile ...