Jump to content

join vs. multiple queries


Maximus

Recommended Posts

I've only recently begun looking at mysql optimization and performance, and something that I can't find answered anywhere:  is a join/left-join/inner-join better than using multiple SELECT queries to extract information from 2-3 tables?  Especially since I often use multiple queries in conjunction with a while loop in PHP to retrieve info, so there are a lot of select queries happening.

 

And by "better" I am of course talking about performance.

 

Any help is appreciated!

Link to comment
Share on other sites

It would depend on the situation, but if you are probably going to use the information from the other tables, it would be better in my opinion to do the joins.  Then again, the overhead of additional queries can be small compared to MySQL processing JOIN criteria, depending on the query.  It's hard to give a yes or no answer here.  What's the query/table schemas?

Link to comment
Share on other sites

Maybe this will clear it up a bit.  As you can see, I'm performing a sepperate query each time the loop iterates...other times its just a simple SELECT from one table and get an ID and use that to SELECT various things from another table.  So would JOIN be more appropriate for these things, or does it not really matter?

 

$query = mysql_query("SELECT `id`,`stockid`,`quantity` FROM `user_stocks` WHERE `userid`='$_COOKIE[userid]'");

while($g = mysql_fetch_array($query)) {

    $info = mysql_fetch_array(mysql_query("SELECT `name`,`tag`,`value` FROM `stocks` WHERE `id`='$g[stockid]' LIMIT 1"));

    //echo various info here

}

Link to comment
Share on other sites

Maybe this will clear it up a bit.  As you can see, I'm performing a sepperate query each time the loop iterates...other times its just a simple SELECT from one table and get an ID and use that to SELECT various things from another table.  So would JOIN be more appropriate for these things, or does it not really matter?

 

$query = mysql_query("SELECT `id`,`stockid`,`quantity` FROM `user_stocks` WHERE `userid`='$_COOKIE[userid]'");

while($g = mysql_fetch_array($query)) {

    $info = mysql_fetch_array(mysql_query("SELECT `name`,`tag`,`value` FROM `stocks` WHERE `id`='$g[stockid]' LIMIT 1"));

    //echo various info here

}

 

in this case do the join ...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.