Jump to content

One big query or a thousand small ones?


Manixat
 Share

Go to solution Solved by awjudd,

Recommended Posts

Hello again freaks,

 

 

I have a dilemma here that I thought I would ask for a professional opinion.

 

 

 

$mysqli->query("SELECT * FROM `table` WHERE `id`='$id1' OR `id`='$id2' OR `id`='$id3' ... OR `id`= '$id3001' OR `id`='$id3002'");

or

$mysqli->query("SELECT * FROM `table` WHERE `id`='$id1'");
$mysqli->query("SELECT * FROM `table` WHERE `id`='$id2'");
$mysqli->query("SELECT * FROM `table` WHERE `id`='$id3'");
...
$mysqli->query("SELECT * FROM `table` WHERE `id`='$id3001'");
$mysqli->query("SELECT * FROM `table` WHERE `id`='$id3002'");

 

which is the better way to go?

Edited by Manixat
Link to comment
Share on other sites

  • Solution

One big query.  It can hit the index once and do it's business.  Since you are doing ORs, you may want to consider switching it to an IN clause.

 

 

SELECT * FROM `table` WHERE `id` IN ( $id1, $id2 )

 

~awjudd

Edited by awjudd
Link to comment
Share on other sites

One big query.  It can hit the index once and do it's business.  Since you are doing ORs, you may want to consider switching it to an IN clause.

 

 

SELECT * FROM `table` WHERE `id` IN ( $id1, $id2 )

 

~awjudd

 

Excellent, thank you for this reply! :)

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.