標籤:
啟用MySQL查詢快取能夠極大地減低資料庫server的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 |+-------------------------+----------+
當中各個參數的意義例如以下:
對於某些不想使用緩衝的語句。能夠這樣使用:select SQL_NO_CACHE count(*) from users where email = ‘hello‘;
(原創文章。轉載請註明轉自Clement-Xu的csdn部落格。)
啟用MySQL查詢快取