where 子句的执行顺序
程序员文章站
2022-11-21 10:54:52
看代码:复制代码 代码如下:set statistics io on set statistics time on go set statistics profile on...
看代码:
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的版本中进行了优化,所有的顺序都被统一成了以过滤能力进行排序的语句。
复制代码 代码如下:
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的版本中进行了优化,所有的顺序都被统一成了以过滤能力进行排序的语句。