Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iteratively optimize a MySQL query?

I'm trying to iteratively optimize a slow MySQL query, which means I run the query, get timing, tweak it, re-run it, get timing, etc. The problem is that the timing is non-stationary, and later executions of the query perform very differently from earlier executions.

I know to clear the query cache, or turn it off, between executions. I also know that, at some level, the OS will affect query performance in ways MySQL can't control or understand. But in general, what's the best I can do wrt this kind of iterative query optimization, so that I can compare apples to apples?

like image 932
shanusmagnus Avatar asked Apr 09 '11 19:04

shanusmagnus


People also ask

How do I optimize a MySQL update query?

To optimize update operations, we should keep the transaction size as short as possible. We need to consider the lock escalation mode of the modified table to minimize the usage of too many resources. Analyzing the execution plan may help to resolve performance bottlenecks of the update query.


2 Answers

Your best tool for query optimization is EXPLAIN. It will take a bit to learn what the output means, but after doing so, you will understand how MySQL's (horrible, broken, backwards) query planner decides to best retrieve the requested data.

Changes in the parameters to the query can result in wildly different query plans, so this may account for some of the problems you are seeing.

You might want to consider using the slow query log to capture all queries that might be running with low performance. Perhaps you'll find that the query in question only falls into the low performance category when it uses certain parameters?

like image 150
Charles Avatar answered Sep 19 '22 15:09

Charles


Create a script that runs the query 1000 times, or whatever number of iterations causes the results to stabilize.

Then follow your process as described above, but just make sure you aren't relying on a single execution, but rather an average of multiple executions, because you're right, the results will not be stable as row counts change, and your machine is doing other things.

Also, try to use a wide array of inputs to the query, if that makes sense for your use case.

like image 44
squawknull Avatar answered Sep 18 '22 15:09

squawknull