select * from t a where exists
(select top 1 * from t b where a.id=b.id and a<=2000 order by A desc)这样写是多余的,因为本先一句就可以直接得到的结果,用了两个步骤,效率当然不高了!
如果必须的话,那么exists的效率好象要高于IN吧
(select top 1 * from t b where a.id=b.id and a<=2000 order by A desc)这样写是多余的,因为本先一句就可以直接得到的结果,用了两个步骤,效率当然不高了!
如果必须的话,那么exists的效率好象要高于IN吧
解决方案 »
- 一个关于先安装sql 2000,后安装sql 2005的问题
- 在SqlServer2008(查询)中,如何把同列同值的数据中,添加一个字段表示成第几行?
- 对排序结果进行分页查询
- 求一sql语句?
- 帮我找找错
- 有四个表,没有关联,字段不一样,想在四个表同时搜索同一关键字,得到的结果分页显示,请问SQL该怎么写??
- MySQL MIN MAX方法使用问题
- 批量复制数据后,剩下的列怎么插入
- 请教一个问题?留下唯一的记录!中午不吃饭要等!
- 求解一个sql语句。%%%%%%%%%%%%%%%%%%
- 关于Sql Server的性能问题,求高手释疑,万谢!
- 关于sp_changeobjectowner的问题
a.*
from
t a
where
not exists(select 1 from t where id=a.id and A<=2000 and A>a.A)
a.*
from
t a
where
a.A<=2000
and
not exists(select 1 from t where id=a.id and A<=2000 and A>a.A)
a.*
from
t a
where
a.A<=2000
and
not exists(select 1 from t where A<=2000 and A>a.A)
我覺得中間過程太多也影響效率。
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)drop table T
FROM T
WHERE A<=2000倒排序的第一笔不就是MAX吗,干嘛搞那么复杂?
--子查询才会有 exits 的效率要比 in 的高的问题.
--同意 libin_ftsafe(子陌红尘|潇湘剑公子@dev-club)