我有一个MySQL数据库,其中有一个包含大约3亿行的InnoDB表。有多达10个连接的客户端每秒发送50-60个读取查询。几个月以来,一切都很顺利,直到最近,MySQL开始停滞,同时使用了大量的CPU (100%+ )。uptime命令显示像15、12、15这样的值。需要500 to的查询现在需要几秒钟,从几十秒到数百秒不等。执行SHOW PROCESSLIST显示在Sending data状态下挂起的查询。
我不知道为什么,任何帮助都很感激。
服务器
英特尔(R) Xeon(R) CPU E5 @ 2.40GHz 12 CPU\ 32 GB内存
my.cnf
innodb_file_per_table = 1
tmp-table-size = 32M
max-heap-table-size = 32M
innodb-log-files-in-group = 2
innodb-flush-method = O_DIRECT
innodb-log-file-size = 512M
innodb-buffer-pool-size = 26G
innodb-flush-log-at-trx-commit = 2
innodb-file-per-table = 1
innodb_file_format = barracuda 表(名称:记录)
+----------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| identifier | int(11) | YES | MUL | 0 | |
| timestamp | int(11) | YES | MUL | NULL | |
| rtype | int(5) | YES | MUL | NULL | |
| x1 | int(11) | YES | | NULL | |
| x2 | int(11) | YES | | NULL | |
| net | bigint(20) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
+----------------+------------+------+-----+---------+----------------+索引并在WHERE查询中使用:
数据大小
Data_length = ~18 GB
Index_length = ~16 GB查询
SELECT identifier, timestamp, x1 AS a, x2 AS b, net
FROM records
WHERE
identifier=1010
AND timestamp >=1463111100
AND timestamp <= 1463738400
AND rtype=5
ORDER BY timestamp;(返回约900行。有时在不到一秒钟的时间内完成,有时是10-10秒)
查询分析
select_type = SIMPLE
type = index_merge
possible_keys = indeXidentifier, indeXtimestamp, indeXrtype
key = indeXidentifier, indeXrtype
key_len = 4,5
rows = 10641
Extra = Using intersect(indeXidentifier,indeXrtype); Using where发布于 2016-06-04 18:57:35
我有两个建议:
1.更改多列索引中的列顺序。推荐的顺序是:标识符、rtype、时间戳。
索引唯一扫描比索引范围扫描快,所以最好先出现。
2.将查询更改如下:
select * from(
SELECT identifier, timestamp, x1 AS a, x2 AS b, net
FROM records
WHERE
identifier=1010
AND timestamp >=1463111100
AND timestamp <= 1463738400
AND rtype=5
) t1 ORDER BY timestamp;以避免使用索引进行排序。
https://stackoverflow.com/questions/37631881
复制相似问题