MySQL查询缓存工作

标签 mysql database caching query-cache

我想了解 MySQL 查询缓存的工作原理。我检查了this api

这些场景将如何运作?

案例一

select * from my_table where column = 'Myvalue';

案例二

select * from my_table where column = 'test';

案例三

select * from my_table where column = 'Myvalue';

所以当案例 3 执行时,我想知道结果是从缓存中还是从数据库中获取。?有人可以解释一下吗?因为他们在手册中提到了这一点

Queries must be exactly the same (byte for byte) to be seen as identical.

最佳答案

由于查询 1 和 3 相同,只要第一个查询的结果仍在 query cache 中并且在查询之间没有对表进行更新,第三个查询将由缓存提供而不是访问表。

查询缓存默认关闭。设置query_cache_size以字节为单位的大小以启用查询缓存。设置为 1024 字节的倍数。文档说:

Sizes in tens of megabytes are usually beneficial.

另请注意以下有关 query cache operation 的信息:

Before MySQL 5.1.17, prepared statements do not use the query cache. Beginning with 5.1.17, prepared statements use the query cache under certain conditions, which differ depending on the preparation method.

If a table changes, all cached queries that use the table become invalid and are removed from the cache.

请注意,根据查询缓存,以下两个查询相同,尽管它们返回相同的结果集:

select * from my_table where column = 'Myvalue';

SELECT * FROM my_table WHERE column = 'Myvalue';

关于MySQL查询缓存工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23889023/

相关文章:

PHP - 在 txt 文件中缓存 MYSQL 查询是一种好习惯吗?

php - 如何防止 PHP 中的 SQL 注入(inject)?

php - 2个表,3列需要1个值

php - 在不使用枚举的情况下,从 MySQL 的一组预选数据中输入数据的最佳方法是什么?

database - 添加/组合标准偏差

.NET 缓存 : maximum number of elements

php - Mysql 查询从 php 更新

php - 需要、选择好的数据库驱动程序的建议

java - 错误 java.lang.NoSuchMethodError

amazon-web-services - 无法从我的 vpc 配置的 lambda 函数连接 dynamoDb