如何存储用户的"Posts"?这样我就可以有效地从数据库中获取它们,然后处理它们,以便在请求特定用户页面时按时间顺序显示它们?
我是否像这样将所有用户的所有帖子存储在一个表中:
Post ID | Poster ID | Post Content | Posted to this Users Wall | Post Timestamp
然后当用户打开UserFoo的页面时,我只是得到所有行Posted to this Users Wall = UserFoo
?
上面的样品不会使一个桌子变得笨重吗?
你提出的布局看起来很合理。5列(4列INT
和1列TEXT
),一点也不"笨重"。
如果您有适当的索引,查询WHERE "Posted to this Users Wall" = "UserFoo"
实际上是即时的。
对于您的目标查询(按时间顺序显示发送到当前用户墙的帖子),最好的索引可能是(Posted to this Users Wall, Post Timestamp)
(一个两列索引)。
users
id | name |
posts
| id | u_id | content |
wall
| id | u_id | post_id |
u_id from posts
is users。id里面是Author
u_id from wall
is 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
}