Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20859
  • 博文数量: 14
  • 博客积分: 145
  • 博客等级: 入伍新兵
  • 技术积分: 105
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-07 11:54
文章分类

全部博文(14)

文章存档

2012年(14)

我的朋友

分类:

2012-08-06 12:35:20

    通常我们在删除线上的大表数据的时候,如果超过一定数目就不能直接删除,否则可能会造成数据库长时间锁定或者直接导致服务器挂掉。合理的做法应该是分多批次删除,比如下面脚本中是每次删除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

阅读(262) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~