Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Is Your Query Cache Really Disabled?

DZone's Guide to

Is Your Query Cache Really Disabled?

This blog post was motivated by an internal discussion about how to fully disable query cache in MySQL.

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

This blog post was motivated by an internal discussion about how to fully disable query cache in MySQL.

According to the manual, we should be able to disable “Query Cache” on the fly by changing query_cache_type to 0, but as we will show this is not fully true. This blog will show you how to properly disable “query cache,” and how common practices might not be as good as we think.

Can we just disable it by changing variables, or does it requires a restart to avoid the global mutex? Let’s see how it works.

Some Query Cache Context

The query cache stores the text of a “Select” statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again. The query cache is shared among sessions, so a result set generated by one client can be sent in response to the same query issued by another client.

But cacheable queries take out an “exclusive lock” on MySQL’s query cache. In addition, any insert, update, delete or other modifications to a table causes any relevant entries in the query cache to be flushed. If you see many “Waiting for query cache lock” in the process list, you might be suffering from this exclusive lock. In this blog post, you can see how this global mutex in high concurrency can cause performance degradation.

If we are facing with this situation, how can we disable it?

Disabling Query Cache

There are two options that you can change: query_cache_type and query_cache_size.

So if we change query_cache_size to “0”, does it means the cache is disabled? Or we also have to change query_cache_type? Or both? And does MySQL require a restart to avoid the global mutex?

The source code shows us this:

Send_Result_to_Client

Running Tests

Context on the tests:

  1. We ran simple OLTP tests using sysbench as follows:
sysbench --test="/usr/share/doc/sysbench/tests/db/oltp.lua" --report-interval=1 --max-time=120 --oltp-read-only=off --max-requests=0 --num-threads=4 --oltp-table-size=2000000 --mysql-host=localhost --mysql-db=test --db-driver=mysql --mysql-user=root run


  1. Important portion of my.cnf file:
query_cache_type =1
query_cache_limit = 1M
query_cache_size =1G
performance_schema_instrument='wait/synch/%Query_cache%=COUNTED'


Disable the Query Cache

So basically the tests were run for two minutes each while playing with query_cache_type and query_cache_size.

  1. Started MySQL with query_cache_type=1 and query_cache_size=1G.
  2. Change query_cache_type to 0. As we can see nothing changed, MySQL is still using the query cache.
  3. But when we stopped sysbench and started again (closing and opening new connections), we can see there are no more inserts going into query cache. But we still can see the queries like “Not Cached” that means changing the query_cache_type applies only for the new connections, and we still can see some mutex.
  4. Restarted MySQL with query_cache_type=0 and query_cache_size=0. Finally we disabled the query cache and all the mutex is disappeared.
  5. Restarted MySQL with query cache enabled.
  6. We changed query_cache_size=0 and it almost worked, we could disable query cache on the fly, but as we can see there is still some mutex activity.
  7. Changing query_cache_type=0 and restarting sysbench does not have any effect on the mutex.

So the only way to stop any activity around Query Cache requires restarting MySQL with query_cache_type=0 and query_cache_size=0. Disabling it or even set it to “0” on runtime is not completely stopping mutex activity.

But why do we still need query_cache_size while in theory query_cache_type should be enough?

As referenced above, the manual says if query_cache_type = 0:

Do not cache results in or retrieve results from the query cache. Note that this does not deallocate the query cache buffer. To do that, you should set query_cache_size to 0.

Based on our test, if we change query_cache_type to 0, it still hits the cache.

So you might think “well, I don’t enable the query cache and use defaults to keep it disabled.” Keep reading, because you might be wrong. According to the manual, starting from 5.6.8 query_cache_type=0 is set by default, but query_cache_size=1048576  (1MB). This means that if we keep default configuration, we will still see activity in the query cache as follows:

mysql-e"show global status like 'qca%';"

+-------------------------+---------+

|Variable_name|Value|

+-------------------------+---------+

|Qcache_free_blocks|1|

|Qcache_free_memory|1031320|

|Qcache_hits|0|

|Qcache_inserts|0|

|Qcache_lowmem_prunes|0|

|Qcache_not_cached|423294|

|Qcache_queries_in_cache|0|

|Qcache_total_blocks|1|

+-------------------------+---------+

But if we just add query_cache_size=0  to my.cnf and check again (of course after restarting server):

mysql-e"show global status like 'qca%';"

+-------------------------+-------+

|Variable_name|Value|

+-------------------------+-------+

|Qcache_free_blocks|0|

|Qcache_free_memory|0|

|Qcache_hits|0|

|Qcache_inserts|0|

|Qcache_lowmem_prunes|0|

|Qcache_not_cached|0|

|Qcache_queries_in_cache|0|

|Qcache_total_blocks|0|

+-------------------------+-------+

We finally get no Query Cache related activity at all. How much overhead is caused by this? We’re not fully sure because we didn’t perform benchmarks, but we like to see no activity when we don’t want to.

Now we’re wondering if this case requires a bug report. Stay tuned, we will publish results in the post soon.

Digging More Code

Let’s have a look on store_query function. MySQL uses this function to store queries in the query cache. If we read the code we can find this:

if (thd->locked_tables_mode || query_cache_size == 0)
    DBUG_VOID_RETURN;

It only checks the query_cache_size, it does not check the type. Store_query is called in handle_query, which also does not check the query_chache_type.

Conclusion

There is some contradiction between checking the query cache and storing the data in the query cache, which needs further investigation. But as we can see it is not possible to fully disable the query cache on the fly by changing query_cache_type  or/and query_cache_size to 0. Based on the code and the tests, if you want to make sure the query cache is fully disabled, change query_cache_size and query_cache_type to 0 and restart MySQL.

It is a known fact that query cache can be a big point of contention, and we are not trying to benchmark the performance overhead since this mostly depends on the workload type. However, we still can see some overhead if the query cache is not fully disabled when MySQL is started.

Create flexible schemas using dynamic columns for semi-structured data. Learn how.

Topics:
database ,query cache ,database performance ,mysql

Published at DZone with permission of Tibor Korocz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}