异常现象
大量的count(*),每秒40个左右,导致2核CPU全部打满,当前CPU如图
image.png
实例基础信息:
- 数据库版本:MariaDB10.0.27
- 硬件信息:2核cpu+4G内存+100GSSD
- 数据库中数据量> innodb buffer pool配置
- 表结构及数据量
CREATE TABLE `xxxxxx` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
`ctime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '创建时间',
`utime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '更新时间',
`friend_status` int(11) NOT NULL DEFAULT '1' COMMENT '好友关系状态;0:无关联;1:单向好友;2:双向好友;',
`from_user_address` varchar(128) NOT NULL DEFAULT '' COMMENT 'xxxxxx',
`to_user_address` varchar(128) NOT NULL DEFAULT '' COMMENT 'xxxxxxx',
`from_user_id` char(36) NOT NULL COMMENT 'xxxxx',
`to_user_id` char(36) NOT NULL COMMENT 'xxxxx',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_multi` (`from_user_id`,`to_user_id`),
KEY `idx_to_user_id` (`to_user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1968705 DEFAULT CHARSET=utf8 COMMENT='xxxx'
MariaDB [turin]> select count(*) from friends;
+----------+
| count(*) |
+----------+
| 1969434 |
+----------+
1 row in set (1.41 sec)
异常排查
- 查看是否有慢查询
tail -f slow.log #观察1分钟,无慢sql打入
- 查看server audit日志
20180327 10:21:52,rmb-db-01,fengchao_rw,192.168.80.41,92603,292637843,QUERY,turin,'select count(*) from friends WHERE ( friend_status = 2
and from_user_id = \'0d6e93691ff145488fb8054d12422654\' )',0
#发现有count(*) sql出现
- 查看count(*) 每秒执行次数
root@rmb-db-01:/data/log/mysql3306# grep "count(\*)" server_audit.log
image.png
- 结论:每秒执行count(*)计算次数太多
- 解决:与开发沟通将该类SQL用redis实现
网友评论