简体   繁体   English

为什么要花这么长时间重命名mysql中的列?

[英]Why does it take so long to rename a column in mysql?

I have a 12 GB table full of pictures, I'm trying to rename the blob column that holds the data, and it is taking forever. 我有一个12 GB的桌子,上面满是图片,我正在尝试重命名保存数据的blob列,并且它一直在消耗。 Can someone give me a blow by blow account of why it is taking so long to rename the column? 有人可以给我一个吹毛求疵的说法,为什么重命名该列需要这么长时间? I would have thought that this operation would be pretty quick, no matter the size of the table? 我会认为,无论表的大小如何,此操作都将很快完成?

EDIT: The query I ran is as follows 编辑:我运行的查询如下

 alter table `rails_production`.`pictures` change `data` `image_file_data` mediumblob NULL

It appears that most of the time is spent waiting for mysql to make a temporary copy of the pictures table, which since it is very large is taking a while to do. 看来,大多数时间都花在等待mysql制作图片表的临时副本上,因为它很大,所以要花一些时间。

It is on the list of things to do, to change the picture storage from the database to the filesystem. 将图片存储从数据库更改为文件系统,这是要做的事情。

EDIT2: Mysql Server version: 5.0.51a-24+lenny2 (Debian) EDIT2:Mysql Server版本:5.0.51a-24 + lenny2(Debian)

I can't give you the blow-by-blow (feature request #34354 would help, except that it probably wouldn't be back-ported to MySQL 5.0), but the extra time is due to the fact that an ALTER ... CHANGE may change the type of the column (and column attributes, if any), which necessitates converting the values stored in the column and other checks. 我不能给您吹牛的声音 (功能请求#34354会有所帮助,但它可能不会反向移植到MySQL 5.0),但是额外的时间是由于ALTER ... CHANGE可能会更改列的类型(和列属性(如果有)),这需要转换存储在列中的值和其他检查。 MySQL 5.0 doesn't include optimizations for when the new type and attributes are the same as the old. 当新的类型和属性与旧的相同时,MySQL 5.0不包括优化。 From the documentation for ALTER under MySQL 5.0: 从MySQL 5.0下ALTER的文档中:

In most cases, ALTER TABLE works by making a temporary copy of the original table. 在大多数情况下,ALTER TABLE通过制作原始表的临时副本来工作。 The alteration is performed on the copy, and then the original table is deleted and the new one is renamed. 在副本上执行更改,然后删除原始表,并重命名新表。 While ALTER TABLE is executing, the original table is readable by other sessions. 执行ALTER TABLE时,其他会话可以读取原始表。 Updates and writes to the table are stalled until the new table is ready, and then are automatically redirected to the new table without any failed updates. 对表的更新和写入将暂停,直到准备好新表,然后将其自动重定向到新表,而不会进行任何失败的更新。

[...] [...]

If you use any option to ALTER TABLE other than RENAME, MySQL always creates a temporary table, even if the data wouldn't strictly need to be copied (such as when you change the name of a column). 如果您使用除RENAME以外的任何ALTER TABLE选项,MySQL始终会创建一个临时表,即使严格不需要复制数据(例如,当您更改列名时)也是如此。

Under 5.1, ALTER has some additional optimizations: 在5.1下, ALTER进行了一些其他优化:

In some cases, no temporary table is necessary: 在某些情况下,不需要临时表:

  • Alterations that modify only table metadata and not table data can be made immediately by altering the table's .frm file and not touching table contents. 可以通过更改表的.frm文件而不接触表内容来立即进行仅修改表元数据而不修改表数据的更改。 The following changes are fast alterations that can be made this way: 以下更改是可以通过这种方式进行的快速更改:

    • Renaming a column, except for the InnoDB storage engine . 重命名列, 但InnoDB存储引擎除外

[...] [...]

Because MySQL will rebuild the entire table when you make schema changes. 因为当您进行模式更改时,MySQL将重建整个表。

This is done because it's the only way of doing it in some cases, and it makes it much easier for the server to rebuild it anyway. 这样做是因为在某些情况下这是唯一的方法,而且服务器无论如何都更容易重建它。

Yes mysql does a temporary copy of the table. 是的,mysql会做表的临时副本。 I don't think there's an easy way around that. 我认为没有简单的方法可以解决此问题。 You should really think about to store the pictures on the filesystem and only store paths in mysql. 您应该真正考虑将图片存储在文件系统上,并且仅将路径存储在mysql中。 That's the only way to fasten it up, I guess. 我猜这是唯一的固定方法。

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

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