doutao1282 2013-04-09 07:14
浏览 52
已采纳

正确使用mysql中的索引

I would like to know that how can I make a proper Indexed table, to understand this concept I will be using movies as example:

I have these 5 tables with multiple fields but I will list here these tables primary indexes fields only:

movies
  movie_id = primary index
actors
  actor_id = primary index
geners
  gener_id = primary index
reviews
  review_id = primary index

And then I have these tables for relation with 2 columns each, I am unsure what type of indexes should have these relational tables:

movie_actor
   movie_id,actor_id
movie_gener
   movie_id,gener_id
movie_review
   movie_id,review_id

I have join on these fields If I wants to get one movie details I will use such query:

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

So what kind of index should I use on relational tables (movie_actor,movie_gener,movie_review) both fields, primary on which one or just index on both? Thanks

  • 写回答

1条回答 默认 最新

  • dongpo5239 2013-04-09 07:21
    关注

    Only one field can be the primary key on each table Your table can only have 1 primary key (corrected by user). Look at this post for information Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题