如何设计数据库模式以通过许多其他表链接两个表
虽然我使用的是Rails,但这个问题更多的是关于数据库设计。我的数据库中有几个实体,其架构有点像这样: http://fishwebby.posterous.com/40423840
如果我想获取一个人的列表并按姓氏排序,那没问题。但是,如果我想获取按姓氏排序、加入特定组的人员列表,我必须使用包含四个表之间的多个联接的 SQL 语句,如下所示
SELECT group_enrolment.*, person.*
FROM person INNER JOIN member ON person.id = member.person_id
INNER JOIN enrolment ON member.id = enrolment.member_id
INNER JOIN group_enrolment ON enrolment.id = group_enrolment.enrolment_id
WHERE group_enrolment.id = 123
ORDER BY person.surname;
:效率有点低,并且随着我的架构的增长,这些查询可能会变得越来越复杂。
另一种选择可能是通过在其他表中包含 person_id 将 person 表连接到查询中的所有其他表,那么它只是一个单一连接,例如
SELECT group_enrolment.*, person.*
FROM person INNER JOIN group_enrolment ON group_enrolment.person_id
WHERE group_enrolment.id = 123
ORDER BY person.surname;
但这意味着在我的模式中, person 表被连接到很多其他表。除了复杂的模式图之外,有人认为这样做有什么缺点吗?
我将非常感谢对此的任何评论 - 无论我现在正在做的事情(多表连接)还是第二个解决方案或我没有想到的另一个解决方案都是最好的方法。
预先非常感谢
Although I'm using Rails, this question is more about database design. I have several entities in my database, with the schema a bit like this: http://fishwebby.posterous.com/40423840
If I want to get a list of people and order it by surname, that's no problem. However, if I want to get a list of people, ordered by surname, enrolled in a particular group, I have to use an SQL statement that includes several joins across four tables, something like this:
SELECT group_enrolment.*, person.*
FROM person INNER JOIN member ON person.id = member.person_id
INNER JOIN enrolment ON member.id = enrolment.member_id
INNER JOIN group_enrolment ON enrolment.id = group_enrolment.enrolment_id
WHERE group_enrolment.id = 123
ORDER BY person.surname;
Although this works, it strikes me as a bit inefficient, and potentially as my schema grows, these queries could get more and more complicated.
Another option could be to join the person table to all the other tables in the query by including person_id in the other tables, then it would just be one single join, for example
SELECT group_enrolment.*, person.*
FROM person INNER JOIN group_enrolment ON group_enrolment.person_id
WHERE group_enrolment.id = 123
ORDER BY person.surname;
But this would mean that in my schema, the person table is joined to a lot of other tables. Aside from a complicated schema diagram, does anyone see any disadvantages to this?
I'd be very grateful for any comments on this - whether what I'm doing now (the many table join) or the second solution or another one that hasn't occurred to me is the best way to go.
Many thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,连接就是数据库的作用。话虽如此,您可以考虑在模型中传播自然键,这样您就可以跳过连接中的某些表。请查看此示例。
编辑
我并不是说这会匹配您的模型(问题),但只是为了好玩尝试类似的查询,如下所示:
Well, joins are what databases do. Having said that, you may consider propagating natural keys in your model, which would then allow you to skip over some tables in joins. Take a look at this example.
EDIT
I'm not saying that this will match your model (problem), but just for fun try similar queries on something like this: