我爱编程

mysql大量count(*)操作导致CPU打满

2018-03-27  本文已影响0人  GAOCHAO_DBA

异常现象

大量的count(*),每秒40个左右,导致2核CPU全部打满,当前CPU如图


image.png

实例基础信息:

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打入
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出现
root@rmb-db-01:/data/log/mysql3306# grep "count(\*)" server_audit.log 
image.png
上一篇 下一篇

猜你喜欢

热点阅读