清理QA测试后的垃圾数据

开发人员用delete去删除上千万的数据,只为了保留300多条数据,囧

我写了自己折腾的脚本,在测试库先跑了一遍,留个纪念


/*建立索引*/
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE INDEX user_registerDate_index ON info_user_u (user_registerDate);
CREATE INDEX token_createDate_index ON info_token_u (token_createDate);

/*建立临时表*/

DROP TABLE IF EXISTS token_temp;
CREATE TABLE `token_temp` (
  `token_id` VARCHAR(36) NOT NULL,
  ......
  `token_lastUpdateDate` BIGINT(20) DEFAULT NULL,
  PRIMARY KEY (`token_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS user_temp;
CREATE TABLE `user_temp` (
  `user_id` BIGINT(20) DEFAULT 0,
  ......
  PRIMARY KEY (`user_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

/*导出需要保留的数据*/

INSERT INTO user_temp
SELECT * FROM info_user_u WHERE user_id = 10263 OR user_id = 17649 OR user_id = 18032 OR user_id = 18034 OR user_id = 18446 OR user_registerDate >=1307721600000;


INSERT INTO token_temp
SELECT * FROM info_token_u WHERE user_id = 10263 OR user_id = 17649 OR user_id = 18032 OR user_id = 18034 OR user_id = 18446 OR token_createDate >=1307721600000;

/*清空旧表*/
TRUNCATE TABLE log_userplace;
TRUNCATE TABLE inter_related;
TRUNCATE TABLE inter_user_hardware;
TRUNCATE TABLE inter_user_software;
TRUNCATE TABLE inter_user_md;
TRUNCATE TABLE info_token_u;
TRUNCATE TABLE info_user_u;


/*把清理后的数据导入正式库*/

INSERT INTO info_user_u SELECT * FROM user_temp;
INSERT INTO info_token_u SELECT * FROM token_temp;

/*
alter table info_token_u AUTO_INCREMENT=1;
ALTER TABLE info_user_u AUTO_INCREMENT=1;
*/


/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
INSERT INTO `time_log` VALUES('7结束',UNIX_TIMESTAMP());
/*清理结束==========================================================================================================================================*/


/*查看进程*/
SHOW PROCESSLIST;
/*查看表锁竞争*/
SHOW STATUS LIKE 'Table%';
/*查看行锁竞争*/
SHOW STATUS LIKE 'innodb_row_lock%';

你可能感兴趣的:(sql,unix,脚本)