给aMySQL5.5table添加索引时btree是什么时候创建的?
When is the btree created when adding an index to a MySQL 5.5 table?
我在 MySQL 5.5 中有相当大的 table,约 200M 行,我想向此 table 中的其中一列添加索引(btree 类型) .该列是整数类型,包含广泛分布的整数。
我的问题是什么时候计算 btree?
当我执行简单的创建索引查询时:
ALTER TABLE bigtable ADD INDEX (column3);
马上returns。 btree 的计算是否在后台进行?我无法想象 MySQL 在创建具有广泛整数分布的 ~200M 值的 btree 时如此之快。
简短回答:是。
长答案:查看 ALTER_TABLE 的 MySQL Documentation 揭示了以下内容:
In most cases, ALTER TABLE makes a temporary copy of the original table. MySQL waits for other operations that are modifying the table, then proceeds. It incorporates the alteration into the copy, deletes the original table, and renames the new one. While ALTER TABLE is executing, the original table is readable by other sessions (with the exception noted shortly). Updates and writes to the table that begin after the ALTER TABLE operation begins are stalled until the new table is ready, then are automatically redirected to the new table without any failed updates. The temporary copy of the original table is created in the database directory of the new table. This can differ from the database directory of the original table for ALTER TABLE operations that rename the table to a different database.
因此,当您创建索引时,会在 table 的临时副本上创建索引,然后在完成时导入该副本以代替现已删除的原始 table。
我在 MySQL 5.5 中有相当大的 table,约 200M 行,我想向此 table 中的其中一列添加索引(btree 类型) .该列是整数类型,包含广泛分布的整数。
我的问题是什么时候计算 btree?
当我执行简单的创建索引查询时:
ALTER TABLE bigtable ADD INDEX (column3);
马上returns。 btree 的计算是否在后台进行?我无法想象 MySQL 在创建具有广泛整数分布的 ~200M 值的 btree 时如此之快。
简短回答:是。
长答案:查看 ALTER_TABLE 的 MySQL Documentation 揭示了以下内容:
In most cases, ALTER TABLE makes a temporary copy of the original table. MySQL waits for other operations that are modifying the table, then proceeds. It incorporates the alteration into the copy, deletes the original table, and renames the new one. While ALTER TABLE is executing, the original table is readable by other sessions (with the exception noted shortly). Updates and writes to the table that begin after the ALTER TABLE operation begins are stalled until the new table is ready, then are automatically redirected to the new table without any failed updates. The temporary copy of the original table is created in the database directory of the new table. This can differ from the database directory of the original table for ALTER TABLE operations that rename the table to a different database.
因此,当您创建索引时,会在 table 的临时副本上创建索引,然后在完成时导入该副本以代替现已删除的原始 table。