Some thoughts on MySQLQueryCache bitsCN.com
Some experiences with MySQL Query Cache
When a memory usage alarm is reported on MySQL online today, I checked the mysql memory usage parameters, focusing on cache, innodb_buffer_pool_size, and query cache usage.
Query_cache_type is enabled by default, and the default size of query_cache_size in the cache is 32 MB. it is generally recommended that the size not exceed MB. you can use the query cache parameter to view the specific value:
[sql] mysql> show variables like '%cache%'; +------------------------------+----------------------+ | Variable_name | Value | +------------------------------+----------------------+ | binlog_cache_size | 32768 | | binlog_stmt_cache_size | 32768 | | have_query_cache | YES | | key_cache_age_threshold | 300 | | key_cache_block_size | 1024 | | key_cache_pision_limit | 100 | | max_binlog_cache_size | 18446744073709547520 | | max_binlog_stmt_cache_size | 18446744073709547520 | | metadata_locks_cache_size | 1024 | | query_cache_limit | 1048576 | | query_cache_min_res_unit | 4096 | | query_cache_size | 33554432 | | query_cache_type | ON | | query_cache_wlock_invalidate | OFF | | stored_program_cache | 256 | | table_definition_cache | 400 | | table_open_cache | 512 | | thread_cache_size | 8 | +------------------------------+----------------------+ 18 rows in set (0.00 sec) mysql>
"Qcache_free_blocks": the number of blocks remaining in the Query Cache. If the value is large,
It indicates that there are too many memory fragments in the Query Cache, and you may need to find a suitable opportunity for sorting ().
● "Qcache_free_memory": the current memory size in the Query Cache. With this parameter, we can be more accurate
Check whether the memory size of the Query Cache in the current system is sufficient, whether it needs to be increased or too much;
● "Qcache_hits": the number of hits. Through this parameter, we can see the basic results of the Query Cache;
● "Qcache_inserts": the number of missed attempts and insertion. Use "Qcache_hits" and "Qcache_inserts"
Then we can calculate the hit rate of the Query Cache:
Query Cache hit rate = Qcache_hits/(Qcache_hits + Qcache_inserts );
● "Qcache_lowmem_prunes": number of queries that are cleared due to insufficient memory. Pass
"Qcache_lowmem_prunes" and "Qcache_free_memory" are combined to better understand our system
Whether the memory size of the Query Cache in the system is really sufficient, and whether the Query is frequently changed due to insufficient memory
Output
● "Qcache_not_cached": because of the query_cache_type setting or the number of queries that cannot be cached;
● "Qcache_queries_in_cache": number of queries in the current Query Cache;
● "Qcache_total_blocks": number of blocks in the current Query Cache;
Query Cache restrictions
Query Cache stores Result sets in a logical structure instead of physical data pages.
, It will also be subject to some specific restrictions.
A) versions earlier than 5.1.17 cannot Cache variable queries. However, since 5.1.17
The Query of helper variables is supported;
B) the external query SQL statements in all subqueries cannot be cached;
C) the Query in Procedure, Function, and Trigger cannot be cached;
D) the Query that contains many other functions that may obtain different results each execution cannot be cached.
In view of the above restrictions, we recommend that you use the Query Cache with precise settings to allow
Suitable table data can enter the Query Cache, so that only some Query results are cached.
In addition, if the Qcache_free_blocks value is a little high, you can use flush query cache to clear it.
A friend's suggestion:
First, if there are many read operations, let's look at the proportion. Simply put, if the user list table or the data proportion is relatively fixed, for example, the product list can be opened, the premise is that these databases are concentrated, and the practices in the databases are relatively small.
Second: when we "cheat", such as stress testing during bidding, opening the query cache, we can still receive the qps surge, of course, the system prompts that the front-end connection pool has the same configuration. In most cases, if most of the data is written and there are not many visits, do not open them. for example, for social websites, 10% of people produce content, and 90% of others consume content, it still works very well, but if you are using qq messages or chatting, it will be terrible.
Third: it doesn't matter if a small website does not have high concurrency. in high concurrency, you will see a lot of qcache lock waits. Therefore, in high concurrency, it is not recommended to open the query cache.
BitsCN.com