|  | 
 
| 在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;这个可以查看警告具体内容,很好。
 
 
 | 
 |