什么是查询“Facebook Wall”的良好数据库模式和方法?特征
我正在编写一个简单的“Facebook Wall”类型功能,用户可以在另一个人的墙上发帖,人们可以简单地回复该墙上的帖子,或发布新的墙上的帖子。用户无法回复回复,您只能回复原始墙贴(就像 facebook 一样)
我最初想到的 mySQL 数据库架构是这样的:
post_id (pk) post_text (char) date_posted (datetime) is_parent (bool) parent_id (id of parent)
它是如何工作的:
如果有人发布新的墙贴,is_parent 将设置为 1,parent_id 将设置为 null。
如果有人回复该帖子,is_parent 将为 0,parent_id 将设置为父帖子的 ID。
我的问题
- 对于此功能来说,这是一个好的架构吗?如果没有,您会使用什么模式?
- 如果它很好,我怎样才能执行一个查询,该查询将按最近发布的顺序返回所有墙上的帖子,同时将孩子与父母分组,以便当我迭代查询结果时,父母和孩子都聚集在一起。
- 还是做2次查询更好? 1 个针对所有父母的查询,1 个针对孩子的查询。
I'm writing a simple "Facebook Wall" type feature where a user can post on another person's wall and people can simply reply to that Wall post, or post a new wall post. Users cannot reply to a reply, you may only reply to the original wall post (just like facebook)
My original mySQL db schema that I had thought of goes like this:
post_id (pk) post_text (char) date_posted (datetime) is_parent (bool) parent_id (id of parent)
How it works:
If someone posts a new wall post, is_parent will be set to 1, and parent_id will be set to null.
If someone posts a reply to that post, is_parent will be 0, and parent_id will be set to the ID of the parent post.
My Questions
- Is this a good schema for this feature? If not, what schema would you use?
- If it is good, how can I do a single query that will return all the wall posts in order of most recently posted, while grouping the children with the parent so that when I iterate over the query result, the parent and children all come together.
- Or is it better to do 2 queries? One query for all the parents, 1 query for the children.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先。
我认为您不需要两个字段来达到相同的目的。在这种情况下,parent_id 就足够了。当你有 post_id 设置自动递增。第一个分配的值将为 1,因此 Parent_id=0 永远不会发生。您可以假设当parent_id=0 时它是第一级帖子。
最好对所有回复使用一个查询。
First of all.
I don't think you need two field for the same purpose. parent_id is enough in this case. When you have post_id set auto-increament. The first assigned value will be 1, so parent_id=0 will never happen. You can assume when parent_id=0 it is a first level post.
It would be better to use one single query for all replies.