1.1. 配置
1.1.1. 配置文件配置
打开/etc/my.cnf,输入一下配置:
#查询缓存区的工作模式:0, 禁用查询缓存区; 1,启用查询缓存区(默认设置); 2,”按需分配”模式,只响应SELECT SQL_CACHE命令。
query_cache_type = 1
#查询缓存区的最大长度(默认设置是0,不开辟查询缓存区)。
query_cache_size = 1048576
#允许临时存放在查询缓存区里的查询结果的最大长度(默认设置是1M)。
query_cache_limit = 1048576
重启mysql生效:service mysqld restart
1.1.2. 动态配置
除上述通过配置文件配置外,也可在mysql运行过程中,动态查询配置并做修改:
STEP1:登录mysql
$ mysql –u root –p
STEP2:查询是否开启缓存
mysql> select @@query_cache_type;
+--------------------+
| @@query_cache_type |
+--------------------+
| ON |
+--------------------+
1 row in set (0.00 sec)
==>以上显示 ON 表示已经开启了缓存
当我们执行 selectid,name from tableName; 这样就会用到查询缓存。
在query_cache_type 打开的情况下,如果你不想使用缓存,需要指明select sql_no_cache id,name from tableName;
当然也可以禁用查询缓存:
mysql> set session uery_cache_type=off;
STEP3:查询缓存是否可用
mysql> show variables like'have_query_cache';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| have_query_cache | YES |
+------------------+-------+
1 row in set (0.01 sec)
==>上面的显示,表示设置查询缓存是可用的。
STEP4:查询可缓存空间大小
mysql> select @@global.query_cache_size;
+---------------------------+
| @@global.query_cache_size |
+---------------------------+
| 1048576 |
+---------------------------+
1 row in set (0.00 sec)
==》上面显示缓存的大小为1M
可通过以下命令设置缓存大小为2M,然后重新查询:
mysql> sset@@global.query_cache_size=2097152;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.query_cache_size;
+---------------------------+
| @@global.query_cache_size |
+---------------------------+
| 2097152 |
+---------------------------+
1 row in set (0.00 sec)
STEP5:查询可缓存结果大小限制
mysql> select@@global.query_cache_limit;
+----------------------------+
| @@global.query_cache_limit |
+----------------------------+
| 1048576 |
+----------------------------+
1 row in set (0.00 sec)
==》上面显示为1M,超过这个大小的文件将不会缓存,也可以使用对应的set命令修改。
1.2. 验证
在mysql中,可以使用show status like 'Qcache%'来查询显示缓存相关的状态:
mysql> show status like 'Qcache%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 2079896 |
| Qcache_hits | 0 |
| Qcache_inserts | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 5 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
+-------------------------+---------+
8 rows in set (0.00 sec)
输入一个select查询,然后在显示状态(可见插入了一个cache项):
mysql> select * from event where sid=5;==》该select语句执行结果会被缓存
mysql> show status like 'Qcache%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 2078360 |
| Qcache_hits | 0 |
| Qcache_inserts | 1 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 6 |
| Qcache_queries_in_cache | 1 |
| Qcache_total_blocks | 4 |
+-------------------------+---------+
8 rows in set (0.00 sec)
再次输入相同的select语句,并显示状态(可见Qcache_hit 为1,表示命中了缓存)
mysql> select * from event where sid=5; è还是这条select
mysql> show status like 'Qcache%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| Qcache_free_blocks |1 |
| Qcache_free_memory | 2078360 |
| Qcache_hits | 1 |
| Qcache_inserts | 1 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 6 |
| Qcache_queries_in_cache | 1 |
| Qcache_total_blocks | 4 |
+-------------------------+---------+
8 rows in set (0.00 sec)
1.3. 参考
http://www.cnblogs.com/toby/articles/2198697.html