如何清除 MySQL 中 MEMORY 表的开销(Data_free)?

发布于 2024-10-09 15:10:32 字数 177 浏览 0 评论 0原文

我在 MySQL 中有一个用于实时聊天的 MEMORY 表(也许这不是最好的表类型?),并且每晚删除行以保持聊天日志易于管理,从而导致表中的开销。但是,由于无法在 MEMORY 表上运行 OPTIMIZE,因此如何消除开销(show table status 中的 Data_free)?

I have a MEMORY table in MySQL for a live chat (maybe this isn't the best table type for this?), and deleting rows every night to keep the chat logs manageable cause overhead in the table. However, since you can't run OPTIMIZE on a MEMORY table, how do you get rid of the overhead (Data_free in the show table status)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2024-10-16 15:10:32

如何摆脱开销?

您可以强制使用 MEMORY/HEAP 存储引擎的表通过更改但不更改任何内容来恢复已删除行丢失的剩余空间。例如

ALTER TABLE my_table ENGINE=MEMORY;

它将重写该表。用文档中的引用来支持这一点

释放行使用的内存
已删除的,使用 ALTER
TABLE ENGINE=MEMORY 强制使用表
重建。

编辑

也许这不是最好的表格类型?

在我看来,这听起来不像是 MEMORY 表的理想应用——总的来说,我认为它是一个遗留引擎。一些值得深思的问题

首先,MEMORY 表不能/不使用 b 树索引(仅哈希索引),因此可以使用索引进行排序或范围(即 <、> 操作)的查询手动/详尽地排序/过滤。

其次,如果你的 innodb_buffer_pool 足够大,Innodb 表将驻留在 RAM 中,并且在并发线程下工作得更好,因此对于大多数应用程序来说,它的性能通常与 MEMORY 表一样好,甚至更好。

第三,也许最重要的是,如果您的 MySQL 关闭,您将丢失表中的所有数据。如果您使用复制,也会产生表截断的影响。

how do you get rid of the overhead?

You can force a table using the MEMORY/HEAP storage engine to recover residual space lost from deleted rows by ALTERing it, but not changing anything. e.g.

ALTER TABLE my_table ENGINE=MEMORY;

It will re-write the table. Backing that up with a quote from the documentation:

To free up the memory used by rows
that have been deleted, use ALTER
TABLE ENGINE=MEMORY to force a table
rebuild.

EDIT

maybe this isn't the best table type for this?

It doesn't sound like the ideal application of a MEMORY table in my opinion -- by and large, I consider it a legacy engine. Some food-for-thought

Firstly, MEMORY tables can't/don't use b-tree indexes (hash indexes only), so queries which could otherwise use an index for ORDERing or ranging (i.e. <, > operations) resort to sorting/filtering manually/exhaustively.

Secondly, Innodb tables will reside in RAM if your innodb_buffer_pool is large enough, and works better with concurrent threads, so it often performs as good if not better than a MEMORY table for most applications.

Thirdly, perhaps most importantly, if your MySQL is ever turned off, you'll lose all data in your table. There are table truncation implications also if you're using replication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文