• 一个批量删除大表数据的shell小脚本


        通常我们在删除线上的大表数据的时候,如果超过一定数目就不能直接删除,否则可能会造成数据库长时间锁定或者直接导致服务器挂掉。合理的做法应该是分多批次删除,比如下面脚本中是每次删除1000行,然后休息10s种后继续删除。

    点击(此处)折叠或打开

    1. #Purpose: 批量删除大表的数据,减少对线上数据的影响,每次删除1000行数据,然后sleep 10
    2. #Author: Carl Zhang
    3. #Date: 2012-04-09
    4. #Useage: ./delete_bigtable_data.sh 'DELETE FROM test.test WHERE test.datetime <="2012-04-09 00:00:00"'
    5. MYSQL='/usr/local/mysql/bin/mysql'
    6. USER='root'
    7. HOST='localhost'
    8. PASSWORD='zhang@123'
    9. if [ $# -ne 1 ];then
    10. echo "Usage: ./delete_bigtable_data.sh 'DELETE_SQL'"
    11. echo "For example: ./delete_bigtable_data.sh 'DELETE FROM test.test WHERE test.datetime <="2012-04-09 00:00:00"'"
    12. echo "Please try again"
    13. exit 1
    14. fi
    15. DELETE_SQL=$1
    16. echo "DELETE_SQL is $DELETE_SQL"
    17. SELECT_SQL=`echo $DELETE_SQL|sed 's/;$//;s/^\w*/SELECT */;s/$/ LIMIT 1/;s/.*/select count(1) from (&) a/'`
    18. echo "SELECT_SQL is $SELECT_SQL"
    19. LAST_SQL=`echo $DELETE_SQL |sed 's/;$//;s/$/ LIMIT 1000/'`
    20. echo "LAST_SQL is $LAST_SQL"
    21. while [ `$MYSQL -u$USER -p$PASSWORD -h$HOST -ss -e "$SELECT_SQL"` -gt 0 ];do
    22. echo "deleting 1000 rows,please wait"
    23. $MYSQL -u$USER -p$PASSWORD -h$HOST -ss -e "$LAST_SQL"
    24. echo "sleep 10"
    25. sleep 10
    26. done

  • 相关阅读:
    oracle数据类型
    hive的union,hive连接查询的优化,mapjoin的设置参数
    hive分隔符
    hive load数据到表的方式
    hive的常用命令操作与数据类型以及hive的严格模式
    hadoop fs 与 dfs 的区别
    pl/sql程序语法,存储过程, orcle服务
    mysql连接启动
    HTTP协议
    php6
  • 原文地址:https://www.cnblogs.com/feihongwuhen/p/7169866.html
Copyright © 2020-2023  润新知