首頁  >  文章  >  資料庫  >  解析MySQL快取啟動方法及參數(query_cache_size)

解析MySQL快取啟動方法及參數(query_cache_size)

怪我咯
怪我咯原創
2017-04-01 09:59:321687瀏覽

MySQL query cache從4.1版本開始提供了,不過值今天本人才對其進行研究。預設設定下,MySQL的該功能是沒有啟動的,可能你透過show variables like '%query_cache%';會發現其變數have_query_cache的值是yes,MYSQL初學者很容易以為這個參數為YES就代表開啟了查詢快取#,實際上是不對的,該參數表示目前版本的MYSQL是否支援Query Cache,實際上是否開啟查詢快取是看另一個參數的值:query_cache_size ,值為0,表示停用query cache,而預設配置正是配置為0。


設定方法:

在MYSQL的設定檔my.ini或my.cnf中找到如下內容:

# Query cache is used to cache SELECT results and later return them

# without actual executing the same query once again. Having the query

# cache enabled may result in significant speed , if your

# have a lot of identical queries and rarely changing tables. See the

# “Qcache_lowmem_prunes” status variable to check if the current value

是 is high










#1110 71700 enough for your load.

# Note: In case your tables change very often or if your queries are

# textually different every time, the query cache may result in a

# slowdown instead of a performance improvement.
query_cache_size=0解析MySQL快取啟動方法及參數(query_cache_size)
以上資訊是預設配置,其
註解
意思是說,MYSQL的查詢快取用於快取select查詢結果,並下次接收到同樣的查詢請求時,不再執行實際查詢處理而直接返回結果,有這樣的查詢快取能提高查詢的速度,使查詢效能得到最佳化,前提條件是你有大量的相同或相似的查詢,而很少改變表裡的數據,否則沒有必要使用此功能。可以透過Qcache_lowmem_prunes變數的值來檢查是否目前的值滿足你目前系統的負載。注意:如果你查詢的表格
更新
比較頻繁,而且很少有相同的查詢,最好不要使用查詢快取。

具體配置方法:

1.將query_cache_size設定為具體的大小,具體大小是多少取決於查詢的實際情況,但最好設定為1024的倍數,參考值32M。

2.增加一行:query_cache_type=1

query_cache_type參數用來控制快取的類型,注意這個值不能隨便設置,必須設定為數字,可選項目以及說明如下:

#如果設定為0,那麼可以說,你的快取根本就沒有用,相當於禁用了。但是這種情況下query_cache_size設定的大小系統是否要為其分配呢,這個問題有待測試?

如果設定為1,將會快取所有的結果,除非你的select語句使用SQL_NO_CACHE停用了查詢快取。

如果設定為2,則只快取在select語句中透過SQL_CACHE指定需要快取的查詢。

OK,配置完後的部分文件如下:

query_cache_size=128M

query_cache_type=1###儲存文件,重新啟動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             | 134217728 |

| query_cache_type             | ON        |

| query_cache_wlock_invalidate | OFF       |

+——————————+———–+

6 rows in set (0.00 sec)
#######主要看query_cache_size和query_cache_type的值是否跟我們設的一致:#######這裡query_cache_size的值是134217728,我們設定的是128M,實際上是一樣的,只是單位不同,可以自己換算下:134217728 = 128*1024*1024。 ######query_cache_type設定為1,顯示為ON,這個前面已經說過了。 ######總之,看到上邊的顯示表示設定正確,但是在實際的查詢中是否能夠快取查詢,還需要手動測試下,我們可以透過show status like '%Qcache%';語句來測試,現在我們開啟了查詢快取功能,在執行查詢前,先來看看相關參數的值:#########
mysql> show status like ‘%Qcache%';

+————————-+———–+

| Variable_name           | Value     |

+————————-+———–+

| Qcache_free_blocks      | 1         |

| Qcache_free_memory      | 134208800 |

| Qcache_hits             | 0         |

| Qcache_inserts          | 0         |

| Qcache_lowmem_prunes    | 0         |

| Qcache_not_cached       | 2         |

| Qcache_queries_in_cache | 0         |

| Qcache_total_blocks     | 1         |

+————————-+———–+

8 rows in set (0.00 sec)


这里顺便解释下这个几个参数的作用:

Qcache_free_blocks:表示查询缓存中目前还有多少剩余的blocks,如果该值显示较大,则说明查询缓存中的内存碎片过多了,可能在一定的时间进行整理。

Qcache_free_memory:查询缓存的内存大小,通过这个参数可以很清晰的知道当前系统的查询内存是否够用,是多了,还是不够用,DBA可以根据实际情况做出调整。

Qcache_hits:表示有多少次命中缓存。我们主要可以通过该值来验证我们的查询缓存的效果。数字越大,缓存效果越理想。

Qcache_inserts: 表示多少次未命中然后插入,意思是新来的SQL请求在缓存中未找到,不得不执行查询处理,执行查询处理后把结果insert到查询缓存中。这样的情况的次数,次数越多,表示查询缓存应用到的比较少,效果也就不理想。当然系统刚启动后,查询缓存是空的,这很正常。

Qcache_lowmem_prunes:该参数记录有多少条查询因为内存不足而被移除出查询缓存。通过这个值,用户可以适当的调整缓存大小。

Qcache_not_cached: 表示因为query_cache_type的设置而没有被缓存的查询数量。

Qcache_queries_in_cache:当前缓存中缓存的查询数量。

Qcache_total_blocks:当前缓存的block数量。
下边我们测试下:

比如执行如下查询语句

mysql> select * from user where id = 2;

+—-+——-+

| id | name  |

+—-+——-+

|  2 | test2 |

+—-+——-+

1 row in set (0.02 sec)


然后执行show status like ‘%Qcache%',看看有什么变化:

mysql> show status like ‘%Qcache%';

+————————-+———–+

| Variable_name           | Value     |

+————————-+———–+

| Qcache_free_blocks      | 1         |

| Qcache_free_memory      | 134207264 |

| Qcache_hits             | 0         |

| Qcache_inserts          | 1         |

| Qcache_lowmem_prunes    | 0         |

| Qcache_not_cached       | 3         |

| Qcache_queries_in_cache | 1         |

| Qcache_total_blocks     | 4         |

+————————-+———–+

8 rows in set (0.00 sec)


对比前面的参数值,我们发现Qcache_inserts变化了。Qcache_hits没有变,下边我们在执行同样的查询

select * from user where id = 2,按照前面的理论分析:Qcache_hits应该等于1,而Qcache_inserts应该值不变(其他参数的值变化暂时不关注,读者可以自行测试),再次执行:
show status like ‘%Qcache%',看看有什么变化:

mysql> show status like ‘%Qcache%';

+————————-+———–+

| Variable_name           | Value     |

+————————-+———–+

| Qcache_free_blocks      | 1         |

| Qcache_free_memory      | 134207264 |

| Qcache_hits             | 1         |

| Qcache_inserts          | 1         |

| Qcache_lowmem_prunes    | 0         |

| Qcache_not_cached       | 4         |

| Qcache_queries_in_cache | 1         |

| Qcache_total_blocks     | 4         |

+————————-+———–+

8 rows in set (0.00 sec)


OK,果然跟我们分析的完全一致。



以上是解析MySQL快取啟動方法及參數(query_cache_size)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn