MySQL's query cache handles the issue of sending the same query over and over to MySQL. If the query cache is enabled, the result of a query is stored, along with the original query string. For subsequent queries, the server first checks if that exact query has been issued before. If so, the result is simply returned from the query cache, saving all the time of parsing, optimization and execution.
To use the query cache effectively, consider:
- The query strings must be absolutely identical to use the cache. Whitespace and case sensitivity matter.
- Non-deterministic queries (those that can return different results each time they are run) cannot be cached. For this reason, replace non-deterministic functions with constant data wherever possible. For example, change:
WHERE field < CURRENT_DATE()
WHERE field < '2007-07-12'
The latter version might need to be evaluated only once per day.
- You can configure the server to cache all queries except those marked, or to only cache those queries which are explicitly marked. Use the first if you have mostly static data and deterministic queries. Use the second if you have mostly volatile data or non-deterministic queries.
- If the tables involved in a query are changed (data added or deleted), the relevant queries will be pruned from the cache. Space in the cache is limited, so do not allow queries against highly volatile tables to cache.
For more information about the query cache in MySQL, check out the MySQL developer site.
Related Q&A from Scott Noyes
Get suggestions for choosing MySQL character sets and field collations from expert Scott Noyes.continue reading
A MySQL expert describes two ways that the multi-master support can be used.continue reading
An expert describes where to find information on detection deadlock algorithms in MySQL.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.