我做了这样的查询:
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 asINSERT
,UPDATE
,DELETE
,TRUNCATE TABLE
,ALTER TABLE
,DROP TABLE
, orDROP 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/