Jump to content

Should duplicate values be removed from mysql IN clause?


NotionCommotion
Go to solution Solved by requinix,

Recommended Posts

I had been using the following script, but question whether I should bother as MySQL will not return duplicates.  Is it better to just send MySQL everything, and let it deal with it?

$input=[5,3,null,7,5,3,2];
if($input=array_values(array_filter(array_unique($input)))){  //Remove non-unique values, null values, and reorder
    $list=rtrim(str_repeat('?,',count($input)),',');
    $sql="SELECT a,b,c FROM my_table WHERE id IN ($list)"; //my_table.id has a NOT NULL constraint
    $stmt = $this->db->prepare($sql);
    $stmt->execute($input);
}
Link to comment
Share on other sites

  • Solution

If the list has only constant values then it will be sorted and MySQL will do a binary search (->). That's one additional comparison every time you double the size of the list.

 

While not a big difference, I'd remove them: one pass in PHP to remove vs. however-many additional comparisons in MySQL.

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.