简体   繁体   English

重命名比MySQL alter table中的DROP + ADD更快

[英]RENAME faster than DROP+ADD in MySQL alter table

I'm performing some MySQL table maintenance that will mean removing some redundant columns and adding some new ones. 我正在执行一些MySQL表维护,这意味着删除一些冗余列并添加一些新列。

Some of the columns to drop are of the same type as ones to add. 要删除的某些列与要添加的列具有相同的类型。 Would the procedure be faster if I took advantage of this and reused some of the existing columns? 如果我利用这一点并重用了一些现有的列,该过程会更快吗?

My rationale is that changing column names should be a simple table metadata change, whereas removing and adding columns means either finding room at the end of the file (fragmenting data) or rebuilding every row with the correct columns so that they're at the same place on the disk. 我的基本原理是,更改列名应该是对表元数据的简单更改,而删除和添加列意味着在文件末尾找到空间(碎片数据),或者使用正确的列重建每一行以使它们位于同一列放在磁盘上。

The engine in question is MyISAM and I'm not up to scratch on how exactly it'll treat this so I'd like to hear from anyone who has been in the same situation before! 有问题的引擎是MyISAM,我不知道如何处理该问题,因此,我想听听以前处于相同情况的任何人的来信!

Unless you have a serious issue with performance, I wouldn't take the renaming approach - because of all the dirty data you're going to leave lying around. 除非您对性能有严重的问题,否则我不会采用重命名的方法-因为您将留下所有脏数据。

Also, by dropping the table, you will cause any indexes to get re-built - which is a good idea every once in a while... 另外,通过删除表,您将导致任何索引都被重新构建-这是一个不时的好主意...

Martin 马丁

I would drop the columns. 我会删除列。 You will have fragmentation either way. 两种方式都会有碎片。 That should be handled in your regular maintenance plans. 这应该在您的定期维护计划中进行处理。 You could accelerate those after a large number of modification operations. 经过大量修改操作后,您可以加速这些操作。

如果您不知道,在Myisam表中,每个ALTER TABLE操作都将复制整个表,因此在服务器需要复制表时,该表将被锁定。

I've used that same logic, and got stung because even with changes that are supposed to not require rewriting the table (ie a table rename), a MySQL bug caused it to think it was a change that required rewriting the table. 我使用了相同的逻辑,并且因为即使不需要更改表(即重命名表)的更改,MySQL的错误仍导致它认为这是需要重写表的更改。

If the fields you are dealing with are date, datetime or timestamp fields, you are likely to be hit by this, which means that you should just assume it has to do a full rewrite and plan that way. 如果要处理的字段是date,datetime或timestamp字段,则很可能会遇到此问题,这意味着您应该假定它必须进行完全重写并计划这种方式。

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

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