啟用MySQL查詢快取

來源:互聯網
上載者:User

標籤:mysql   緩衝   查詢   

啟用MySQL查詢快取可以極大地減低資料庫伺服器的CPU使用率,實際使用方式是:開啟前CPU使用率120%左右,開啟後降到了10%。


查看查詢快取情況:mysql> show variables like ‘%query_cache%‘; (query_cache_type 為 ON 表示已經開啟)+------------------------------+----------+| Variable_name                | Value    |+------------------------------+----------+| have_query_cache             | YES      || query_cache_limit            | 1048576  || query_cache_min_res_unit     | 4096     || query_cache_size             | 20971520 || query_cache_type             | ON       || query_cache_wlock_invalidate | OFF      |+------------------------------+----------+
如果不是ON,修改設定檔以開啟查詢快取:> vi /etc/my.cnf[mysqld]中添加:query_cache_size = 20Mquery_cache_type = ON
重啟mysql服務:> service mysql restart
查看緩衝使用方式:
mysql> show status like ‘qcache%‘;  +-------------------------+----------+| Variable_name           | Value    |+-------------------------+----------+| Qcache_free_blocks      | 83       || Qcache_free_memory      | 19811040 || Qcache_hits             | 3108196  || Qcache_inserts          | 757254   || Qcache_lowmem_prunes    | 20720    || Qcache_not_cached       | 47219    || Qcache_queries_in_cache | 47       || Qcache_total_blocks     | 276      |+-------------------------+----------+
    其中各個參數的意義如下:  
  • Qcache_free_blocks:緩衝中相鄰記憶體塊的個數。數目大說明可能有片段。FLUSH QUERY CACHE會對緩衝中的片段進行整理,從而得到一個空閑塊。  
  • Qcache_free_memory:緩衝中的空閑記憶體。  
  • Qcache_hits:每次查詢在緩衝中命中時就增大  
  • Qcache_inserts:每次插入一個查詢時就增大。叫用次數除以插入次數就是不中比率。  
  • Qcache_lowmem_prunes:緩衝出現記憶體不足並且必須要進行清理以便為更多查詢提供空間的次數。這個數字最好長時間來看;如果這個 數字在不斷增長,就表示可能片段非常嚴重,或者記憶體很少。(上面的 free_blocks和free_memory可以告訴您屬於哪種情況)  
  • Qcache_not_cached:不適合進行緩衝的查詢的數量,通常是由於這些查詢不是 SELECT 語句或者用了now()之類的函數。  
  • Qcache_queries_in_cache:當前緩衝的查詢(和響應)的數量。  
  • Qcache_total_blocks:緩衝中塊的數量。 

對於某些不想使用緩衝的語句,可以這樣使用:select SQL_NO_CACHE count(*) from users where email = ‘hello‘;
(原創文章,轉載請註明轉自Clement-Xu的csdn部落格。)

著作權聲明:本文為原創文章,轉載請註明轉自Clement-Xu的csdn部落格。

啟用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.