select arrival_record 慢查询语句都类似于如下所示,where语句中的参数字段是一样的,传入的参数值不一样 select count(*) from arrival_record where product_id=26 and receive_time between '2019-03-25 14:00:00' and '2019-03-25 15:00:00' and receive_spend_ms>=0\G
传入的过滤条件 where product_id=26 and receive_time between '2019-03-25 14:00:00' and '2019-03-25 15:00:00' and receive_spend_ms>=0 没有station_nu字段,使用不到复合索引 IXFK_arrival_record的 product_id,station_no,sequence,receive_time 这几个字段
IFS_OLD=$IFS IFS=$'\n' for i in `cat /tmp/select_arri.log `;doecho${i#*'from'}; done | less IFS=$IFS_OLD arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=17 and arrivalrec0_.station_no='56742' arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S7100' arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4631' arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S9466' arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4205' arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4105' arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4506' arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4617' arrival_record arrivalrec0_ where
arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S8356' arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S8356' select 该表 where条件中有product_id,station_no,sequence字段,可以使用到复合索引IXFK_arrival_record的前三个字段
2019-04-08-11:17:36 stop slave mysql: [Warning] Using a password on the command line interface can be insecure. ddl_start 2019-04-08 11:17:36ddl_stop 2019-04-08 11:45:132019-04-08-11:45:13 onlie ddl stop 2019-04-08-11:45:13 add foreign key mysql: [Warning] Using a password on the command line interface can be insecure. 2019-04-08-12:33:48 add foreign key stop 2019-04-08-12:33:48 start slave
*再次查看delete 和select语句的执行计划 *
explain select count(*) from arrival_record where receive_time '2019-03-10', '%Y-%m-%d')\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: arrival_record partitions: NULL type: range possible_keys: idx_receive_time key: idx_receive_time key_len: 6 ref: NULL rows: 7540948 filtered: 100.00 Extra: Using where; Using index explain select count(*) from arrival_record where product_id=26 and receive_time between '2019-03-25 14:00:00' and '2019-03-25 15:00:00' and receive_spend_ms>=0\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: arrival_record partitions: NULL type: range possible_keys: idx_product_id_sequence_station_no,idx_receive_time key: idx_receive_time key_len: 6 ref: NULL rows: 291448 filtered: 16.66 Extra: Using index condition; Using where 都使用到了idx_receive_time 索引,扫描的行数大大降低
索引优化后
delete 还是花费了77s时间
delete from arrival_record where receive_time '2019-03-10', '%Y-%m-%d')\G
#得到满足时间条件的最大主键ID #通过按照主键的顺序去 顺序扫描小批量删除数据 #先执行一次以下语句 SELECT MAX(id) INTO @need_delete_max_id FROM `arrival_record` WHERE receive_time<'2019-03-01' ; DELETE FROM arrival_record WHERE id select ROW_COUNT(); #返回20000
#执行小批量delete后会返回row_count(), 删除的行数 #程序判断返回的row_count()是否为0,不为0执行以下循环,为0退出循环,删除操作完成 DELETE FROM arrival_record WHERE id select ROW_COUNT(); #程序睡眠0.5s