How would i monitor a MySQL to detect SELECTs which are running slowly? Having identified a poorly performing SELECT, how would i analyse it with a view to improving it?
You would enable slow query logging, e.g. add this to your my.cnf under the [mysqld] section
set-variable=slow_query_log=on
set-variable=long_query_time=20
This will log all queries taking 20 seconds or more to a "hostname"-slow.log, e.g. /var/lib/mysql/localhost-slow.log
You'd then inspect those queries, and at least run EXPLAIN on them, and figure out what makes it slow, e.g. add indexes, rewrite the SQL etc. - though optimizing queries is a whole other book.
MySQL keeps a 'slow query log' which will provide you with information about slow running queries.
I think it's enabled by default, and its location is set in your MySQL .ini file.
See here for more information on the slow query log.
You can use EXPLAIN to get information about how MySQL is executing your queries - just put EXPLAIN before your query. There is a good article here on how to interpret the results.
Related
I have two identical databases on separate server machines, and if I execute one query on both machines, on one server it would go smoothly while on the other it would cause slow log. Explain shows me that they are not using same indexes. Any suggestion or advice, it would be helpful.
The index statistics which MySQL keeps, sometimes become inaccurate (I don't know why/when).
Running ANALYZE TABLE <table> on both servers should correct the statistics.
If the problem appears again, you can use index hints and/or IF's to force MySQL to use the correct index.
Logging inefficient queries and query frequency in MySQL
We have a pour performing mysql DB. And I'm interested in logging queries and their frequency to assess which ones my time is best spent on. We have slow query logging but these queries aren't the ones hammering the DB. I've been told statspack for Oracle does exactly that with some sort of weighting on the queries.
Any ideas or suggestions would be appreciated.
The MySQL general query log will show all statements being executed. On a busy server, this will grow quickly, and will slow performance, so you likely want to enable it briefly, and then disable it again. That will give you every statement executed, as well as client connects and disconnects.
http://dev.mysql.com/doc/refman/5.5/en/query-log.html
The slow query log can also be a useful tool.
There are some third party tools that can assist with analyzing the output from the general log, the slow query log, and the output from continually running a SHOW FULL PROCESSLIST. One example is MONyog.
Does mysql server keeps records of queries executed on it, if it does so , is it possible to retrieve those queries.
Thanks.
You can use the General Query Log, which logs all queries in plain text.
We have this enabled on our development environment only. It shouldn't be a problem to turn this on temporarily on a production server during off-peak times.
If you're looking for slow queries, you may as well use the Slow Query Log.
If you want to keep record of all queries that are executed, you can enable either the General Query Log or the Slow Query Log and set the threshold to 0 seconds (any query that takes more than 0 seconds will be logged).
Another option is the Binary log. However, binary log does not keep record of queries like SELECT or SHOW that do not modify data.
Note that these logs get pretty big pretty fast in a server with traffic. And that both might contain passwords so they have to be protected from unauthorized eyes.
You can use MySQL Proxy which stands between client app and RDBMS itself.
http://forge.mysql.com/wiki/MySQL_Proxy
You can see online queries and also it allows you to rewrite queries based on rules.
There's another option - use a profiler.
For instance: http://www.jetprofiler.com/
Is a way to prevent a single query from appearing in mysql slow query log?
One may actually disable logging before executing the query (by setting a global variable) and enable it back after the query, but this would prevent logging in other threads as well, which is not desirable.
Do you have any ideas?
In MySQL 5.1 and later, you can make runtime changes to the time threshold for which queries are logged in the slow query log. Set it to something ridiculously high and the query is not likely to be logged.
SET SESSION long_query_time = 20000;
SELECT ...whatever...
SET SESSION long_query_time = 2;
Assuming 2 is the normal threshold you use.
I don't know if you can prevet a single query from appearing in the slow query log, but you could use a grepped output from the query log. Having said that, if I remember correctly, every slow query is dumped as multiple lines so it would not be easy to grep it out, but not impossible.
mysqldumpslow has a "-g pattern" option to "Consider only queries that match the (grep-style) pattern." which may help in your situation.
I hope this helps.
Cheers
Tymek
Can anyone suggest a good MYSQL optimization tool which helps in finding the bottlenecks in a long query and hence help in optimization?? I am looking for a query profiler.
thanks...
Well, You mean Query Optimization? I guess EXPLAIN <query> is excellent in giving hits as to where the bottlenecks are. After which redefine you indexes & ...
UPDATE1: You could check out - MySQL optimization tools
UPDATE2: After digging up in my code, I see that I used to do 2 things for query optimization.
Turn on Slow Query Log - MySQL can record expensive SQL queries in the slow query log. You can define your expectations in seconds using parameter long_query_time.
mysqldumpslow command - After logging is turned on you can analyze the log contents using mysqldumpslow command. mysqldumpslow /path/to/your/mysql-slow-queries.log -t 10. This will show you top 10 performance killers. For each statement in the output you can see the number of identical calls, execution time in seconds, rows affected and the statement itself.