Q

MySQL query cache

An expert defines MySQL's query cache and describes when to use it and when to avoid it.

How can MySQL's query cache be used to boost the performance of other applications?

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()

    to

    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.

This was first published in July 2007

Dig deeper on Open source databases

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataCenter

SearchServerVirtualization

SearchCloudComputing

SearchEnterpriseDesktop

Close