mysql查詢快取

來源:互聯網
上載者:User

標籤:style   color   io   使用   ar   for   strong   sp   資料   

第一: 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; 這樣就會用到查詢快取。

①在 query_cache_type 開啟的情況下,如果你不想使用緩衝,需要指明
select sql_no_cache id,name from tableName;

②當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的,那麼就要自己設定下。

設定 set @@global.query_cache_size=1000000; 這裡是設定1M左右,900多K。

再次查看下 select @@global.query_cache_size;

+---------------------------+
| @@global.query_cache_size |
+---------------------------+
| 999424 |
+---------------------------+
顯示我們設定新的大小,表示設定成功。

第四: query_cache_limit 控制緩衝查詢結果的最大值

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

MySql 可以設定一個最大的緩衝值,當你查詢快取數結果資料超過這個值就不會
進行緩衝。預設為1M,也就是超過了1M查詢結果就不會緩衝。

mysql> select @@global.query_cache_limit;

+----------------------------+
| @@global.query_cache_limit |
+----------------------------+
| 1048576 |
+----------------------------+

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

好了,通過4個步驟就可以 開啟了查詢快取,具體值的大小和查詢的方式 這個因不同
的情況來指定了。

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             | 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)

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    | 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

mysql查詢快取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.