I noticed that when I execute a query more than one time consecutively on my mysql machine, the execution time drops and changes significantly (40/50%) after the first execution, and that WITHOUT query cache activated and no other processes interfering.

I suspect that there is some "hidden" caching involved, like temporary tables, specially when the query involves several subqueries, or maybe some disk IO cache, but I don't understand it really.

Since sometimes I need to fine tune some queries, I don't know how to deal with this problem. What I do usually, is to run the query several times and get rid of the peaks

I'd like to know if there is a better solution...


The operating system uses unused memory as cache. Why is this a problem?


To get times spent with the different part of a query operation, you can set on profiling with

set profiling=1;

and show the profile with show profiles and show profile for query <n> after running a query.

  • well, I'm changing the query in order to optimize it, so during the process I don't want anything to interfere...I want to optimize SQL only, independently from the operating system (it's not the same as to productive system) and anything that is not just SQL. – Glasnhost Oct 13 '14 at 12:39
  • So, you want to optimize a system in a very artificial way that doesn't resemble a usual production system with caches and all? Anyway, look at my edit. – Sven Oct 13 '14 at 12:49
  • not really, I'm trying to understand the effect of changes in the SQL query only. I agree that's artificial,and it's exactly what I want now...This "set profiling" seems to me very good for this, I didn't know it, thanks! – Glasnhost Oct 13 '14 at 15:58

Be careful with your optimization targets, as already mentioned: When you optimize your queries for repeated runs, you will get optimal results for queries which are run several times in short succession (but you might use query cache for that).

By repeating the queries, you "warm up" OS disk caches, so disk seeks are not accounted into your timing any more. This may worsen your results in real scenarios significantly, when several seek operations on busy disks are needed.

side note: You might also want to try "explain select..." which will tell you about used and usable indexes. In most cases, this will help you in determining which additonal indexes to create or how to rewrite your queries.

  • actually I want to optimize for NOT repeated runs. I see some queries that take 1200ms to execute the first time and 200ms if they are repeated shortly after. This is my usage pattern, the query is executed maybe once every few hours, when any cache is lost and that is the moment I want fast performance. Of course explain can give useful hints, but I don't know very well how to test the "cold" disk cache situation – Glasnhost Oct 13 '14 at 16:29
  • I guess this is the most complete answer I found: Query profiling Bypassing caches (percona.com) link – Glasnhost Oct 13 '14 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.