简体   繁体   English

如何更改大型 mysql 数据库中的表

[英]How to alter a table in large mysql DataBase

I have very large mysql DB(more than 100 G) and I want to migrate some changes in table.我有非常大的 mysql DB(超过 100 G),我想迁移表中的一些更改。 It need to posing that I have no space to make backup for this size, as results, it rolls back all changes.它需要冒充我没有空间来备份这个大小,结果,它回滚了所有更改。 In clear way, when I want to alter table in mysql, it backup for it self in local disk and because of no space on my disk, it 's rolling back.明确地说,当我想alter mysql 中的表时,它会在本地磁盘中自行备份,并且由于我的磁盘上没有空间,它正在回滚。 Is there any one to help for this issue?有没有人可以帮助解决这个问题?

It depends on the type of ALTER change.这取决于 ALTER 更改的类型。 Since MySQL 5.6, some alterations can be done "inplace" which does not use extra storage.从 MySQL 5.6 开始,可以“就地”完成一些更改,而不使用额外的存储空间。 Read https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html for details.阅读https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html了解详细信息。

But many types of alterations (basically any change that affects the storage size of a row), still require a "table rebuild" which means it makes a copy of the table in the process of doing the alter.但是许多类型的更改(基本上是影响行存储大小的任何更改)仍然需要“表重建”,这意味着它在进行更改的过程中会生成表的副本。 This requires extra storage, usually about the same as the size of the original table.这需要额外的存储空间,通常与原始表的大小大致相同。 If your server does not have enough storage space to do this, then you cannot alter the table, FULL STOP.如果您的服务器没有足够的存储空间来执行此操作,那么您无法更改表,FULL STOP。 You should have addressed this long before the table grew to this size.您应该在表格增长到这个大小之前很久就解决这个问题。 It is your responsibility to monitor the size of the database and make sure you have enough storage space.您有责任监控数据库的大小并确保您有足够的存储空间。

You said in the comments above that it is not possible to add more storage space.您在上面的评论中说无法添加更多存储空间。

Sometimes InnoDB tablespaces can become "fragmented."有时 InnoDB 表空间会变得“碎片化”。 That means they could store the same rows using slightly less storage space.这意味着他们可以使用稍少的存储空间来存储相同的行。 You can accomplish defragmentation with OPTIMIZE TABLE <name>;您可以使用OPTIMIZE TABLE <name>; or ALTER TABLE <name> FORCE;ALTER TABLE <name> FORCE;

A possible strategy is to optimize each of your tables, starting with the smallest table and doing one by one in order of size.一种可能的策略是优化每个表,从最小的表开始,按大小顺序逐个进行。 You hope that you have enough space for the copy table needed to do this, and any space freed up by optimizing the small tables will help you to optimize the medium-size tables, and so on.您希望您有足够的空间用于执行此操作所需的复制表,并且通过优化小表释放的任何空间都将帮助您优化中型表,依此类推。 This is a long shot, because it's also possible you don't have enough "wasted" space to recover to make a difference.这是一个长镜头,因为您也可能没有足够的“浪费”空间来恢复以产生影响。 You may still not be able to alter your largest tables once you're all done with this process.完成此过程后,您可能仍然无法更改最大的表。 Unfortunately, MySQL has no way of reporting fragmentation of the tables, so you can only try it and see how much space, if any, you recover.不幸的是,MySQL 没有办法报告表的碎片,所以你只能试试看,如果有的话,你恢复了多少空间。

After that, the only other option is to delete data from the table until you can do the alteration.之后,唯一的其他选择是从表中删除数据,直到您可以进行更改。 The alter still requires extra storage, but only as much as needed to copy the remaining rows of data.更改仍然需要额外的存储空间,但只需要复制剩余的数据行。 That is, if you delete 50% of the rows, then the tablespace file that stores the copy only needs roughly 50% of the storage space.也就是说,如果你删除了 50% 的行,那么存储副本的表空间文件只需要大约 50% 的存储空间。

You can also try dropping some of the indexes of the table as you do your alteration.您还可以尝试在更改时删除表的一些索引。 Indexes take space in the same tablespace, and it's not uncommon if you have multiple indexes for them to take more space than the data itself.索引在同一个表空间中占用空间,如果您有多个索引让它们占用比数据本身更多的空间,这种情况并不少见。 If you include some DROP INDEX operations in your ALTER TABLE statement, the resulting copy will take less space.如果在 ALTER TABLE 语句中包含一些 DROP INDEX 操作,则生成的副本将占用更少的空间。 On the other hand, those indexes might be necessary for proper query optimization, and without the indexes, your application won't be usable.另一方面,这些索引可能是正确查询优化所必需的,没有索引,您的应用程序将无法使用。

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

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