解決:設置mysql的query_cache_size值無效

在mysql中開啓query_cache,有時能夠提升查詢效率。默認狀況下query_cache是關閉的。
我在測試mysql的查詢緩存時遇到了這麼一個問題:
mysql> show global variables like 'query_cache%';
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size |
0 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+---------+
5 rows in set (0.00 sec)

mysql> set global query_cache_size=1024;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show global variables like 'query_cache%';
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 0 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+---------+
5 rows in set (0.00 sec)
從顯示結果能夠看出設置無效,並且有一個警告,咱們看看警告內容是什麼:
mysql> show warnings;
+---------+------+----------------------------------------------------------------+
| Level     | Code | Message |
+---------+------+----------------------------------------------------------------+
| Warning | 1282 | Query cache failed to set size 1024; new query cache size is 0 |
+---------+------+----------------------------------------------------------------+
1 row in set (0.00 sec)
警告說我設置的值1024失敗,爲何呢?看看mysql的參考手冊:
############################################################
# 當設置query_cache_size變量爲非零值時,應記住查詢緩存至少大約須要40KB來分配其數據結構。(具體
# 大小取決於系統結構)。若是你把該值設置的過小,將會獲得一個警告,如本例所示:
#
# mysql> SET GLOBAL query_cache_size = 40000;
#
# Query OK, 0 rows affected, 1 warning (0.00 sec)
############################################################
由此得知咱們設置的值過小了,至少應該大於40KB,咱們從新設置一個大點的值看看:
mysql> set global query_cache_size=1024*50;
Query OK, 0 rows affected (0.00 sec)

mysql> show global variables like 'query_cache%';
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 51200 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+---------+
5 rows in set (0.00 sec)
此次設置的值沒有警告信息了,並且再次查看發現有值了,成功了!

通常在生產環境不會遇到這樣的問題,由於誰也不會把這個值設置成小於40KB的,我這是在測試時遇到的。
並且這裏面有一個小技巧,就是show warnings;這個能夠查看警告具體內容,很好。
相關文章
相關標籤/搜索