MySQL optimization program, on the Internet can find a lot of information, today on the Mysql cache fragmentation and hit ratio of the detailed understanding, the individual made a simple collation.
One,Mysql Query cache fragmentation and cache hit ratio.
- mysql> SHOW STATUS like ' qcache% ';
- +-------------------------+-----------+
- | variable_name | Value |
- +-------------------------+-----------+
- | Qcache_free_blocks | 5 |
- | Qcache_free_memory | 134176648 |
- | Qcache_hits | 110 |
- | Qcache_inserts | 245 |
- | Qcache_lowmem_prunes | 0 |
- | qcache_not_cached | 7119 |
- | Qcache_queries_in_cache | 9 |
- | Qcache_total_blocks | 31 |
- +-------------------------+-----------+
- 8 rows in Set (0.01 sec)
MySQL Query Cache variable
|
Variable name |
Description |
|
Qcache_free_blocks |
The number of contiguous memory blocks in the cache. A large number indicates that there may be fragmentation. FLUSH QUERY CACHE fragments in the cache are collated to get a free block. |
|
Qcache_free_memory |
Free memory in the cache. |
|
Qcache_hits |
Increases each time the query is hit in the cache. |
|
Qcache_inserts |
Increases each time a query is inserted. The number of hits divided by the number of insertions is not the middle ratio; subtracting this value by 1 is the hit rate. In the above example, approximately 87% queries are hit in the cache. |
|
Qcache_lowmem_prunes |
The number of times that the cache is out of memory and must be cleaned up to provide space for more queries. This number is best seen over a long period of time, and if the number is growing, it can mean that fragmentation is very serious, or that there is little memory. (The free_blocks and free_memory above can tell you which is the case). |
|
Qcache_not_cached |
The number of queries that are not suitable for caching, usually because these queries are not SELECT statements. |
|
Qcache_queries_in_cache |
The number of queries (and responses) currently cached. |
|
Qcache_total_blocks |
The number of blocks in the cache. |
- mysql> SHOW VARIABLES like '%query_cache% ';
- +------------------------------+-----------+
- | variable_name | Value |
- +------------------------------+-----------+
- | Have_query_cache | YES |
- | Query_cache_limit | 1048576 |
- | Query_cache_min_res_unit | 4096 |
- | Query_cache_size | 134217728 |
- | Query_cache_type | On |
- | Query_cache_wlock_invalidate | OFF |
- +------------------------------+-----------+
- 6 rows in Set (0.00 sec)
Query_cache_min_res_unit size of the smallest block allocated by the query cache ( bytes )
Query_alloc_block_size the memory block size allocated for objects created during query parsing and execution
Qcache_free_blocks represents the amount of memory free blocks, reflecting the situation of memory fragmentation
==========================
1when the query is in progress,MysqlSave the query results in theQurey Cache, but if you want to save a larger result thanQuery_cache_min_res_unitthe value, this timeMySQLThe result will be retrieved while the results are saved, so sometimes not all of the results are obtained and then saved once, but eachQuery_cache_min_res_unitthe size of the memory space to save the result set, after use, then allocate one such block, if not enough, then allocate a block, and so on, that is, it is possible in a query,MySQLthe operation to make multiple memory allocations.
2) The generation of memory fragments. When a piece of allocated memory is not fully used,MySQLwill put this piece of memoryTrimand return the unused part to reuse. For example, the first time allocation4KB,it only used3KB, left1KB, second consecutive operation, assignment4KB, with a2KB, left2KB, these two consecutive operations are left with a total of1kb+2kb=3kbis not enough to allocate a memory unit, the memory fragment is generated.
3) usingFlush Query Cache, you can eliminate fragmentation
4) If the qcache_free_blocks value is too large, it may be that the query_cache_min_res_unit value is too large, should be adjusted smaller
5) Estimated value of Query_cache_min_res_unit:(query_cache_size-qcache_free_memory)/qcache_ Queries_in_cache
Check Query Cache usage
The simplest way to check whether to benefit from the query cache is to check the cache hit ratio
When the server receives a SELECT statement, theqcache_hits and com_select variables are cached according to the query
The situation is incremented
The calculation formula for query cache hit ratio is:qcache_hits/(qcache_hits + com_select).
Mysql> Show status like '%com_select% ';
+---------------+-------+
| variable_name | Value |
+---------------+-------+
| Com_select | 1 |
+---------------+-------+
1 row in Set (0.00 sec)
Query cache Hit ratio at this time:3/(3+1)=75%; Due to the personal test database, the query is less, less, and the hit rate is quite high.
Http://blog.chinaunix.net/uid-25266990-id-3426963.html
MySQL Query cache fragmentation, cache hit rate, and Nagios monitoring