温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL query_cache_type的DEMAND参数介绍和使用举例

发布时间:2020-08-09 15:21:10 来源:ITPUB博客 阅读:130 作者:chenfeng 栏目:MySQL数据库

Query Cache存储SELECT语句及其产生的数据结果,特别适用于表数据变化不是很频繁的场景,例如一些静态页面,

或者页面中的某块不经常发生 变化的信息。如果此表上有任何写表操作发生,那么和这个表相关的所有缓存都将失效。

由于Query Cache需要缓存最新数据结果,因此表数据 发生任何变化(INSERT、UPDATE、DELETE或其他有可能产生

数据变化的操作),都会导致Query Cache被刷新。对于更新压力大的数据库来说,查询缓存的命中率也会非常低。

但我们可以将参数 query_cache_type 设置成 DEMAND(按需及用)方式,这样对于默认的SQL语句不使用查询缓存,

而对于确定要使用query cache的SQL语句, 可以用sql_cache的方法指定,例如:

select sql_cache * from table_name; 

或 select sql_cache count(*) from table_name;



以下是query_cache_type三个参数的含义:


query_cache_type=0(OFF)关闭


query_cache_type=1(ON)缓存所有结果,除非select语句使用SQL_NO_CACHE禁用查询缓存


query_cache_type=2(DEMAND),只缓存select语句中通过SQL_CACHE指定需要缓存的查询


修改为DEMAND方式:

vi /etc/my.cnf,加入如下行:

query_cache_type =2


保存并重启mysql


mysql5.7版本如果直接修改可能会报错:

mysql>set global query_cache_type=2;

ERROR 1651 (HY000): Query cache is disabled; restart the server with query_cache_type=1 to enable it



查看是否开启DEMAND参数:

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             | 1048576 |

| query_cache_type             | DEMAND  |

| query_cache_wlock_invalidate | OFF     |

+------------------------------+---------+

6 rows in set (0.44 sec)



mysql>>show global  status like '%Qcache%';

+-------------------------+---------+

| Variable_name           | Value   |

+-------------------------+---------+

| Qcache_free_blocks      | 1       |

| Qcache_free_memory      | 1031832 |

| Qcache_hits             | 0       |

| Qcache_inserts          | 0       |

| Qcache_lowmem_prunes    | 0       |

| Qcache_not_cached       | 3       |

| Qcache_queries_in_cache | 0       |

| Qcache_total_blocks     | 1       |

+-------------------------+---------+

8 rows in set (0.14 sec)


相关参数解释如下:


Qcache_free_blocks:表示查询缓存中目前还有多少剩余的blocks,如果该值显示较大,说明查询缓存中的内存碎片过多。


Qcache_free_memory:表示Query Cache中目前剩余的内存大小。


Qcache_hits:表示有多少次命中缓存。


Qcache_inserts: 表示多少次未命中缓存然后插入,意思是新来的SQL请求如果在缓存中未找到,不得不执行查询处理,执行查询处理后把结果insert到查询缓存中。


Qcache_lowmem_prunes:该参数记录有多少条查询因为内存不足而被移出查询缓存。


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


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


Qcache_total_blocks:当前缓存的block数量。


执行一次查询:

>select count(*) from test;

+----------+

| count(*) |

+----------+

|    36675 |

+----------+

1 row in set (0.41 sec)


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


mysql>show global  status like '%Qcache%';

+-------------------------+---------+

| Variable_name           | Value   |

+-------------------------+---------+

| Qcache_free_blocks      | 1       |

| Qcache_free_memory      | 1031832 |

| Qcache_hits             | 0       |

| Qcache_inserts          | 0       |

| Qcache_lowmem_prunes    | 0       |

| Qcache_not_cached       | 4       |

| Qcache_queries_in_cache | 0       |

| Qcache_total_blocks     | 1       |

+-------------------------+---------+

8 rows in set (0.01 sec)


Qcache_hits为0.



再次执行select count(*) from test;

mysql>select count(*) from test;

+----------+

| count(*) |

+----------+

|    36675 |

+----------+

1 row in set (0.02 sec)


mysql>show global  status like '%Qcache%';

+-------------------------+---------+

| Variable_name           | Value   |

+-------------------------+---------+

| Qcache_free_blocks      | 1       |

| Qcache_free_memory      | 1031832 |

| 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)

Qcache_hits依旧为0,说明没有使用Query Cache.


加sql_cache执行一下语句,看看有什么变化:

mysql>select sql_cache count(*) from test;

+----------+

| count(*) |

+----------+

|    36675 |

+----------+

1 row in set, 1 warning (0.00 sec)


mysql>show global  status like '%Qcache%';

+-------------------------+---------+

| Variable_name           | Value   |

+-------------------------+---------+

| Qcache_free_blocks      | 1       |

| Qcache_free_memory      | 1030296 |

| Qcache_hits             | 1       |

| Qcache_inserts          | 1       |

| Qcache_lowmem_prunes    | 0       |

| Qcache_not_cached       | 5       |

| Qcache_queries_in_cache | 1       |

| Qcache_total_blocks     | 4       |

+-------------------------+---------+

8 rows in set (0.00 sec)


可以看到Qcache_hits的值变为了1,再次执行:


mysql>select sql_cache count(*) from test;

+----------+

| count(*) |

+----------+

|    36675 |

+----------+

1 row in set, 1 warning (0.00 sec)


mysql>show global  status like '%Qcache%';

+-------------------------+---------+

| Variable_name           | Value   |

+-------------------------+---------+

| Qcache_free_blocks      | 1       |

| Qcache_free_memory      | 1030296 |

| Qcache_hits             | 2       |

| Qcache_inserts          | 1       |

| Qcache_lowmem_prunes    | 0       |

| Qcache_not_cached       | 5       |

| Qcache_queries_in_cache | 1       |

| Qcache_total_blocks     | 4       |

+-------------------------+---------+

8 rows in set (0.01 sec)


可以看到Qcache_hits的值变为了2,每次执行都累加1,说明使用了query cache。


备注:从MySQL 8.0版本以后直接取消了查询缓存的整块功能。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI