更改架构时如何加快 MySQL 中的表重建速度?

发布于 2024-12-06 13:19:00 字数 225 浏览 5 评论 0原文

我有一个相对较大的 MySQL InnoDB 表(已压缩),有时需要更改其架构(增加列大小或添加字段)。

对于一个包含数百万行的 500 MB 表大约需要 1 小时,但服务器似乎不是很忙(CPU @5%,使用的 RAM 不多,I/O 为 2.5 MB/s)。

该表未在生产中使用,因此不会同时发生并发请求。只有一个主索引(前 5 列)和一个外键约束。

您对如何加快表更改过程有什么建议吗?

I have a relatively big MySQL InnoDB table (compressed), and I sometimes need to alter its schema (increasing column size or adding a field).

It takes around 1 hour for a 500 MB table with millions of rows, but the server doesn't seem to be very busy (CPU @5%, not much RAM used, and 2.5 MB/s as I/O).

The table is not used in production so there are no concurrent requests at the same time. There is only a primary index (on the first 5 columns) and one foreign key constraint.

Do you have any suggestion on how to speed-up the table alteration process?

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

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

发布评论

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

评论(2

猫弦 2024-12-13 13:19:00

更改存储引擎(更换为新一代引擎,例如 TokuDB)似乎是可行的方法,直到 InnoDB 被“修复”。

Changing storage engine (to newer generation engines like TokuDB) seems the way to go, until InnoDB is "fixed".

夜访吸血鬼 2024-12-13 13:19:00

了解确切的表和主键/索引定义会很有帮助,而不太重要的是,行数精确到百万,尽管我猜测该表只有 500mb,可能少于 2000 万行。另外,您更改表的方法 - 您是创建一个新模式并插入其中,还是使用更改表等。

我之前在这方面取得了成功,采用了

  • 更改索引键组合、添加唯一键
  • 删除 等方法首先索引,然后更改表,然后添加回索引。有时,如果独立索引受到更改的影响,它们可能会成为真正的性能杀手。
  • 优化表结构以删除不需要的或过大的列,这些列
  • 分隔出的数据(通常是列,但在某些情况下可以垂直分区)不会从可能更改的核心结构中改变,因此您只需搅动表的一小部分

Would be helpful to know the exact table and primary key/index definitions, and of lesser importance, the row count to the nearest million, although I would guess as the table is only 500mb it's probably less than 20 million rows. Also, your approach to changing the table - are you creating a new schema and inserting into it, or using a alter table etc.

I've had success in this area before with approaches like

  • changing the index key composition, adding a unique key
  • dropping the indexes first, then changing the table, then adding the indexes back. sometimes independent indexes can be a real performance killer if they are affected by the change.
  • optimizing the table structure to remove unneeded or oversized columns
  • separating out data (normally columns but you can vertically partition in some circumstances) that won't change from the core structure that might change, so you only churn a smaller part of your table
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文