【MySQL】【Aurora】 実行時間が掛かるSQLの監視手法

■ 実行時間が掛かるSQLの監視手法

[1] show full processlist で、実行中のSQLを表示する
[2] slow_query_log / long_query_time を使って、遅いクエリを特定する 

[1] show full processlist で、実行中のSQLを表示する

G オプションでクエリ結果を垂直表示になる
mysql -u root -ppassword -h localhost

mysql> SHOW FULL PROCESSLIST \G;

参考文献

https://qiita.com/akokubu/items/8395cde296c76f263973
http://d.hatena.ne.jp/takami_hiroki/20101027/p1

[2] slow_query_log / long_query_time を使って、遅いクエリを特定する

mysql -u root -ppassword -h localhost

mysql> set global slow_query_log=1;
mysql> set global long_query_time=1;
mysql> exit

service mysqld reload
確認
mysql -u root -ppassword -h localhost

mysql> SHOW VARIABLES LIKE 'slow_query%';
+---------------------+-----------------------------------+
| Variable_name       | Value                             |
+---------------------+-----------------------------------+
| slow_query_log      | ON                                |
| slow_query_log_file | USERNAME-slow.log                 |
+---------------------+-----------------------------------+

mysql> SHOW VARIABLES LIKE 'long_query%';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
遅いSQLで確認
mysql> SELECT SLEEP(2.0), NOW();

【Windowsの場合】
C:\ProgramData\MySQL\MySQL Server 5.7\Data
USERNAME-slow.log
~~~~~~~
# Time: 2018-01-21T05:28:06.494445Z
# User@Host: root[root] @ localhost [127.0.0.1]  Id:     4
# Query_time: 2.004834  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1516512486;
SELECT SLEEP(2.0), NOW();
~~~~~~~

slow_query_log (スロークエリーログ)

 * スロークエリーログは、long_query_time を超えたらログ出力

long_query_time

 * デフォルトは、10[秒]

参考文献

http://gihyo.jp/dev/serial/01/mysql-road-construction-news/0007
https://dev.mysql.com/doc/refman/5.6/ja/slow-query-log.html

関連記事

MySQLMySQL のコマンドあれこれ

https://blogs.yahoo.co.jp/dk521123/34567845.html