SQL Query execution sequence in WHERE clause
来源:互联网 发布:知乎增加关注话题 编辑:程序博客网 时间:2024/06/05 17:22
http://stackoverflow.com/questions/8686289/sql-query-execution-sequence-in-where-clause
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.
3 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.
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.
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.
- SQL Query execution sequence in WHERE clause
- Unknown column 'rownum' in 'where clause'解决方案
- Column 'id' in where clause is ambiguous
- Column 'id' in where clause is ambiguous
- Unknown column '安妮宝贝' in 'where clause'
- Column 'id' in where clause is ambiguous
- Unknown column '??????' in 'where clause'问题
- Unknown column 'xxx' in 'where clause'错误
- Unknown column 'ORGANIZATION_BANK' in 'where clause'
- 中文乱码 unknown column in 'where clause'
- Column 'Email' in where clause is ambiguous
- Column 'id' in where clause is ambiguous
- Column 'is_valid' in where clause is ambiguous
- Column 'id' in where clause is ambiguous
- Column ' ' in where clause is ambiguous
- column 'typeid' in where clause is ambiguous
- aggregates not allowed in where clause
- java.sql.SQLException:Unknown column 'admin' in 'where clause'错误问题
- poco,c++库学习,日期和时间
- PHP面试题及答案
- how to learn unit test mock framework
- # 对视频播放器的研究
- UML---九种图总结
- SQL Query execution sequence in WHERE clause
- Java常量池理解与总结
- coreText
- Android批量插入数据到SQLite数据库
- oracle 中几种排序方法
- K最近邻(kNN,k-NearestNeighbor)
- (编程题)5-20 打印九九口诀表
- 解析 Android 事件传递机制
- # Unity 自学与进阶必会目录