Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query execution sequence in WHERE clause

Tags:

sql

What is the excution order in Query like:

SELECT * FROM [users] WHERE [userid] = 50001 AND [username] = 'new user'

My question is what will be matched for first - [userid] or [username].

and so will affect the execution time.

Any suggesetion to improve this query will be appriciated.

like image 594
nirav Avatar asked Dec 31 '11 04:12

nirav


1 Answers

The answer depends on the indexes that you make available to the SQL engine. If userid is indexed but username is not, the engine is likely to start with userid; if username is indexed but userid is not, then the engine would probably start with the name; if both fields are indexed, the engine will search that index, and look up the rows by internal ids. Note that all of this is highly dependent on the RDBMS that you are using. Some engines would forego index searches altogether in favor of full table scans when the number of rows is low.

like image 52
Sergey Kalinichenko Avatar answered Sep 28 '22 02:09

Sergey Kalinichenko