|
|
It has happened to all of us running a website or application using mysql as its back-end database. Performance is suddenly very sluggish and you have no idea what is causing it. Now there may be other factors that are causing the issue (overloaded CPU, harddrive running out of space, or a lack of bandwidth), but it could also be a query that is not optimized and/or is taking much longer than it should to return.
How do you know which queries are taking the longest to execute? Mysql has built-in functionality for checking this through the slow query log.
To enable (do one of the following):
1) add this to /etc/my.cnf
log-slow-queries=/tmp/slow_queries.log
long_query_time=10
2) call mysqld with –log-slow-queries[=/tmp/slow_queries.log]
long_query_time is the maximum amount of seconds a query can take before it will be logged to the slow query log.
other related options:
If slow query logging has been enabled successfully, you will see “ON” in the VALUE field for “log_slow_queries” (shown above).
Note: Queries handled by the query cache are not added to the slow query log, nor are queries that would not benefit from the presence of an index because the table has zero rows or one row.
You may also run into the case where a query is slow at one time (such as when you are logging it) but not another (if you execute it manually):
Log Analysis
MySQL also comes with mysqldumpslow, a perl script that can summarize the slow query log and provide a better idea of how often each slow query executes.
Justin Silverton is a software engineer from Troy, MI. He is the founder and CEO of Jaslabs, inc. and has a degree in computer science from Oakland University in Rochester Hills, MI. You can reach Justin at justin@jaslabs.com.