简体   繁体   English

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

[英]How can I speed-up the table reconstruction in MySQL when altering the schema?

I have a relatively big MySQL InnoDB table (compressed), and I sometimes need to alter its schema (increasing column size or adding a field). 我有一个相对较大的MySQL InnoDB表(已压缩),有时我需要更改其架构(增加列大小或添加字段)。

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). 具有数百万行的500 MB表大约需要1个小时,但是服务器似乎并不很忙(CPU @ 5%,未使用太多RAM,并且I / O为2.5 MB / s)。

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. 只有一个主索引(在前5列上)和一个外键约束。

Do you have any suggestion on how to speed-up the table alteration process? 您对如何加快表格更改过程有任何建议吗?

在InnoDB被“修复”之前,改变存储引擎(向TokuDB之类的新一代引擎)似乎是必须的。

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. 了解确切的表和主键/索引定义会有所帮助,并且次要的是,将行数精确到最接近的百万,尽管我猜想因为表只有500mb,所以可能少于2000万行。 Also, your approach to changing the table - are you creating a new schema and inserting into it, or using a alter table etc. 另外,更改表的方法-是创建新的架构并将其插入其中,还是使用alter table等。

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 分离出不会改变的数据(通常是列,但在某些情况下可以垂直分区)和可能改变的核心结构,因此您只需要搅动表的一小部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM