什么是查询“Facebook Wall”的良好数据库模式和方法?特征

发布于 2024-08-12 11:43:22 字数 594 浏览 2 评论 0原文

我正在编写一个简单的“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。

我的问题

  1. 对于此功能来说,这是一个好的架构吗?如果没有,您会使用什么模式?
  2. 如果它很好,我怎样才能执行一个查询,该查询将按最近发布的顺序返回所有墙上的帖子,同时将孩子与父母分组,以便当我迭代查询结果时,父母和孩子都聚集在一起。
  3. 还是做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

  1. Is this a good schema for this feature? If not, what schema would you use?
  2. 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.
  3. Or is it better to do 2 queries? One query for all the parents, 1 query for the children.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁绊已千年 2024-08-19 11:43:22

首先。
我认为您不需要两个字段来达到相同的目的。在这种情况下,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.

你怎么这么可爱啊 2024-08-19 11:43:22
  1. 不知道/可能。丢失 is_parent 列。
  2. 我最近通过查询按 post_id 升序排序的所有帖子来做到这一点。然后使用一些 PHP 将其缓存在 2D 数组中(array['parrent'] -> array['post']...[])。
  3. 应该用#2中描述的方法来做
  1. No idea/probably. Lose the is_parent column.
  2. I recently did this by querying for all posts sorted by the ascending order of post_id. Then cached it in a 2D array with some PHP (array['parrent'] -> array['post']...[]).
  3. One should do it in method described in #2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文