我想知道如何制作正确的索引表,为了理解这个概念,我将以电影为例:
我有这 5 个包含多个字段的表,但我将在此处仅列出这些表的主索引字段:
movies
movie_id = primary index
actors
actor_id = primary index
geners
gener_id = primary index
reviews
review_id = primary index
然后我有这些表与2列的关系,我不确定什么类型的索引应该有这些关系表:
movie_actor
movie_id,actor_id
movie_gener
movie_id,gener_id
movie_review
movie_id,review_id
我已经加入这些字段如果我想获得一部电影的详细信息,我将使用这样的查询:
SELECT *
FROM movies as m
LEFT JOIN movie_actor AS ma ON ma.movie_id = m.movie_id
LEFT JOIN actors AS a ON a.ator_id = ma.actor_id
LEFT JOIN movie_gener AS mg ON mg.movie_id = m.movie_id
LEFT JOIN geners AS g ON g.gener_id = mg.gener_id
LEFT JOIN movie_review AS mr ON mr.movie_id = m.movie_id
LEFT JOIN reviews AS r ON r.review_id = mr.review_id
WHERE m.movie_id = 1234
那么我应该在关系表(movie_actor,movie_gener,movie_review)这两个字段上使用哪种索引,主要在哪个字段上或仅在两者上都使用索引?谢谢