Jump to content

WHERE - Order of conditions


Brian W

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
https://forums.phpfreaks.com/topic/232344-where-order-of-conditions/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

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