阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

131次阅读
没有评论

共计 4382 个字符,预计需要花费 11 分钟才能阅读完成。

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 版本以后直接取消了查询缓存的整块功能。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计4382字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中