我如何存储用户的“帖子”?这样我就可以有效地从数据库中获取它们,然后处理它们,以便在请求特定用户页面时按时间顺序显示它们?
我是否将所有用户的所有帖子存储在一个表中,如下所示:
Post ID | Poster ID | Post Content | Posted to this Users Wall | Post Timestamp然后,当用户打开UserFoo的页面时,我只得到Posted to this Users Wall = UserFoo的所有行
上面的示例不会让桌子变得笨重吗?
发布于 2013-06-21 21:18:17
你提出的布局看起来很合理。5列(4个INT和1个TEXT),根本不是一个“庞大”的表。
如果您有适当的索引,那么查询WHERE "Posted to this Users Wall" = "UserFoo"实际上是即时的。
对于您的目标查询(按时间顺序显示发送到当前用户墙上的帖子),最好的索引可能是(Posted to this Users Wall, Post Timestamp) (一个两列索引)。
发布于 2013-06-21 21:17:54
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
}https://stackoverflow.com/questions/17236123
复制相似问题