简体   繁体   中英

'not small' table in mysql with innodb compact stoage is slow to add a column

有人可以解释为什么在一个innodb紧凑表中添加一个列,其中包含大量数据会很慢吗?

Using ALTER TABLE to add a column to a table requires the entire table to be rebuilt, which is slow for a large table.

If you want to continue using the table while the ALTER is running, you should consider using the pt-online-schema-change tool:

http://www.percona.com/doc/percona-toolkit/2.1/pt-online-schema-change.html

From the mysql documentation of compact tables it states that the header of each record has a bit which indicates if the field is null or not. If you add a column you'll have to change the size of that header and that will at the very least change the size of all rows in that table.

Excerpt: Rows in InnoDB tables that use COMPACT row format have the following characteristics:

Each index record contains a five-byte header that may be preceded by a variable-length header. The header is used to link together consecutive records, and also in row-level locking.

The variable-length part of the record header contains a bit vector for indicating NULL columns. If the number of columns in the index that can be NULL is N, the bit vector occupies CEILING(N/8) bytes. (For example, if there are anywhere from 9 to 15 columns that can be NULL, the bit vector uses two bytes.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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