Facebook-Timeline系统应如何在数据库中查看?

时间:2013-06-21 13:11:56

标签: php mysql database-design

我如何存储用户“帖子”?这样我就可以有效地从数据库中获取它们,然后在请求特定用户页面时按时间顺序显示它们?

我是否将所有用户的所有帖子存储在一个表中,如下所示:

Post ID | Poster ID | Post Content | Posted to this Users Wall | Post Timestamp

然后,当用户打开UserFoo的页面时,我只获得Posted to this Users Wall = UserFoo所有的行?

上面的样本不会使表格变得笨重吗?

2 个答案:

答案 0 :(得分:2)

users

id | name |

posts

|  id   |   u_id    |   content    |

wall

| id   |   u_id    |  post_id    |

来自posts的u_id是users.id,其中是作者

来自wall的u_id是users.id,它是目标(在哪个墙上发布)

你可以更明确地命名它,即poster_id,target_id


另一种方法是

post

| id | poster_id |

wall

| id | post_id  | target_id |

content

|  post_id | content |

您还可以添加其他特定内容,例如帖子是评论或其他内容,在另一个表格中,还是post表格中的列


function getUsersWallPosts($target_id) {
    $query = "SELECT c.content FROM content AS c, INNER JOIN wall AS w ON w.post_id = c.post_id WHERE w.target_id = $target_id";
    $result = someUserDefinedFunctionForQueryAndFetch($query);
    return $result
}

答案 1 :(得分:2)

您建议的布局看起来合理。 5列(四个INT和一个TEXT),并不是一个“笨重”的表格。

如果您有正确的索引,则查询WHERE "Posted to this Users Wall" = "UserFoo"几乎是即时的。

对于您的目标查询(按时间顺序显示发送到当前用户墙的帖子),最佳索引可能位于(Posted to this Users Wall, Post Timestamp)(两列索引)。