The slow query log, as the name implies, records the log of the slow query execution.
Check whether the slow query log is enabled:
show variables like '%slow%';
Open the slow query log. Modifying the MySQL configuration file my.cn is generally under the /etc directory, and restart MySQL after adding the following three lines of configuration.
slow_query_log = ON slow_launch_time = 2 slow_query_log_file = /usr/local/mysql/data/slow_query.log
slow_launch_time can only be accurate to seconds. If you need more precision, you can use some third-party tools such as pt-query-digest described later.
Note: The MySQL version I use here is 5.6. Different versions of MySQL have different configurations for enabling slow queries. For example, some versions before 5.6 are ong_query_time, long_query_time and log-slow-queries. You can first execute show variables like '%slow%' in the terminal; to see what the specific configuration of the current version is.
You can also open the slow query log in the terminal by setting a global variable:
set @@global.slow_query_log = ON;
Save the slow query log to a table
MySQL supports saving the slow query log to the mysql.slow_log table. Through @@global.log_output can be set to default to TABLE, only one of FILE and TABLE can be used at the same time.
set @@global.log_output='TABLE';
Reference: http://blog.csdn.net/wulantian/article/details/40296541