两个澄清开头:
> MySQL查询缓存是服务器端功能,没有“本地缓存”这样的东西.你可能会对FLUSH命令中的LOCAL关键字感到困惑.正如docs解释的那样,它只是NO_WRITE_TO_BINLOG的别名(因此它与复制有关,“本地”意味着“此服务器”).
>如果您启用了该功能,MySQL将仅返回缓存数据,并将其设置为默认值或选择使用SQL_CACHE提示.根据我的经验,大多数服务器默认没有它.
The query cache is shared among sessions, so a result set generated by
one client can be sent in response to the same query issued by another
client.
这是有道理的:无法重用存储数据的缓存不那么有用.
我不知道你想要测试什么.您的数据应始终保持新鲜:
The query cache does not return stale data. When tables are modified,
any relevant entries in the query cache are flushed.
但是,您可能想知道查询运行多长时间.您可以随时选择退出the SQL_NO_CACHE keyword:
The server does not use the query cache. It neither checks the query
cache to see whether the result is already cached, nor does it cache
the query result.
只要考虑到第二次运行的查询即使没有缓存也可能运行得更快,因为部分数据段可能已经加载到RAM中.