MySQL查詢緩存

1.查詢緩存相關(guān)變量

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 | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
+------------------------------+---------+
6 rows in set (0.00 sec)

2.查詢緩存狀態(tài)

mysql> show status like "%Qcache%";
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 1031352 |
| Qcache_hits | 0 |
| Qcache_inserts | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 24017272 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
+-------------------------+----------+
8 rows in set (0.00 sec)

釋義
Qcache_free_blocks:目前還處于空閑狀態(tài)的 Query Cache 中內(nèi)存 Block 數(shù)目
Qcache_free_memory:目前還處于空閑狀態(tài)的 Query Cache 內(nèi)存總量
Qcache_hits:Query Cache 命中次數(shù)
Qcache_inserts:向 Query Cache 中插入新的 Query Cache 的次數(shù),也就是沒(méi)有命中的次數(shù)
Qcache_lowmem_prunes:當(dāng) Query Cache 內(nèi)存容量不夠,需要從中刪除老的 Query Cache 以給新的 Cache 對(duì)象使用的次數(shù)
Qcache_not_cached:沒(méi)有被 Cache 的 SQL 數(shù),包括無(wú)法被 Cache 的 SQL 以及由于 query_cache_type 設(shè)置的不會(huì)被 Cache 的 SQL
Qcache_queries_in_cache:目前在 Query Cache 中的 SQL 數(shù)量
Qcache_total_blocks:Query Cache 中總的 Block 數(shù)量

3.衡量緩存有效性的參數(shù)

  • 查詢緩存命中率 = Qcache_hits/(Qcache_hits + Com_select)。
  • 查詢緩存碎片率 = Qcache_free_blocks / Qcache_total_blocks * 100%
  • 查詢緩存命中率 = (Qcache_hits - Qcache_inserts) / Qcache_hits * 100%
  • 查詢緩存利用率 = (query_cache_size - Qcache_free_memory) / query_cache_size * 100%

如果查詢緩存碎片率超過(guò)20%,可以用FLUSH QUERY CACHE整理緩存碎片,或者試試減小query_cache_min_res_unit,如果你的查詢都是小數(shù)據(jù)量的話。
查詢緩存利用率在25%以下的話說(shuō)明query_cache_size設(shè)置的過(guò)大,可適當(dāng)減小;查詢緩存利用率在80%以上而且 Qcache_lowmem_prunes > 50的話說(shuō)明query_cache_size可能有點(diǎn)小,要不就是碎片太多。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容