当前位置: 首页 > 图文教程 > 数据库 > 数据库技巧 > where 子句的执行顺序

数据库技巧
数据库设计经验谈
数据库设计的折衷方法
Access与sql server的语法区别总结
收藏的SQLServer技巧集
多种获取远程连接access数据库的方法
SQL中NTEXT字段内容显示<long text>的原因
SQL Prompt--绝好的SQL语法提示工具
海量数据库的查询优化及分页算法方案 2 之 改良SQL语句
海量数据库的查询优化及分页算法方案集合1/2
海量数据库的查询优化及分页算法方案集合2/2
取随机记录的语句
问个高难度的复杂查询(在一个时间段内的间隔查询)
Sql语句求最小可用id
Sql Server 和 Access 操作数据库结构Sql语句小结
ACCESS转化成SQL2000需要注意的几个问题小结
SQL Server数据库性能优化技术
数据库正规化和设计技巧
如何让你的SQL运行得更快
Select data from an Excel sheet in MSSQL
一个多表查询的SQL语句

数据库技巧 中的 where 子句的执行顺序


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 58 ::
收藏到网摘: n/a

貌似在2005之前的版本中,where子句的顺序是从前往后的。但是又貌似在2005的版本中进行了优化,所有的顺序都被统一成了以过滤能力进行排序的语句。 看代码:
复制代码 代码如下:

set statistics io on
set statistics time on
go
set statistics profile on
go
use pubs
select * from authors
where (au_fname like 'S%' or au_fname like 'a%')
and (state like 'CA' or state like 'UT')
and (cast(zip as int) > 90000)
select * from authors
where (au_fname like 'S%' or au_fname like 'a%')
and (cast(zip as int) > 90000)
and (state like 'CA' or state like 'UT')
select * from authors
where (cast(zip as int) > 90000)
and (au_fname like 'S%' or au_fname like 'a%')
and (state like 'CA' or state like 'UT')

目的:想验证where语句的各个条件的查询顺序
环境:SQLServer 2005Express版本
步骤:显示查询计划
结果:无一例外,都被统一成了这样的代码
复制代码 代码如下:

|--Clustered Index Scan(OBJECT:([pubs].[dbo].[authors].[UPKCL_auidind]), WHERE:(CONVERT(int,[pubs].[dbo].[authors].[zip],0)>(90000) AND ([pubs].[dbo].[authors].[au_fname] like 'S%' OR [pubs].[dbo].[authors].[au_fname] like 'a%') AND ([pubs].[dbo].[authors].[state] like 'CA' OR [pubs].[dbo].[authors].[state] like 'UT')))

结论:貌似在2005之前的版本中,where子句的顺序是从前往后的。但是又貌似在2005的版本中进行了优化,所有的顺序都被统一成了以过滤能力进行排序的语句。