为什么我对字段做fk约束时KEY自动生成了?

Why KEY has automatically created when I make fk constraint for field?

我正在使用以下语法创建 table:

CREATE TABLE movies_genres 
(
    id BIGINT AUTO_INCREMENT,
    movie_id INT NOT NULL,
    genre_id INT NOT NULL,
    
    PRIMARY KEY (id),
    CONSTRAINT `fk_movie_id` FOREIGN KEY (movie_id) REFERENCES movies(id),
    CONSTRAINT `fk_genre_id` FOREIGN KEY (genre_id) REFERENCES genres(id),
    CONSTRAINT unique_id_pair UNIQUE(movie_id, genre_id)
);

但后来我查看了 MySQL Workbench 中关于 table 的信息,我看到:

CREATE TABLE `movies_genres` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `movie_id` int NOT NULL,
  `genre_id` int NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_id_pair` (`movie_id`,`genre_id`),
  KEY `fk_genre_id` (`genre_id`),
  CONSTRAINT `fk_genre_id` FOREIGN KEY (`genre_id`) REFERENCES `genres` (`id`),
  CONSTRAINT `fk_movie_id` FOREIGN KEY (`movie_id`) REFERENCES `movies` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

为什么会生成这行代码?

KEY `fk_genre_id` (`genre_id`)

我还看到创建了我没有订购的额外索引...

Screenshot with extra index

https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html 说:

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist.

(强调我的)

(Bill 给出了答案;这提供了另一个提示。)

从 table 中去掉 id 并更改为这两个索引:

PRIMARY KEY (`movie_id`,`genre_id`),
KEY `fk_genre_id` (`genre_id`),

这将使您对 many-to-many table 运行 的一些使用更快。它还会缩小 table 大小。

如果需要id

由于single-row删除和更新需要id,保留id,但使用

PRIMARY KEY (id)
INDEX(`movie_id`, `genre_id`),
INDEX(`genre_id`, `movie_id`),

PK会继续让Delete/Update高效;其他两个索引将使 many-to-many JOIN 高效。