dongzuan4491 2017-12-26 12:33
浏览 254
已采纳

如何构建我的数据库以存储我的网站的用户行为(关注,收藏...)[关闭]

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.

  • 写回答

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.

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?