DeNormalize 多对多但不想在 SQL 世界中存储冗余数据
假设我有新闻通讯
,然后是订阅者
:
每当用户订阅
新闻通讯时,我们需要将其存储在表中 - 订阅者列表。
我应该将其存储在新闻通讯表
中还是用户表
中?即,时事通讯是否应该存储订阅者列表,或者用户是否也应该存储他已订阅的时事通讯列表?这两种情况都将被广泛使用。用户还需要显示他已订阅的新闻通讯,并且新闻通讯需要显示订阅它的用户。
如何设计一个表结构来优化读取?我不想走NoSql路线。
Say I have newsletters
and then subscribers
:
Whenever a user subscribes
to a newsletter we need to store this in a table - the subscribers list.
Should I store this in the newsletter table
or in the user table
? I.e. should the newsletter store the list of subscribers or should the user store the list of newsletters he has subscribed too? Both the cases will be widely used. User will need to show the newsletters he has subscribed too and newsletters will need to show the users subscribed to it.
How can I design a table structure for optimized for reads? I don’t want to go the NoSql route.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
Users
和Subscribers
表是否相同?如果不是,他们应该是。Users
表应包含您的用户、Newsletters
表、您的新闻通讯和您的订阅者
以及两者之间的关系。假设您有:
用户
新闻通讯
订阅者
用户 a 订阅了 x 和 y,用户 b 订阅了 y。您还应该在订阅者表的
user_id
和newsletter_id
之后添加索引,并且 PK 应为(user_id,newsletter_id)
。Are your
Users
andSubscribers
tables the same? If not they should be. TheUsers
table should contain your users, theNewsletters
table, your newsletters and yourSubscribers
the relation between the two.Say you have:
Users
Newsletters
Subscribers
User a is subscribed to x and y, user b is subscribed to y. You should also add indexing after
user_id
andnewsletter_id
to the Subscribers table and the PK should be(user_id,newsletter_id)
.这就是多对多表的用途。您将两者之间的关系存储在单独的表中。例子:
This is what many-to-many tables are for. You store the relationship between the two in a separate table. Example: