mysql上亿记录_【MySQL】谈谈上亿⼤表的优化实践
当MySQL单表记录数过⼤时,增删改查性能都会急剧下降,这个时候就需要进⾏优化了,那今天我们就来谈谈如何优化上亿数据的⼤表。
(说明:采⽤Mysql存储千亿级的数据,确实是⼀项⾮常⼤的挑战。Mysql单表确实可以存储10亿级的数
据,只是这个时候性能⾮常差,项⽬中⼤量的实验证明,Mysql单表容量在500万左右,性能处于最佳状态。)
背景
XX实例(⼀主⼀从)xxx告警中每天凌晨在报SLA报警,该报警的意思是存在⼀定的主从延迟(若在此时发⽣主从切换,需要长时间才可以完成切换,要追延迟来保证主从数据的⼀致性)
XX实例的慢查询数量最多(执⾏时间超过1s的sql会被记录),XX应⽤那⽅每天晚上在做删除⼀个⽉前数据的任务
分析
使⽤pt-query-digest⼯具分析最近⼀周的mysql-slow.log
pt-query-digest --since=148h mysql-slow.log | less结果第⼀部分
最近⼀个星期内,总共记录的慢查询执⾏花费时间为25403s,最⼤的慢sql执⾏时间为266s,平均每个慢sql执⾏时间5s,平均扫描的⾏数为1766万
结果第⼆部分
lect arrival_record操作记录的慢查询数量最多有4万多次,平均响应时间为4s,delete arrival_record记录了6次,平均响应时间258s
lect xxx_record语句
lect arrival_record 慢查询语句都类似于如下所⽰,where语句中的参数字段是⼀样的,传⼊的参数值不⼀样
lect 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
葡萄苗种植方法
lect
arrival_record 语句在mysql中最多扫描的⾏数为5600万、平均扫描的⾏数为172万,推断由于扫描的⾏数多导致的执⾏时间长
查看执⾏计划
explain lect 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
lect_type: SIMPLE
table: arrival_record
partitions: NULL
type: ref
possible_keys: IXFK_arrival_record
key: IXFK_arrival_record儿童侦探故事
春天里简谱>原画场景
key_len: 8
ref: const
rows: 32261320
filtered: 3.70
Extra: Using index condition; Using where
1 row in t, 1 warning (0.00 c)
⽤到了索引IXFK_arrival_record,但预计扫描的⾏数很多有3000多w⾏
物理用英语怎么说show index from arrival_record;
+----------------+------------+---------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------------+------------+---------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| arrival_record | 0 | PRIMARY | 1 | id | A | 107990720 | NULL | NULL | | BTREE | | |
| arrival_record | 1 | IXFK_arrival_record | 1 | product_id | A | 1344 | NULL | NULL | | BTREE | | |
| arrival_record | 1 | IXFK_arrival_record | 2 | station_no | A | 22161 | NULL | NULL | YES | BTREE | | |
| arrival_record | 1 | IXFK_arrival_record | 3 | quence | A | 77233384 | NULL | NULL | | BTREE | | |
| arrival_record | 1 | IXFK_arrival_record | 4 | receive_time | A | 65854652 | NULL | NULL | YES | BTREE | | |
| arrival_record | 1 | IXFK_arrival_record | 5 | arrival_time | A | 73861904 | NULL | NULL | YES | BTREE | | |
+----------------+------------+---------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
show create table arrival_record;
..........arrival_spend_ms bigint(20) DEFAULT NULL,total_spend_ms bigint(20) DEFAULT NULL,
PRIMARY KEY (id),
KEY IXFK_arrival_record (product_id,station_no,quence,receive_time,arrival_time) USING BTREE,
CONSTRAINT FK_arrival_record_product FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=614538979 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
---
该表总记录数约1亿多条,表上只有⼀个复合索引,product_id字段基数很⼩,选择性不好
传⼊的过滤条件 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,quence,receive_time 这⼏个字段
根据最左前缀原则,lect arrival_record只⽤到了复合索引IXFK_arrival_record的第⼀个字段product_id,⽽该字段选择性很差,导致扫描的⾏数很多,执⾏时间长
receive_time字段的基数⼤,选择性好,可对该字段单独建⽴索引,lect arrival_record sql就会使⽤到该索引
小牙刷手中拿儿歌现在已经知道了在慢查询中记录的lect arrival_record where语句传⼊的参数字段有
product_id,receive_time,receive_spend_ms,还想知道对该表的访问有没有通过其它字段来过滤了?
神器tcpdump出场的时候到了
使⽤tcpdump抓包⼀段时间对该表的lect语句
tcpdump -i bond0 -s 0 -l -w - dst port 3316 | strings | grep lect | egrep -i 'arrival_record' >/tmp/lect_arri.log
获取lect 语句中from 后⾯的where条件语句
IFS_OLD=$IFS
IFS=$'\n'
经济类书籍
for i in `cat /tmp/lect_arri.log `;do echo ${i#*'from'}; done | less
IFS=$IFS_OLD
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.product_id=17 and arrivalrec0_.station_no='56742'
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.produc
t_id=22 and arrivalrec0_.station_no='S7100'
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4631'
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S9466'
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4205'
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4105'
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4506'
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4617'
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S8356'
arrival_record arrivalrec0_ where arrivalrec0_.quence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S8356'
综上所⽰,优化⽅法为,删除复合索引IXFK_arrival_record,建⽴复合索引idx_quence_station_no_product_id,并建⽴单独索引indx_receive_time
delete xxx_record语句
该delete操作平均扫描⾏数为1.1亿⾏,平均执⾏时间是262s
delete语句如下所⽰,每次记录的慢查询传⼊的参数值不⼀样
delete from arrival_record where receive_time < STR_TO_DATE('2019-02-23', '%Y-%m-%d')\G
执⾏计划
explain lect * from arrival_record where receive_time < STR_TO_DATE('2019-02-23', '%Y-%m-%d')\G *************************** 1. row ***************************
id: 1
lect_type: SIMPLE
table: arrival_record
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 109501508
filtered: 33.33
Extra: Using where
1 row in t, 1 warning (0.00 c)
该delete语句没有使⽤索引(没有合适的索引可⽤),⾛的全表扫描,导致执⾏时间长
优化⽅法也是 建⽴单独索引indx_receive_time(receive_time)
测试
拷贝arrival_record表到测试实例上进⾏删除重新索引操作XX实例arrival_record表信息
lect count() from cq_new_cimiss.arrival_record;
+-----------+
| count() |
大数据的概念+-----------+
| 112294946 |
+-----------+1亿多记录数
SELECT
table_name,
CONCAT(FORMAT(SUM(data_length) / 1024 / 1024,2),'M') AS dbdata_size,
CONCAT(FORMAT(SUM(index_length) / 1024 / 1024,2),'M') AS dbindex_size,
CONCAT(FORMAT(SUM(data_length + index_length) / 1024 / 1024 / 1024,2),'G') AS table_size(G), AVG_ROW_LENGTH,table_rows,update_time
FROM
information_schema.tables
WHERE table_schema = 'cq_new_cimiss' and table_name='arrival_record';