mysql - MySQL 上的查询缓存会在一段时间后自动重置吗?一些结果缓存了一点,但第二天会重置

标签 mysql sql amazon-web-services amazon-rds

我做了这样的查询:

SELECT * 
FROM graphql.cases 
where parties = 'Bondelmonte v Bondelmonte' 
LIMIT 0, 1000

需要 2 秒。然后我重复它,用时 0.2 秒,向我展示缓存正在工作。

但第二天我重复了同样的事情,一开始需要 2 秒,然后又需要 0.2 秒。

我不是在一天前第一次缓存了查询吗?

最佳答案

有多种情况可能会导致查询结果被冲出缓存,例如:

  • 数据可能已被修改
  • 您可能运行了一条语句,其文本与缓存的语句略有不同(小写/大写、换行符……)
  • 缓存可能已达到其大小限制之一(内存、查询计数、 block ...)并决定逐出您的特定查询
  • 缓存碎片变高并迫使缓存修剪您的查询,即使仍有一些内存可用
  • 等等...

底线:即使进行了适当的微调,查询也不会永远保留在缓存中;有时,MySQL 需要重新执行查询并再次缓存它,这就是您所看到的。

为了您的引用,这里有一些来自 the documentation 的引述。 :

Queries must be exactly the same (byte for byte) to be seen as identical. In addition, query strings that are identical may be treated as different for other reasons. Queries that use different databases, different protocol versions, or different default character sets are considered different queries and are cached separately.

[...]

If a table changes, all cached queries that use the table become invalid and are removed from the cache. This includes queries that use MERGE tables that map to the changed table. A table can be changed by many types of statements, such as INSERT, UPDATE, DELETE, TRUNCATE TABLE, ALTER TABLE, DROP TABLE, or DROP DATABASE.

[...]

If you have a lot of queries with small results, the default block size may lead to memory fragmentation, as indicated by a large number of free blocks. Fragmentation can force the query cache to prune (delete) queries from the cache due to lack of memory

关于mysql - MySQL 上的查询缓存会在一段时间后自动重置吗?一些结果缓存了一点,但第二天会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57779487/

相关文章:

sql - 获取最近 90 天的记录 (SQL)

php - 使用 PHP 解密 mysql 数据库中的某些字段

amazon-web-services - 在 AWS Glue 中删除具有空值的行的问题

node.js - 无法让 AWS lambda 等待异步请求调用完成

php - 如何通过PHP从MySQL db获取数据并输出为JSON,然后在JQuery中跨域ajax请求数据

MySQL 查询在复合键表上运行速度非常慢

mysql - 从表中选择列后自连接

php - 使用 PHP/MySQL 计算百分比

mysql - 时间戳列的更新时间

amazon-web-services - Lambda 函数中的 DynamoDB BatchGetItem 动态表名