| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 690 人关注过本帖
标题:[求助]关于一个Select语句的两个问题
只看楼主 加入收藏
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
 问题点数:0 回复次数:2 
[求助]关于一个Select语句的两个问题
SELECT *
FROM dbo.SampleInspect si
WHERE EXISTS
(
SELECT 1
FROM dbo.Project p
INNER JOIN dbo.Customer c
ON c.CustomerID = p.CustomerID
INNER JOIN dbo.SampleRevision sr
ON sr.PJ_RowID = p.PJ_RowID
INNER JOIN dbo.SampleReceive src
ON src.SR_RowID = sr.SR_RowID
INNER JOIN dbo.Supplier sp
ON sp.SupplierID = src.SupplierID
WHERE si.SRC_RowID = src.SRC_RowID
AND si.AddDate >= IsNull(@BeginDate, si.AddDate)
AND si.AddDate < DateAdd(day, 1, ISNULL(@EndDate, si.AddDate))
AND (p.SampleID = ISNULL(@SampleID, p.SampleID) OR sr.SampleRevID = ISNULL(@SampleID, sr.SampleRevID))
ANDp.CustomerID = ISNULL(@CustomerID, p.CustomerID)
)


问题一: Exists是不是也可以用IN关键字来代替?
问题二: "SELECT 1"中的"1"在语句中是什么意思?
搜索更多相关主题的帖子: Select 语句 
2007-06-06 22:15
立志成佛
Rank: 1
等 级:新手上路
威 望:2
帖 子:314
专家分:0
注 册:2006-11-1
收藏
得分:0 

问题1:我觉得应该可以代替的,附上转的一篇文章

关于EXISTS与IN的区别:
EXISTS检查是否有结果,判断是否有记录,返回的是一个布尔型(TRUE/FALSE)。
IN是对结果值进行比较,判断一个字段是否存在于几个值的范围中,所以 EXISTS 比 IN 快。

主要区别是:
exists主要用于片面的,有满足一个条件的即可,
in主要用于具体的集合操作,有多少满足条件.

exists是判断是否存在这样的记录,
in是判断某个字段是否在指定的某个范围内。
exists快一些吧 。

in适合内外表都很大的情况,exists适合外表结果集很小的情况。


在ASKTOM的讲解:

Well, the two are processed very very differently.

Select * from T1 where x in ( select y from T2 )

is typically processed as:

select *
from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;

The subquery is evaluated, distinct'ed, indexed (or hashed or sorted) and then
joined to the original table -- typically.


As opposed to

select * from t1 where exists ( select null from t2 where y = x )

That is processed more like:


for x in ( select * from t1 )
loop
if ( exists ( select null from t2 where y = x.x )
then
OUTPUT THE RECORD
end if
end loop

It always results in a full scan of T1 whereas the first query can make use of
an index on T1(x).


So, when is where exists appropriate and in appropriate?

Lets say the result of the subquery
( select y from T2 )

is "huge" and takes a long time. But the table T1 is relatively small and
executing ( select null from t2 where y = x.x ) is very very fast (nice index on
t2(y)). Then the exists will be faster as the time to full scan T1 and do the
index probe into T2 could be less then the time to simply full scan T2 to build
the subquery we need to distinct on.


Lets say the result of the subquery is small -- then IN is typicaly more
appropriate.


If both the subquery and the outer table are huge -- either might work as well
as the other -- depends on the indexes and other factors.


在这种情况下Exists要比In快

因为你的子查询只是一个开关,只需要返回true或者false
不需要返回查询记录

如果你用了in 就会返回符合条件的纪录 事实上是不必要的

[此贴子已经被作者于2007-6-7 10:44:13编辑过]


曾经的曾经已不在
2007-06-07 10:28
立志成佛
Rank: 1
等 级:新手上路
威 望:2
帖 子:314
专家分:0
注 册:2006-11-1
收藏
得分:0 
问题二:

Select 1 From ***
在满足条件情况下返回的都是1
因为外层仅仅要判断时候有返回的记录,所以不在乎返回的东西
Select 2 也行
外层只判断返回了就是True没有返回就是False不在乎你返回的东西的

如果你写上字段也是对的,不过参考网上人一般的看法这样效率会较以前的低,因为需要再次查询字段。

[此贴子已经被作者于2007-6-7 10:37:49编辑过]


曾经的曾经已不在
2007-06-07 10:31
快速回复:[求助]关于一个Select语句的两个问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034834 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved