我在測試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)
+------------------------------+---------+
| 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)
+---------+------+----------------------------------------------------------------+
| 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)
############################################################
# 當設置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)
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;這個可以查看警告具體內容,很好。