--表
Create Table T(id int identity(1,1),A int, B datetime)
select
top 1 字段=A from T
where
A<=2000
order by A desc
1:它用exists 语句怎么写?
2:它的效率和exists效率哪一个高?
...全文
15514打赏收藏
效率问题.
--表 Create Table T(id int identity(1,1),A int, B datetime) select top 1 字段=A from T where A<=2000 order by A desc 1:它用exists 语句怎么写? 2:它的效率和exists效率哪一个高?
--表
Create Table T(id int identity(1,1),A int, B datetime)
insert into t select 1900,getdate()
insert into t select 1900,getdate()
insert into t select 1900,getdate()
insert into t select 2015,getdate()
insert into t select 1998,getdate()
insert into t select 1999,getdate()
insert into t select 2000,getdate()
insert into t select 2001,getdate()
insert into t select 2900,getdate()
select
top 1 字段=A from T
where
A<=2000
order by A desc
select
字段=a.A
from
t a
where
a.A<=2000
and
not exists(select 1 from t where A<=2000 and A>a.A)
如果就单单从,exists 与<=比的话,那一定是,exists效率高了,因为exists是对逻辑进行判断而<=是对每条记录的大小进行比较,效率当然就没有exists高了,楼主有兴趣的话可以看一下有关优化的文章,in,or,<=,>=,这些都是半优化的语句,而exists是全优化语句,还有楼主说的用exists怎么查询,对于楼主的这个问题如果用了,exists也不能提高多少效率因为,这个问题用exists写是这样的,select * from t a where not exists (select * from t where a.id=id and a>2000)这样写后可以说是用了,exists,可是它同时也用了子查询,而这同时也会影响查询的效率所以对于楼主这个问题,楼主用这两种办法都是差不多的