2017-12-26 12:33
浏览 219


I'm building a website that contains articles, these articles won't be shown until the user is logged in, I'm planning to add two buttons, the first one is called "follow this article", the second is meant to make the article as a favourite. My question is how can I structure that in my database?

Should I just add a table that contains id_article, id_user, followed(1 or 0) and favourite (1 or 0), or is there another solution that would let me economize more memory space?

NOTE: My website will have lots of articles that will be visited by a lot of users.

图片转代码服务由CSDN问答提供 功能建议

我正在建立一个包含文章的网站,这些文章在用户登录前不会显示, 我打算添加两个按钮,第一个叫做“关注这篇文章”,第二个是打算把文章作为最爱。 我的问题是如何在我的数据库中构建它?

我应该只添加一个包含 id_article id_user ,<的表 代码>跟随(1或0)收藏夹(1或0),还是有另一种解决方案让我节省更多的内存空间? < 注意:我的网站上会有很多文章会被很多用户访问。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhoulei8959 2017-12-26 13:24

    Make two separate relations: articles_followed (id, id_article, id_user) and articles_favourite (id, id_article, id_user). This will give you enough flexibility in:

    • adding new attributes, specific for each relation,
    • sharding your database (at some point): it'd be easier to move one relation from another since they are not depending on each other,
    • concurrent update: you can use separate locks on these two, in addition, you'll have fewer issues with transaction visibility,
    • you don't need to keep is_followed or is_favourite flag: if there's an entry, then it means that article is followed (once unfollowed, simply remove the line)

    Think about your domain classes structure -- which option would be more convenient to code? I bet, in case of two separate relations, it would be clearer.

    打赏 评论

相关推荐 更多相似问题