如何在 MySQL 8 InnoDB 中清除数据库缓存

huangapple go评论54阅读模式
英文:

How to clear the database cache in mysql 8 innodb

问题

我正尝试在同一个 MySQL 8 数据库和表上多次运行相同的查询。

我需要进行实验,以确定调整查询或表本身是否会提高性能。然而,在第一次尝试之后,响应时间要快得多,我认为是因为数据被缓存了。

MySQL 8 InnoDB

我有哪些选项可以清除缓存,以便从头开始获取数据。


看起来之前提出的答案都与 MySQL 5 有关,而不是 MySQL 8。大多数命令似乎现在已被弃用。

https://stackoverflow.com/questions/5231678/clear-mysql-query-cache-without-restarting-server

英文:

I am attempting to run the same query multiple times on the same mysql 8 database and table.

I need to carry out experiments to determine if tweaking the query and or table itself improves performance. However after the first attempt the response time is much faster, i assume becuase the data is cached.

mysql 8 innodb

What options do i have to clear the cache so the data is fetched from scratch.


It appears the answers that have been proposed before are all related to mysql 5 and not mysql 8. Most of the commands seem to now be deprecated.

https://stackoverflow.com/questions/5231678/clear-mysql-query-cache-without-restarting-server

答案1

得分: 3

你链接的问题涉及查询缓存,在MySQL 8.0中已被移除,因此不再需要清除它。

你的措辞表明你在询问缓冲池,这与查询缓存不同。缓冲池缓存数据和索引页面,而查询缓存(在存在时)缓存查询的结果。

没有命令可以在不重新启动MySQL服务器的情况下清除缓冲池。页面在缓冲池中保留,直到被其他页面逐出。

缓冲池位于RAM中,因此如果重新启动MySQL服务器进程,其内容将被清除。因此,如果你想从头开始,你需要重新启动该进程(不需要重新启动整个操作系统,只需重新启动MySQL服务)。

需要注意的是,在MySQL 8.0中,重新启动时并不完全清除缓冲池的内容。在关闭期间,缓冲池的一部分内容会被保存,并在启动时自动重新加载。此功能默认启用,但你可以选择禁用。

阅读更多信息:

英文:

The question you link to is about the query cache, which is removed in MySQL 8.0 so there's no need to clear it anymore.

Your wording suggests you are asking about the buffer pool, which is not the same as the query cache. The buffer pool caches data and index pages, whereas the query cache (when it existed) cached results of queries.

There is no command to clear the buffer pool without restarting the MySQL Server. Pages remain cached in the buffer pool until they are evicted by other pages.

The buffer pool is in RAM so its contents are cleared if you restart the MySQL Server process. So if you want to start from scratch, you would need to restart that process (you don't need to reboot the whole OS, just restart the MySQL service).

The caveat is that in MySQL 8.0, the contents of the buffer pool are not entirely cleared when you restart. A percentage of the content of the buffer pool is saved during shutdown, and reloaded automatically on startup. This feature is enabled by default, but you can optionally disable it.

Read more about this:

答案2

得分: 0

如果查询将在生产中经常运行,那么我认为你的目标是相反的。我建议你应该运行查询两次,并使用第二次的时间。这样,它更接近生产环境将看到的情况——数据已经在缓存中。

(Bill解释了“查询缓存”是无关紧要的,而“缓冲池”是相关的。)

这是一个我喜欢的在测试查询而无需构建足够大的表来查看时间变化的技巧:

FLUSH STATUS;
SELECT ...;
SHOW SESSION STATUS LIKE 'Handler%';

这些数字是实际的数据或索引行的读取/写入次数。数字可能看起来像输出行的数量,表中的行数等。这让你知道是否发生了全表扫描(或索引扫描)等情况。非零的“Handler_write%”值表示需要临时表(或表)。

如果你的版本支持,还可以查看EXPLAIN ANALYZE

更多提示:Index Cookbook

英文:

If the query is one that will be run frequently in production, then I claim that your goal is backward. I suggest that you should run the query twice, and use the timing from the second one. That way, it is closer to what the production will see -- data already in cache.

(Bill explains that the "Query cache" is irrelevant and that the "buffer pool" is relevant.)

Here's a technique that I like for testing queries without needing to build a big enough table to see timing changes:

FLUSH STATUS;
SELECT ...;
SHOW SESSION STATUS LIKE 'Handler%';

The numbers are actual reads/writes of data or index rows. Numbers may look like the number of output rows, the number of rows in the table, etc. This gives you a clue of whether there was, for example, a full table (or index) scan. Non-zero "Handler_write%" values indicate a temp table (or tables) was needed.

See also EXPLAIN ANALYZE if that is available on your version.

More tips: Index Cookbook

huangapple
  • 本文由 发表于 2023年2月19日 23:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定