Mysql 查詢緩存
查詢緩存的作用就是當查詢接收到一個和之前同樣的查詢,服務器將會從查詢緩存種檢索結果,而不是再次分析和執行上次的查詢。這樣就大大提高了性能,節省時間。1.配置查詢緩存
修改配置文件,修改[mysqld]下的query_cache_size和query_cache_type(如果沒有則添加)。其中query_cache_size表示緩存的大小,而query_cache_type有3個值,表示緩存那種類 型的select結果集,query_cache_type各個值如下:
0或off關閉緩存
1或on開啓緩存,但是不保存使用sql_no_cache的select語句,如不緩存select sql_no_cache name from wei where id=2
2或demand開啓有條件緩存,只緩存帶sql_cache的select語句,緩存select sql_cache name from wei where id=4
例子的配置爲下,配置完成重啓Mysql服務器即可。
- query_cache_size=10M
- query_cache_type=1
可以用如下命令查看是否開啓,其中have_query_cache爲是否開啓,query_cache_limit 指定單個查詢能夠使用的緩衝區大小,缺省爲1M;query_cache_min_res_unit爲系統分配的最小緩存塊大小,默認是4KB,設置值大對大數據查詢有好處,但如果你的查詢都是小數據 查詢,就容易造成內存碎片和浪費;query_cache_size和query_cache_type就是上面我們的配置;query_cache_wlock_invalidate表示當有其他客戶端正在對MyISAM表進行寫操作時,如果查詢在query cache中,是否返回cache結果還是等寫操作完成再讀表獲取結果。
- 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 | 10485760 |
- | query_cache_type | ON |
- | query_cache_wlock_invalidate | OFF |
- +------------------------------+----------+
- 6 rows in set (0.00 sec)