How to Monitor a MySQL Slow Query

How to Monitor a MySQL Slow Query thumbnail
Use logging to test and optimize your query.

The time it takes a MySQL query to run is a reflection of a number of different factors, from the efficiency of the table design and the size of the fields involved to the number of records the query returns. If you find your MySQL query is running more slowly than it should, you can use the logging function to analyze query performance and tweak it to run faster and more efficiently.

Instructions

    • 1

      Log on to your computer and open the MySQL configuration file. Search for the "My.cnf" file if you do not know where the configuration file is located. Scroll to the "log_slow_queries" setting and change it to "ON."

    • 2

      Go to the "mysqld" section and set the "long_query_time" to 1. This identifies a long query as one taking more than a second to run. Set the "log-slow-queries" setting to "= /var/log/mysql/mysql-slow.log." This specifies the name and location of the log file that will be created any time a query takes more than one second to run.

    • 3

      Run your query and wait for the results. Open the "mysql-slow.log" file if your query took longer than one second to run. Examine the results of that query to find out what is slowing down the results. Tweak and optimize your query, rerun it and examine the log file again. Continue the process until your query is running as efficiently as possible.

Related Searches:

References

  • Photo Credit David Sacks/Lifesize/Getty Images

Comments

Related Ads

Featured