Jump to content

WHERE - Order of conditions


Brian W
Go to solution Solved by Maq,

Recommended Posts

I know that LIKE conditions are slower than a strait equals conditions. In the case that I'm trying to find a particular user's information and I know their first name, I could do something like:

 

SELECT * FROM users WHERE name LIKE '$name%';

 

And if I knew their state too:

SELECT * FROM users WHERE state_id = '$state_id' AND name LIKE '$name%';

 

The Question is:

I would expect that because the state condition comes first, MySQL would only run the LIKE condition on the dataset that matched the state which should increase the speed. Am I correct?

Link to comment
Share on other sites

  • Solution
I would expect that because the state condition comes first, MySQL would only run the LIKE condition on the dataset that matched the state which should increase the speed. Am I correct?

From what I understand, yes, you are correct.  To be sure, run it a few times with/out the WHERE clause and do a time comparison.

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.