Enable Logging of Slow Queries in MySQL Database

Any non-optimal SQL  commands or statements that take too long or lengthy time to execute will use up a lot of system resources, causing MySQL database to run slower.

MySQL has built-in functionality to capture slow query log or identify queries that take a long time to execute. You can enable the full SQL queries logs to a file or only slow running queries log.

To enable you just need to add some lines to your my.cnf file, and restart. Add the following:

* To enable slow Query Log only

log-slow-queries = /var/log/mysql/mysql-slow.log
long_query_time = 1

After enabling slow query, mysqld writes a statement to the slow query log file and it consists of all SQL statements that took more than long_query_time seconds to execute. The time to acquire the initial table locks is not counted as execution time. mysqld only log after SQL statements has been executed and after all locks have been released, so log order might be different from execution order. The minimum and default values of long_query_time are 1 and 10, respectively.

  * To enable full Log Query

log=/var/log/mysqldquery.log

The above will log all queries to the log file.

Restart MySql after making the changes.

Both comments and pings are currently closed.

Comments are closed.