美文网首页
Mysql性能优化-6.查询缓存,query cache

Mysql性能优化-6.查询缓存,query cache

作者: 笨鸡 | 来源:发表于2019-05-19 21:16 被阅读0次

1.Query Cache是MySQL层面提供的数据缓存,用于缓存select查询的结果。

2.在配置文件中开启缓存

my.ini
query_cache_type 0 or 1 or 2
0,关闭
1或者2,表示开启。
1、表示开启,但是默认缓存,需要增加sql-no-cache提示,放弃缓存。
2、表示开启,但是默认不缓存,需要增加sql-cache提示,执行缓存。

配置完毕,重启mysql生效。
重启之后,在客户端,使用:show variables like ‘query_cache_type’来看结果:

mysql> show variables like 'query_cache_type';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| query_cache_type | DEMAND |
+------------------+--------+
1 row in set, 1 warning (0.00 sec)

3.更改mysql的query cache size


mysql> show variables like 'query_cache_size';
+------------------+---------+
| Variable_name    | Value   |
+------------------+---------+
| query_cache_size | 1048576 |
+------------------+---------+
1 row in set, 1 warning (0.00 sec)

mysql> set global query_cache_size=64*1024*1024;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show variables like 'query_cache_size';
+------------------+----------+
| Variable_name    | Value    |
+------------------+----------+
| query_cache_size | 67108864 |
+------------------+----------+
1 row in set, 1 warning (0.00 sec)

4.查询,缓存结果

  • query_cache_type=1,直接执行,需要不缓存,则:
mysql> select sql_no_cache * from t_student where user like '%a-a' limit 5;
+------+------------+-----------+--------+-------+--------------------------------------+----------+
| id   | first_name | last_name | gender | user  | password                             | class_id |
+------+------------+-----------+--------+-------+--------------------------------------+----------+
|   74 | 01ede      | fedb7     |      1 | e4a-a | d4e7e57e-b892-4049-a191-ae689c368801 |      210 |
| 1236 | 5f691      | b307b     |      2 | 8da-a | c49f2835-e0fe-47ab-a2dd-07fc25445352 |      452 |
| 1282 | 4dc7a      | a064f     |      2 | 98a-a | 725283c2-aff1-4696-bfb9-0a145f0bbeca |      796 |
| 1503 | a4916      | dc637     |      2 | bea-a | 2f245e98-a083-42ec-bb7e-d1df77b839f2 |       20 |
| 1774 | 57d71      | 75068     |      1 | 2fa-a | c318d24c-952b-4bec-9d15-3008ea54510e |      692 |
+------+------------+-----------+--------+-------+--------------------------------------+----------+
5 rows in set, 1 warning (0.00 sec)
  • query_cache_type=2,需要使用sql-cache提示:
mysql> select sql_cache * from t_student where user like '%a-a' limit 5;
+------+------------+-----------+--------+-------+--------------------------------------+----------+
| id   | first_name | last_name | gender | user  | password                             | class_id |
+------+------------+-----------+--------+-------+--------------------------------------+----------+
|   74 | 01ede      | fedb7     |      1 | e4a-a | d4e7e57e-b892-4049-a191-ae689c368801 |      210 |
| 1236 | 5f691      | b307b     |      2 | 8da-a | c49f2835-e0fe-47ab-a2dd-07fc25445352 |      452 |
| 1282 | 4dc7a      | a064f     |      2 | 98a-a | 725283c2-aff1-4696-bfb9-0a145f0bbeca |      796 |
| 1503 | a4916      | dc637     |      2 | bea-a | 2f245e98-a083-42ec-bb7e-d1df77b839f2 |       20 |
| 1774 | 57d71      | 75068     |      1 | 2fa-a | c318d24c-952b-4bec-9d15-3008ea54510e |      692 |
+------+------------+-----------+--------+-------+--------------------------------------+----------+
5 rows in set, 1 warning (0.00 sec)

5.重置缓存

mysql> reset query cache;
Query OK, 0 rows affected, 1 warning (0.00 sec)

6.注意

  • 应用程序,不应该关心查询缓存的使用情况。可以尝试使用,不能由查询缓存决定业务逻辑。
  • 动态数据不能被缓存。
  • 缓存检所依赖于SQL语句的字符串规则。大小写,空格等都会导致缓存不匹配。

7.缓存失效问题(严重的问题)

当数据表改动时,基于该数据表的任何缓存都会被删除。表层面的管理。

相关文章

网友评论

      本文标题:Mysql性能优化-6.查询缓存,query cache

      本文链接:https://www.haomeiwen.com/subject/uxaqzqtx.html