English 中文(简体)
What is a good db schema and method to query a "Facebook Wall" feature
原标题:

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.
最佳回答

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.

问题回答
  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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签