0

I may sound stupid but if I specify multiple WHERE clauses, does it speed up mysql query or slows it down?

Which one would be faster?

Example 1:

SELECT id FROM table WHERE username='user'

Example 2:

SELECT id FROM table WHERE username='user' AND country='US' AND email='[email protected]'
2
  • 1
    Benchmark them and find out Commented Sep 19, 2013 at 17:43
  • This depends a lot on your index coverage. Commented Sep 19, 2013 at 17:44

3 Answers 3

1

Well, it all depends on the column you are searching whether it has an index defined or not.

In the example one, username must have an index to have better performance.

ALTER TABLE tableName INDEX (username)

In example two, the three column must have an index also,

ALTER TABLE tableName INDEX (username, country, email)
Sign up to request clarification or add additional context in comments.

Comments

0

It depends on a few things, like what your table is indexed by, but limiting your result set should almost always be faster than returning all results. As with everything else, benchmark both and decide then.

Comments

0

Usually the second one will be faster, as it would return a subset of first one's resultset.

However, it is possible that MySQL will incorrectly prefer an index access on country or email for the second query over the index access on username or a full table scan, which would slow the query down.

If your are asking "whether the complex queries are slower than simple", then answer would be "parse time would in general be negligible compared to other things a query needs to do"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.