mysql查詢緩存打開、設置、參數查詢、性能變量意思

一般,我們會把 query_cache_type 設置爲 ON,默認情況下應該是ON
mysql> select @@query_cache_type;
+--------------------+
| @@query_cache_type |
+--------------------+
| ON |
+--------------------+
user_name from users where user_id = '100';
這樣 當我們執行 select id,name from tableName; 這樣就會用到查詢緩存。
①在 query_cache_type 打開的情況下,如果你不想使用緩存,需要指明
select sql_no_cache id,name from tableName;



第二: 系統變量 have_query_cache 設置查詢緩存是否可用

+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| have_query_cache | YES |
+------------------+-------+
上面的顯示,表示設置查詢緩存是可用的。

表示查詢緩存大小,也就是分配內存大小給查詢緩存,如果你分配大小爲0,

mysql> select @@global.query_cache_size;
+---------------------------+
| @@global.query_cache_size |
+---------------------------+
| 16777216 |
+---------------------------+
上面是 mysql6.0設置默認的,之前的版本好像默認是0的,那麼就要自己設置下。

再次查看下 select @@global.query_cache_size;
+---------------------------+
| @@global.query_cache_size |
+---------------------------+
| 999424 |
+---------------------------+
顯示我們設置新的大小,表示設置成功。

例如: 如果查詢結果很大, 也緩存????這個明顯是不可能的。
MySql 可以設置一個最大的緩存值,當你查詢緩存數結果數據超過這個值就不會
進行緩存。缺省爲1M,也就是超過了1M查詢結果就不會緩存。

+----------------------------+
| @@global.query_cache_limit |
+----------------------------+
| 1048576 |
+----------------------------+
這個是默認的數值,如果需要修改,就像設置緩存大小一樣設置,使用set
重新指定大小。
好了,通過4個步驟就可以 打開了查詢緩存,具體值的大小和查詢的方式 這個因不同
的情況來指定了。

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

mysql> show status like '%Qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 11       |
| Qcache_free_memory      | 16610552 |
| Qcache_hits             | 10       |
| Qcache_inserts          | 155      |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 21       |
| Qcache_queries_in_cache | 111      |
| Qcache_total_blocks     | 256      |
+-------------------------+----------+
8 rows in set (0.00 sec)

Qcache_free_blocks:目前還處於空閒狀態的 Query Cache 中內存 Block 數目

Qcache_hits:Query Cache 命中次數

Qcache_lowmem_prunes:當 Query Cache 內存容量不夠,需要從中刪除老的 Query Cache 以給新的 Cache 對象使用的次數

Qcache_queries_in_cache:目前在 Query Cache 中的 SQL 數量



檢查是否從查詢緩存中受益的最簡單的辦法就是檢查緩存命中率

的情況進行遞增

mysql> show status like '%Com_select%';

| Variable_name | Value |

| Com_select    | 1     |



query_cache_min_res_unit的配置是一柄”雙刃劍”,默認是4KB,設置值大對大數據查詢有好處,但如果你的查詢都是小數據 查詢,就容易造成內存碎片和浪費。

查詢緩存碎片率 = Qcache_free_blocks / Qcache_total_blocks * 100%

如果查詢緩存碎片率超過20%,可以用FLUSH QUERY CACHE整理緩存碎片,或者試試減小query_cache_min_res_unit,如果你的查詢都是小數據量的話。

查詢緩存利用率 = (query_cache_size - Qcache_free_memory) / query_cache_size * 100%

查詢緩存利用率在25%以下的話說明query_cache_size設置的過大,可適當減小;查詢緩存利用率在80%以上而且 Qcache_lowmem_prunes > 50的話說明query_cache_size可能有點小,要不就是碎片太多。

查詢緩存命中率 = (Qcache_hits - Qcache_inserts) / Qcache_hits * 100%

示例服務器 查詢緩存碎片率 = 20.46%,查詢緩存利用率 = 62.26%,查詢緩存命中率 = 1.94%,命中率很差,可能寫操作比較頻繁吧,而且可能有些碎片。

引用一段前輩的話

優化提示:
如果Qcache_lowmem_prunes 值比較大,表示查詢緩存區大小設置太小,需要增大。
如果Qcache_free_blocks 較多,表示內存碎片較多,需要清理,flush query cache
根據我看的 《High Performance MySQL》中所述,關於query_cache_min_res_unit大小的調優
,書中給出了一個計算公式,可以供調優設置參考:
query_cache_min_res_unit = (query_cache_size - Qcache_free_memory) / Qcache_queries_in_cache

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章