Given 3 possible values for X (A, B, C), is it faster to do:
WHERE (X = 'B' OR X = 'C'), orWHERE X != 'A'
Or does it depend? If so, then what does it depend on?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Option 1:
and
are equivalent and may use an index on
(X).Option 2:
will not use an index on
(X). See a comment by Henrik Grubbström at the MySQL docs, How MySQL Optimizes WHERE Clauses page:So, if the use of index makes the query faster (for example, if 99% of the table has
X = 'A'), use the first option.Note: The
!=operator is a synonym (in MySQL) of the SQL-standard<>inequality operator.