数据库关系概念
早上好,专家们
,我需要你们的建议。假设我们有 user
、profile
和 post
实体。
变体 1:user
有许多 profile
,profile
有许多 post
变体 2:user
> 有很多个人资料
,用户
有很多帖子
什么是最好的?是为了方便、“正确的方式”,还是性能?
Good morning, experts
I need your advice. Let's say we have user
, profile
and post
entities.
Variant 1: user
has many profile
, profile
has many post
Variant 2: user
has many profile
, user
has many post
What's the best? By convenience, "right way", or maybe even performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于...
1) 系统如何工作...帖子是否与个人资料相关联。那么您应该使用变体 1。例如,我在 Stackoverflow 中发布的问题应该与我的 Stackoverflow 个人资料相关联。我在超级用户中发布的问题应该与该个人资料相关联。
如果您想在变体 2 中保持这种类型的行为。帖子必须有一个字段来指示帖子所属的位置。
2) 关于您将如何使用这些数据。如果您在查询帖子时不需要个人资料信息,那么您可能不应该将个人资料和帖子关联起来。
3)在性能方面,使用非常基本的逻辑,跨3个表连接来检索一些信息可能比跨2个表连接更糟糕(当然取决于连接中的列,索引等......但说所有其他是平等的)。
我认为最好的办法就是保持简单。保持简单,可以更容易地确定某些事情是否会按照您期望的方式工作。如果没有,会更容易找出原因并修复它。如果需要,您以后也可以更轻松地对其进行修改。
It depends...
1) on how the system works..if the posts are associated with profiles. Then you should use Variant 1. For example, the questions I post in Stackoverflow should be associated with my Stackoverflow profile. The questions I post in Superuser should be associated with that profile.
If you want to maintain this type of behaviour with Variant 2. The post would have to have a field indicating where the post belongs.
2) on how you will use the data. If you do not need the profile information when querying for posts, then may be you shouldn't associate profile and post.
3) In terms of performance, using very basic logic, joining across 3 tables to retrieve some information is likely to be worse than joining across 2 tables (of course depends on the columns in the join, index etc...but say all else being equal).
I think the best thing to do is to keep it simple. Keeping it simple, it'll be easier to figure out if something will work the way you expect it to. If it doesn't, it will be easier to figure out why and fix it. And if required, it'll be easier for you to modify it later on as well.
假设您想问:“一般来说,在设计数据模型时,应该最重视哪些考虑因素 - 方便性、正确性还是性能?”,那么我会说正确性应该是首要任务。便利性和性能将是次要问题。
Assuming you mean to ask: "In general, when designing a data model, what considerations should be valued highest - convenience, correctness, or performance?", then I would say that correctness should be a first priority. Convenience and performance will be secondary concerns.
选项 2 对我来说似乎更好。
但指定您在个人资料中存储的信息类型将有助于理解。
我假设配置文件包含角色/类别信息。
Option 2 seems much better to me.
But specifying what kind of information your store in a profile would help understanding.
I assumed that profile holds role/category information.