使用大量数据在现有 table 上创建 MySQL 索引

Create a MySQL index on already existing table with a lot of data

我有一个 Spring 引导服务和 MySQL 数据库 (AWS RDS)。

有一个特定的 table,包含大约 200 万行,对它的一些查询使 CPU 上升到数据库实例。

我注意到使用的列上没有索引,所以我想尝试添加这个索引。

问题是:

一些附加信息:

想法是创建这个索引:

CREATE INDEX my_key ON info (my_key);

使用 MySql 的更新版本,您可以 create an index 而无需锁定 table:

The table remains available for read and write operations while the index is being created. The CREATE INDEX statement only finishes after all transactions that are accessing the table are completed, so that the initial state of the index reflects the most recent contents of the table.

显然创建索引对数据库来说是一项额外的工作,因此如果您的数据库处于痛苦状态,请尝试在数据库上执行的活动减少时更新索引。