COUNT(DISTINCT column_name)SQL Server 2008中的差异与COUNT(column_name)的差异?

前端之家收集整理的这篇文章主要介绍了COUNT(DISTINCT column_name)SQL Server 2008中的差异与COUNT(column_name)的差异?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到一个让我疯狂的问题.
当运行下面的查询时,我得到一个233,769的计数
SELECT COUNT(distinct  Member_List_Link.UserID)  
 FROM Member_List_Link  with (nolock)   
 INNER JOIN MasterMembers with (nolock)  
     ON Member_List_Link.UserID = MasterMembers.UserID   
  WHERE MasterMembers.Active = 1 And
        Member_List_Link.GroupID = 5 AND 
        MasterMembers.ValidUsers = 1 AND 
        Member_List_Link.Status = 1

但是如果我运行相同的查询而没有distinct关键字,748的计数

SELECT COUNT(Member_List_Link.UserID)  
 FROM Member_List_Link  with (nolock)   
 INNER JOIN MasterMembers with (nolock)
   ON Member_List_Link.UserID = MasterMembers.UserID   
 WHERE MasterMembers.Active = 1 And Member_List_Link.GroupID = 5 
  AND MasterMembers.ValidUsers = 1 AND Member_List_Link.Status = 1

要测试,我重新创建了所有的表,并将它们放入临时表并再次运行查询

SELECT COUNT(distinct  #Temp_Member_List_Link.UserID)  
  FROM #Temp_Member_List_Link  with (nolock)   
  INNER JOIN #Temp_MasterMembers with (nolock)
    ON #Temp_Member_List_Link.UserID = #Temp_MasterMembers.UserID   
  WHERE #Temp_MasterMembers.Active = 1 And 
        #Temp_Member_List_Link.GroupID = 5 AND 
        #Temp_MasterMembers.ValidUsers = 1 AND 
        #Temp_Member_List_Link.Status = 1

而没有distinct关键字

SELECT COUNT(#Temp_Member_List_Link.UserID)  
  FROM #Temp_Member_List_Link  with (nolock)   
  INNER JOIN #Temp_MasterMembers with (nolock)
    ON #Temp_Member_List_Link.UserID = #Temp_MasterMembers.UserID   
  WHERE #Temp_MasterMembers.Active = 1 And 
        #Temp_Member_List_Link.GroupID = 5 AND 
        #Temp_MasterMembers.ValidUsers = 1 AND 
        #Temp_Member_List_Link.Status = 1

在旁注,我通过简单地运行(select * from Member_List_Link到#temp …)重新创建了临时表

现在,当我检查这些temp表之间的COUNT(列)与COUNT(不同的列)之间的差异时,我看不到任何的!

那么为什么原始表格有差异?

我正在运行sql Server 2008(开发版).

更新 – 包括统计资料

PhysicalOp列仅用于第一个查询(不区分)

NULL
Compute Scalar
Stream Aggregate
Clustered Index Seek

PhysicalOp列仅用于第一个查询(具有不同)

NULL
Compute Scalar
Stream Aggregate
Parallelism
Stream Aggregate
Hash Match
Hash Match
Bitmap
Parallelism
Index Seek
Parallelism
Clustered Index Scan

第一个查询的行和执行(不区分)

1   1
0   0
1   1
1   1

第二个查询的行和执行(不同)

Rows    Executes
1   1
0   0
1   1
16  1
16  16
233767  16
233767  16
281901  16
281901  16
281901  16
234787  16
234787  16

将OPTION(MAXDOP 1)添加到第二个查询(具有不同的)

Rows Executes

1           1
0           0
1           1
233767          1
233767          1
281901          1
548396          1

并产生PhysicalOp

NULL
Compute Scalar
Stream Aggregate
Hash Match
Hash Match
Index Seek
Clustered Index Scan

解决方法

http://msdn.microsoft.com/en-us/library/ms187373.aspx
NOLOCK相当于READUNCOMMITTED.有关更多信息,请参阅本主题后面的READUNCOMMITTED.

READUNCOMMITED将读取行两次,如果它们是转换的主题 – 因为当事务处于IN进程时,滚动后退和回滚行均存在于数据库中.

默认情况下,所有查询都被读取,不包括未提交的行

当您插入临时表时,选择将仅给出已提交的行 – 我相信这将涵盖您尝试解释的所有症状

原文链接:https://www.f2er.com/mssql/81204.html

猜你在找的MsSQL相关文章