SQL Query execution sequence in WHERE clause


http://stackoverflow.com/questions/8686289/sql-query-execution-sequence-in-where-clause








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.

share improve this question
 

3 Answers

up vote 6 down vote accepted

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.

share improve this answer
 
 
Assume that both columns are non-indexed, then what will be executed first.(as this is an example I am showing only 2 conditions to match, but what if there are so many AND conditions)? –  nirav  Dec 31 '11 at 4:46
1 
@nirav.patel In the absence of indexes RDBMS is free to pick whatever order it calculates would give you the best performance. Some RDBMS engines (e.g. Oracle) would calculate statistics on the dispersion of your data to pick the best order. For example, if RDBMS knows that your ID column has 40000 distinct values in a table of 40000 rows, and first name has 3000 distinct values, it would look at the ID first, because it promises a more specific answer. –  dasblinkenlight  Dec 31 '11 at 5:01
 
And how can one find which columns are indexed in pl/sql? –  Java GWT Developer  Apr 14 '16 at 16:15

The database will decide what order to execute the conditions in.

Normally (but not always) it will use an index first where possible.

You can see how conditions in where or joins are needs to be optimized: -SQLStatementExecution -Example Discussion1 -Example Discussion2 -Example Discussion3

Generally speaking, the order of criteria in the WHERE clause is evaluated and optimized by the query optimizer prior to creating an execution plan. This is good. However, I would encourage you to review the query execution plan for each query prior to putting it into production.

share improve this answer
 

SQL-based database engines will generally optimize based on the clustered (the physical order of data records) and any available indexes. MySQL and MS SQL Server (at a minimum - many others are too) are smart enough to know which order to execute filters to optimize a query.

For your purposes, it doesn't matter and the execution results will be the exact same, with the same performance, in either order.

share improve this answer
 
 
But if the AND conditions are more say 15 so in that case what will be the sequence. –  nirav  Dec 31 '11 at 4:45

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值