我遇到一个让我疯狂的问题.
当运行下面的查询时,我得到一个233,769的计数
当运行下面的查询时,我得到一个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