MySQL查詢緩存設置 提高MySQL查詢性能

MySQL查詢緩存設置 提高MySQL查詢性能

啓用MySQL查詢緩存可以極大地減低數據庫服務器的CPU使用率,實際使用情況是:開啓前CPU使用率120%左右,開啓後降到了10%。

query_cache_type 使用查詢緩存的方式

一般,我們會把query_cache_type 設置爲 ON,默認情況下應該是ON

mysql> SELECT @@query_cache_type;
+--------------------+
| @@query_cache_type |
+--------------------+
| ON                 |
+--------------------+

query_cache_type有3個值 0代表關閉查詢緩存OFF,1代表開啓ON,2(DEMAND)代表當sql語句中有SQL_CACHE關鍵詞時才緩存,如:

select SQL_CACHE user_name from users where user_id = '100';

這樣 當我們執行 select id,name from tableName; 就會用到查詢緩存。

1)在query_cache_type 打開的情況下,如果你不想使用緩存,需要指明

SELECT sql_no_cache id,name FROM tableName; 

2)當sql中用到mysql函數,也不會緩存

當然也可以禁用查詢緩存: 

mysql> set session query_cache_type=off;

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

mysql> show variables like 'have_query_cache';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| have_query_cache | YES   |
+------------------+-------+

上面的顯示,表示設置查詢緩存是可用的。

系統變量 query_cache_size

表示查詢緩存大小,也就是分配內存大小給查詢緩存,如果你分配大小爲0,那麼第一步和 第二步起不到作用,還是沒有任何效果。

mysql> select @@global.query_cache_size;
+---------------------------+
| @@global.query_cache_size |
+---------------------------+
|                  16777216 |
+---------------------------+

上面是 mysql6.0設置默認的,之前的版本好像默認是0的,那麼就要自己設置下。

--設置global.query_cache_size這裏是設置1M左右,900多K。
mysql > set @@global.query_cache_size=1000000;

query_cache_limit 控制緩存查詢結果的最大值

例如: 如果查詢結果很大, 也緩存????這個明顯是不可能的。

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

mysql> select @@global.query_cache_limit;
+----------------------------+
| @@global.query_cache_limit |
+----------------------------+
|                    1048576 |
+----------------------------+

這個是默認的數值,如果需要修改,就像設置緩存大小一樣設置,使用set重新指定大小。

mysql查詢緩存相關變量

複製代碼
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             | 0       |
| query_cache_type             | ON      |
| query_cache_wlock_invalidate | OFF     |
+------------------------------+---------+
複製代碼

查看緩存的狀態

複製代碼
mysql> show status like '%Qcache%';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Qcache_free_blocks      | 0     |
| Qcache_free_memory      | 0     |
| Qcache_hits             | 0     |
| Qcache_inserts          | 0     |
| Qcache_lowmem_prunes    | 0     |
| Qcache_not_cached       | 0     |
| Qcache_queries_in_cache | 0     |
| Qcache_total_blocks     | 0     |
+-------------------------+-------+
複製代碼

MySQL 提供了一系列的 Global Status 來記錄 Query Cache 的當前狀態,具體如下:

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

Qcache_free_memory:目前還處於空閒狀態的 Query Cache 內存總量

Qcache_hits:Query Cache 命中次數

Qcache_inserts:向 Query Cache 中插入新的 Query Cache 的次數,也就是沒有命中的次數

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

Qcache_not_cached:沒有被 Cache 的 SQL 數,包括無法被 Cache 的 SQL 以及由於 query_cache_type 設置的不會被 Cache 的 SQL

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

Qcache_total_blocks:Query Cache 中總的 Block 數量

檢查查詢緩存使用情況

檢查是否從查詢緩存中受益的最簡單的辦法就是檢查緩存命中率,當服務器收到SELECT 語句的時候,Qcache_hits 和Com_select 這兩個變量會根據查詢緩存的情況進行遞增

查詢緩存命中率的計算公式是:Qcache_hits/(Qcache_hits + Com_select)。

mysql> show status like 'Com_select%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select    | 3     |
+---------------+-------+

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
關於query_cache_min_res_unit大小的調優,給出了一個計算公式,可以供調優設置參考:
query_cache_min_res_unit = (query_cache_size - Qcache_free_memory) / Qcache_queries_in_cache

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