类似QQ空间的社交网站的用户动态的数据库应该怎么设计?
最近在做类似的功能,遇到几个问题:
1. 动态类型多样性;
2. 数据模块化存储,各模块间通过rest调用数据,造成拉取动态列表响应时间变长;
3. 数据层级复杂,编码逻辑通用性差。例如:转发一篇文章后,评论该转发,然后回复该评论时的对象层级为: 回复->评论->文章。
求有相关经验者分享一下经验为谢!
以下是现有的设计:
动态的结构:
<code> { user_id: 动态创建者ID, action: 行为类型, object_type: 动态对象类型, object_id: 对象ID, object_user: 对象所有者, view_count: 0, created_at: 创建时间, deleted_at: 删除时间, } </code>
场景列表:
<code>// A 发布 了 文章 xxx 'action' => NEW, 'user_id' => A的ID, 'object_id' => 文章ID, 'object_user' => A的ID, 'object_type' => ARTICLE, 'ext' => [], // A 发布 了 N张 图片 'action' => NEW, 'user_id' => A的ID, 'object_id' => 图片ID(数组,以逗号隔开), 'object_user' => A的ID, 'object_type' => PICTURE, 'ext' => [], // 4. A 提了 问题 xxxx 'action' => NEW, 'user_id' => A的ID, 'object_id' => 问题ID, 'object_user' => A的ID, 'object_type' => QUESTION, 'ext' => [], // 5. A 在 文章 中回复了 B 的 评论 'action' => REPLY, 'user_id' => A的ID, 'object_id' => 评论ID, 'object_user' => B的ID, 'object_type' => COMMENT, 'ext' => [ 'text' => $text, 'comment_target_id' => '文章ID', //评论所属对象 'comment_target_type' => 'ARTICLE',//评论所属对象类型 'reply_id' => 回复ID, ], // 6. A 评论 了 B的 文章 xxxx 'action' => COMMENT, 'user_id' => A的ID, 'object_id' => 文章ID, 'object_user' => B的ID, 'object_type' => 'ARTICLE', 'ext' => [ 'comment_id' => '评论ID', ], // 7. A 回答 了 B 的 提问 xxx 'action' => RESPOND, 'user_id' => A的ID, 'object_id' => 问题ID, 'object_user' => B的ID, 'object_type' => QUESTION, 'ext' => [ 'answer_id' => '答案ID', ], </code>
回复内容:
最近在做类似的功能,遇到几个问题:
1. 动态类型多样性;
2. 数据模块化存储,各模块间通过rest调用数据,造成拉取动态列表响应时间变长;
3. 数据层级复杂,编码逻辑通用性差。例如:转发一篇文章后,评论该转发,然后回复该评论时的对象层级为: 回复->评论->文章。
求有相关经验者分享一下经验为谢!
以下是现有的设计:
动态的结构:
<code> { user_id: 动态创建者ID, action: 行为类型, object_type: 动态对象类型, object_id: 对象ID, object_user: 对象所有者, view_count: 0, created_at: 创建时间, deleted_at: 删除时间, } </code>
场景列表:
<code>// A 发布 了 文章 xxx 'action' => NEW, 'user_id' => A的ID, 'object_id' => 文章ID, 'object_user' => A的ID, 'object_type' => ARTICLE, 'ext' => [], // A 发布 了 N张 图片 'action' => NEW, 'user_id' => A的ID, 'object_id' => 图片ID(数组,以逗号隔开), 'object_user' => A的ID, 'object_type' => PICTURE, 'ext' => [], // 4. A 提了 问题 xxxx 'action' => NEW, 'user_id' => A的ID, 'object_id' => 问题ID, 'object_user' => A的ID, 'object_type' => QUESTION, 'ext' => [], // 5. A 在 文章 中回复了 B 的 评论 'action' => REPLY, 'user_id' => A的ID, 'object_id' => 评论ID, 'object_user' => B的ID, 'object_type' => COMMENT, 'ext' => [ 'text' => $text, 'comment_target_id' => '文章ID', //评论所属对象 'comment_target_type' => 'ARTICLE',//评论所属对象类型 'reply_id' => 回复ID, ], // 6. A 评论 了 B的 文章 xxxx 'action' => COMMENT, 'user_id' => A的ID, 'object_id' => 文章ID, 'object_user' => B的ID, 'object_type' => 'ARTICLE', 'ext' => [ 'comment_id' => '评论ID', ], // 7. A 回答 了 B 的 提问 xxx 'action' => RESPOND, 'user_id' => A的ID, 'object_id' => 问题ID, 'object_user' => B的ID, 'object_type' => QUESTION, 'ext' => [ 'answer_id' => '答案ID', ], </code>
http://www.oschina.net/question/12_70587 这个比较符合我的问题意思
最终我参考开源中国做了调整,以完成我们的需求:
动态的结构:
{ user_id:13, action: 行为, object_id: 对象ID, object_type: 对象类型, object_user_id: 对象用户ID, parent_object_id: 对象父级ID, parent_object_type: 对象父级类型, parent_object_user_id: 对象父级用户ID, reply_id: 回复ID, // action为回复时有用 parent_reply_id: 回复的父级回复ID, // action为回复时有用,回复了别人对评论的回复 text: '转发或者分享时附加文字', view_count: 0, created_at: 创建时间, deleted_at: 删除时间, }
说明:
1. object_*
只存储主要模块内容信息,不含评论;
2. parent_object_*
存储有嵌套关系的对象,比如当object_*
为答案时,parent_object_*
为问题;
3. reply_id
用于直接回复评论时用到;
4. parent_reply_id
父回复ID;
5. 两个回复ID,使用情况是:当回复了别人的回复时,根据comment_id
拉取评论与全部回复,在模板显示时只显示对话的两个回复。
场景列表:
一级结构:
- 安正超 发布 了 文章
<br>'action' => NEW, 'user_id' => 安正超ID, 'object_id' => 文章ID, 'object_user_id' => 安正超ID, 'object_type' => ARTICLE,
- 安正超 上传 了 N张 图片
<br>'action' => NEW, 'user_id' => 安正超ID, 'object_id' => 图片ID(数组,以逗号隔开), 'object_user_id' => 安正超ID, 'object_type' => PICTURE,
- 安正超 提了 问题 xxxx
<br>'action' => NEW, 'user_id' => 安正超ID, 'object_id' => 问题ID, 'object_user_id' => 安正超ID, 'object_type' => QUESTION
二级结构:
- 安正超 评论 了 文章 xxxx(回答了通用)
<br>展示: 文章: xxxxx 评论:xxxxx (李林评论的)
<br>'action' => COMMENT, 'user_id' => 安正超ID, 'object_id' => 评论ID, 'object_type' => COMMENT, 'object_user_id' => 安正超ID 'parent_object_id' => 文章ID, 'parent_object_user_id' => 作者ID 'parent_object_type' => ARTICLE,
三级结构:
- 安正超 在 文章 中 回复 了 李林 的 评论
<br>展示: 文章: xxxxx 评论:xxxxx (李林评论的) 回复:xxxx (安正超)
'action' => REPLY, 'user_id' => 安正超ID, 'object_id' => 评论ID, 'object_type' => COMMENT, 'object_user_id' => 李林ID 'parent_object_id' => 文章ID, 'parent_object_user_id' => 作者ID 'parent_object_type' => ARTICLE, 'reply_id' => 安正超的回复ID
四级结构:
- 安正超 回复了 李文凯 在 问题 “xxxx” 中 李林 的答案 下的 评论
说明:问题信息从答案接口取回
<br>展示: 问题: xxxxx 答案1... 答案2... 答案3...(李林回答的) 评论:xxxxx (李文凯评论的) 回复:xxxx (安正超)
<br>'action' => RESPOND, 'user_id' => 安正超ID, 'object_id' => 评论ID, 'object_type' => COMMENT, 'object_user_id' => 李文凯的ID 'parent_object_id' => 答案ID, 'parent_object_type' => ANSWER, 'parent_object_user_id' => 李林ID 'reply_id' => 安正超的回复ID
- 安正超 回复了 李文凯 在 问题 “xxxx” 中 李林 的答案 下的 回复
说明:问题信息从答案接口取回
<br>展示: 问题: xxxxx 答案1... 答案2... 答案3...(李林回答的) 评论:xxxxx (A评论的) 李文凯 回复 A:xxxx 安正超 回复 李文凯:xxxx
<br>'action' => RESPOND, 'user_id' => 安正超ID, 'object_id' => 评论ID, 'object_type' => COMMENT, 'object_user_id' => A的ID 'parent_object_id' => 答案ID, 'parent_object_type' => QUESTION, 'parent_object_user_id' => 李林ID, // 以下两个回复只在模板中用到用以决定显示哪两个回复,因为根据comment_id带着回复会全部拉回来 'parent_reply_id' => 李文凯的回复ID, 'reply_id' => 安正超的回复ID,
欢迎各位指正!
这是你想要的 http://blog.csdn.net/java2king/article/details/6010250
怎样获取不同模块内容信息,你这个查询这张表,只能查出id,并没有具体的内容

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.
