Detecting Slow Queries, Analyzing Them with EXPLAIN, and Notifying Slack
This is a follow-up to Notifying Slack of error logs with fluentd.

Overview
I set this up so that I could check the slow query status of a MySQL DB server
in real time on Slack.
Environment
- CentOS 6.5
- td-agent 0.12.26
Installing the Fluent Plugins
Install the modules required this time.
1 | # td-agent-gem install fluent-plugin-nata2 |
fluent-plugin-nata2
- Strips out
SET timestampin advance and also retains information about the DB being accessed - [https://github.com/studio3104/fluent-plugin-nata2]
- Strips out
fluent-plugin-mysql_explain
- Retrieves the EXPLAIN execution result for the
sqlattribute of the JSON obtained byin_mysqlslowquery_ex - [https://github.com/kikumoto/fluent-plugin-mysql_explain]
[https://github.com/kikumoto/fluent-plugin-sql_fingerprint]
- Retrieves the EXPLAIN execution result for the
fluent-plugin-sql_fingerprint
- Masks the parameters of the SQL
- [https://github.com/kikumoto/fluent-plugin-sql_fingerprint]
Installing the Percona Toolkit
Install the fingerprint tool used by fluent-plugin-sql_fingerprint.
1 | # rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm |
Creating the fluentd Configuration File
Let’s configure the following file.
- /etc/td-agent/conf.d/mysql.conf
1 | <source> |
*Change the slow query path, the DB access account, and so on to match your own environment.
Restarting td-agent
1 | # service td-agent restart |
Verification
Issue a slow query and check whether it gets notified to Slack.
- We sleep for 3 seconds so that it exceeds the
long-query-timethreshold (in seconds) configured in my.cnf.
1 | mysql > SELECT count(*), sleep(3) FROM <table>; |
- Checking the Slack notification
It was notified to Slack!
When you click “show more”, you can see that the EXPLAIN result is also notified.
Wrap-up
Real-time notifications were especially effective during new development.
Also, accumulating the data in ElasticSearch and analyzing it along a time axis is interesting, since you can see the correlation with site events.
You start to see the slow queries that occur depending on the environment and the situation.
That’s all.
Detecting Slow Queries, Analyzing Them with EXPLAIN, and Notifying Slack
https://kenzo0107.github.io/en/2016/08/24/detect-slowquery-to-slack/
