MySQL Query cache fragmentation, cache hit rate, and Nagios monitoring

Source: Internet
Author: User

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.



  1. mysql> SHOW STATUS like ' qcache% ';
  2. +-------------------------+-----------+
  3. | variable_name | Value |
  4. +-------------------------+-----------+
  5. | Qcache_free_blocks | 5 |
  6. | Qcache_free_memory | 134176648 |
  7. | Qcache_hits | 110 |
  8. | Qcache_inserts | 245 |
  9. | Qcache_lowmem_prunes | 0 |
  10. | qcache_not_cached | 7119 |
  11. | Qcache_queries_in_cache | 9 |
  12. | Qcache_total_blocks | 31 |
  13. +-------------------------+-----------+
  14. 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.




    1. mysql> SHOW VARIABLES like '%query_cache% ';
    2. +------------------------------+-----------+
    3. | variable_name | Value |
    4. +------------------------------+-----------+
    5. | Have_query_cache | YES |
    6. | Query_cache_limit | 1048576 |
    7. | Query_cache_min_res_unit | 4096 |
    8. | Query_cache_size | 134217728 |
    9. | Query_cache_type | On |
    10. | Query_cache_wlock_invalidate | OFF |
    11. +------------------------------+-----------+
    12. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.