USA - Toll Free: +1-866-221-0634
USA - From abroad: +1-408-701-9009
USA - Subscription Renewals: +1-866-830-4410
Latin America: +1 512 535 7751
UK: +44 845 399 1124
Ireland: +353 1 6919191
Germany: +49 89 420 95 98 95
France: +33 1 70 38 72 00
Sweden: +46 730 207 871
Benelux: +358 50 5710 528
Italy: +39 06-99268193
Israel: +358 50 5710 528
Spain & Portugal: + 34 933905461
Other EMEA countries: +353 1 6919191
Asia Pacific: +81 3 5918 7507
Learn about new MySQL releases, technical articles, events and more.
If your application executes several different queries, changes are, that one or two of the queries might be slowing the total query log, because they are not properly optimized. But there is a way, how you can by yourself find out, which queries need optimization.
First you need to turn the query log on, or if you want to just see the slow queries, turn on the slow log. The MySQL log files are explained at http://www.mysql.com/doc/en/Log_Files.html
When you've run your application, take a look at the log and try to spot distinct query types. By distinct types we mean queries which are different more than just in some constants.
I.e.
select * from a where id=5
and
select * from b where id=7
fall in the same type and you'll need to analyze only one of them.
The next step is to select the queries which are the best candidates for optimization. This is usually done by the following criterias:
* Query is very often executed in your application
* Query scans large amount of rows, especially if this results just in a few rows sent. Numbers more than 1000-10000 shall be treated as large based on your application
* Queries which just are the slowest - taking largest amount of time to execute.
As soon as you've spotted the best candidates for optimization run EXPLAIN SELECT .... to get info how it is optimized.
If you have UPDATE/DELETE query EXPLAIN will not work, but you can rewrite it to SELECT query to make it work.
E.g.
update a set t=5 where b=7 and d=8
can be rewritten for EXPLAIN to work as:
select * from a where b=7 and d=8
You can read in more detail about EXPLAIN at
http://www.mysql.com/doc/en/EXPLAIN.html
