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.
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 processlist, 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 this situation, how can we disable it?
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:
|
1 |
int Query_cache::send_result_to_client(THD *thd, const LEX_CSTRING &sql)<br>{<br> ulonglong engine_data;<br> Query_cache_query *query;<br>#ifndef EMBEDDED_LIBRARY<br> Query_cache_block *first_result_block;<br>#endif<br> Query_cache_block *result_block;<br> Query_cache_block_table *block_table, *block_table_end;<br> char *cache_key= NULL;<br> size_t tot_length;<br> Query_cache_query_flags flags;<br> DBUG_ENTER("Query_cache::send_result_to_client");<br><br> /*<br> Testing 'query_cache_size' without a lock here is safe: the thing<br> we may loose is that the query won't be served from cache, but we<br> save on mutex locking in the case when query cache is disabled.<br> See also a note on double-check locking usage above.<br> */<br> if (is_disabled() || thd->locked_tables_mode ||<br> thd->variables.query_cache_type == 0 || query_cache_size == 0)<br> goto err;<br>... |
MySQL is going to check if the query cache is enabled before it locks it. It is checking four conditions, and one of them has to be true. The last three could be obvious, but what is the “is_disabled()” function? Following the source code, we can find the next: sql_cache.h
|
1 |
void disable_query_cache(void) { m_query_cache_is_disabled= TRUE; }<br>...<br>bool is_disabled(void) { return m_query_cache_is_disabled; } |
|
1 |
void Query_cache::init()<br>{<br> DBUG_ENTER("Query_cache::init");<br> mysql_mutex_init(key_structure_guard_mutex,<br> &structure_guard_mutex, MY_MUTEX_INIT_FAST);<br> mysql_cond_init(key_COND_cache_status_changed,<br> &COND_cache_status_changed);<br> m_cache_lock_status= Query_cache::UNLOCKED;<br> initialized = 1;<br> /*<br> If we explicitly turn off query cache from the command line query cache will<br> be disabled for the reminder of the server life time. This is because we<br> want to avoid locking the QC specific mutex if query cache isn't going to<br> be used.<br> */<br> if (global_system_variables.query_cache_type == 0)<br> query_cache.disable_query_cache();<br><br> DBUG_VOID_RETURN;<br>} |
If the global_system_variables.query_cache_type == 0 condition is true it is going to call the disable_query_cache function, which sets m_query_cache_is_disabled = True, so is_disabled going to be “True”. That means if we are setting query_cache_type to 0 in runtime, that should eliminate the global mutex. Let’s run some tests to confirm this and see if the global mutex disappears after changing query_cache_type to 0.
Running tests
Context on the tests:
|
1 |
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 |
query_cache_type =1<br>query_cache_limit = 1M<br>query_cache_size =1G<br>performance_schema_instrument='wait/synch/%Query_cache%=COUNTED' |
So basically the tests were run for two minutes each while playing with query_cache_type and query_cache_size.
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 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:
|
1 |
mysql -e "show global status like 'qca%';"<br>+-------------------------+---------+<br>| Variable_name | Value |<br>+-------------------------+---------+<br>| Qcache_free_blocks | 1 |<br>| Qcache_free_memory | 1031320 |<br>| Qcache_hits | 0 |<br>| Qcache_inserts | 0 |<br>| Qcache_lowmem_prunes | 0 |<br>| Qcache_not_cached | 423294 |<br>| Qcache_queries_in_cache | 0 |<br>| Qcache_total_blocks | 1 |<br>+-------------------------+---------+ |
But if we just add query_cache_size=0 to my.cnf and check again (of course after restarting server):
|
1 |
mysql -e "show global status like 'qca%';"<br>+-------------------------+-------+<br>| Variable_name | Value |<br>+-------------------------+-------+<br>| Qcache_free_blocks | 0 |<br>| Qcache_free_memory | 0 |<br>| Qcache_hits | 0 |<br>| Qcache_inserts | 0 |<br>| Qcache_lowmem_prunes | 0 |<br>| Qcache_not_cached | 0 |<br>| Qcache_queries_in_cache | 0 |<br>| Qcache_total_blocks | 0 |<br>+-------------------------+-------+ |
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.
Let’s have a look at store_query function. MySQL uses this function to store queries in the query cache. If we read the code we can find this:
|
1 |
if (thd->locked_tables_mode || query_cache_size == 0)<br> 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.
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.