Database Queries
-
Looking for opinions on which method is best.
Lets say I want to query all Posts based on user id, and along with that I need all Comments associated with each Post of the query. I see two solutions (could be more).
#1
I take the result set of Posts query and loop through it, and for each Post I then query the Comments in database based on Post id.foreach($post as $key => $value){ $comments = query database here $post[$key]->comments = $comments; }#2
I query the Posts and query the Comments based on user id. Then loop through Posts and within that loop, loop through the Comments and compare the Comments Post id with the Post id.foreach($post as $key => $value){ foreach($comments as $k => $v){ if($v->comment_id == $value->id){ $post[$key]->comments = $comments[$k]; } } }I looking for the less expensive way for server load/bandwidth. More queries vs 2 larger queries(All Posts & Comments -> User)?
The topic ‘Database Queries’ is closed to new replies.