正确使用mysql中的索引

时间:2013-04-09 07:14:38

标签: php mysql

我想知道如何制作一个合适的索引表,理解这个概念,我将以电影为例:

我有这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)两个字段上使用什么样的索引,哪个是哪个,哪个只是索引? 感谢

1 个答案:

答案 0 :(得分:1)

每个表只能有一个字段作为主键您的表只能有1个主键(由用户更正)。请查看此帖子以获取信息Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?