MySql slow_log查询分组

时间:2016-11-23 12:51:44

标签: mysql performance count grouping

我正在尝试收集有关我数据库中性能最差的查询的一些统计信息。 我已经开启了MySQL的慢速查询日志记录,并且一直在监视数据库中的slow_log表。

我正在尝试获取每天唯一查询计数的计数,如下所示:

Date              count      query
2016-11-20        15         'SELECT * FROM XYAA'
2016-11-20        196        'SELECT count(*) FROM BLAH'
2016-11-19        7          'SELECT * FROM XYAA'
2016-11-19        296        'SELECT count(*) FROM BLAH' 

slow_log表结构:

CREATE TABLE `slow_log` (
  `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `user_host` mediumtext NOT NULL,
  `query_time` time NOT NULL,
  `lock_time` time NOT NULL,
  `rows_sent` int(11) NOT NULL,
  `rows_examined` int(11) NOT NULL,
  `db` varchar(512) NOT NULL,
  `last_insert_id` int(11) NOT NULL,
  `insert_id` int(11) NOT NULL,
  `server_id` int(10) unsigned NOT NULL,
  `sql_text` mediumtext NOT NULL,
  `thread_id` bigint(21) unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log' |

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT DATE(start_time) as `date`,
       COUNT(*) as `count`,
       sql_text as query
FROM `slow_log`
GROUP BY DATE(start_time) , sql_text