久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL Server 2008 中的 COUNT (DISTINCT column_name) 與 COUNT

COUNT (DISTINCT column_name) Discrepancy vs. COUNT (column_name) in SQL Server 2008?(SQL Server 2008 中的 COUNT (DISTINCT column_name) 與 COUNT (column_name) 的差異?)
本文介紹了SQL Server 2008 中的 COUNT (DISTINCT column_name) 與 COUNT (column_name) 的差異?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我遇到了一個讓我發(fā)瘋的問題.運行下面的查詢時,我得到的計數(shù)為 233,769

I'm running into a problem that's driving me nuts. When running the query below, I get a count of 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

但是如果我運行相同的查詢沒有不同的關(guān)鍵字,我會得到233,748

But if I run the same query without the distinct keyword, I get a count of 233,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

為了測試,我重新創(chuàng)建了所有表并將它們放入臨時表中,然后再次運行查詢:

To test, I recreated all the tables and place them into temp tables and ran the queries again:

  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

并且沒有不同的關(guān)鍵字

  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 into #temp...) 重新創(chuàng)建了臨時表

On a side note, I recreated the temp tables by simply running (select * from Member_List_Link into #temp...)

現(xiàn)在,當我使用這些臨時表檢查 COUNT(column) 與 COUNT(distinct column) 之間的差異時,我什么也沒看到!

And now when I check to see the difference between COUNT(column) vs. COUNT(distinct column) with these temp tables, I don't see any!

那么為什么與原始表格存在差異?

So why is there a discrepancy with the original tables?

我運行的是 SQL Server 2008(開發(fā)版).

I'm running SQL Server 2008 (Dev Edition).

更新 - 包括統(tǒng)計資料

UPDATE - Including statistics profile

PhysicalOp 列僅用于第一個查詢(無不同)

PhysicalOp column only for the first query (without distinct)

NULL
Compute Scalar
Stream Aggregate
Clustered Index Seek

PhysicalOp 列僅用于第一個查詢(具有不同的)

PhysicalOp column only for the first query (with distinct)

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

第一個查詢的行數(shù)和執(zhí)行數(shù)(沒有不同的)

Rows and Executes for the 1st query (without distinct)

1   1
0   0
1   1
1   1

第二個查詢的行數(shù)和執(zhí)行數(shù)(具有不同的)

Rows and Executes for the 2nd query (with distinct)

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) 添加到第二個查詢(具有不同的)

Adding OPTION(MAXDOP 1) to the 2nd query (with distinct)

Rows Executes

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

以及由此產(chǎn)生的 PhysicalOp

And the resulting PhysicalOp

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

推薦答案

FROM http://msdn.microsoft.com/en-us/library/ms187373.aspxNOLOCK 相當于 READUNCOMMITTED.有關(guān)詳細信息,請參閱本主題后面的 READUNCOMMITTED.

FROM http://msdn.microsoft.com/en-us/library/ms187373.aspx NOLOCK Is equivalent to READUNCOMMITTED. For more information, see READUNCOMMITTED later in this topic.

READUNCOMMITED 將讀取行兩次,如果它們是事務的主題 - 因為在事務處理過程中,前滾行和回滾行都存在于數(shù)據(jù)庫中.

READUNCOMMITED will read rows twice if they are the subject of a transation- since both the roll foward and roll back rows exist within the database when the transaction is IN process.

默認情況下所有查詢都是讀提交的,不包括未提交的行

By default all queries are read committed which excludes uncommitted rows

當您插入臨時表時,選擇只會給您提交的行 - 我相信這涵蓋了您試圖解釋的所有癥狀

When you insert into a temp table the select will give you only committed rows - I believe this covers all the symptoms you are trying to explain

這篇關(guān)于SQL Server 2008 中的 COUNT (DISTINCT column_name) 與 COUNT (column_name) 的差異?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

SQL trigger on Truncate(截斷時的 SQL 觸發(fā)器)
sql search query with multiple optional search parameters(具有多個可選搜索參數(shù)的 sql 搜索查詢)
SQL Efficiency: WHERE IN Subquery vs. JOIN then GROUP(SQL 效率:WHERE IN 子查詢 vs. JOIN 然后 GROUP)
Retrieving XML element name using t-SQL(使用 t-SQL 檢索 XML 元素名稱)
Insert double quotes into SQL output(在 SQL 輸出中插入雙引號)
Delete rows from CTE in SQL SERVER(從 SQL SERVER 中的 CTE 中刪除行)
主站蜘蛛池模板: 成人免费视频 | 久久精品亚洲 | 五月婷亚洲 | 精品久久久久一区二区国产 | 一区二区三区视频在线观看 | 中文字幕欧美一区 | 国产二区视频 | 在线资源视频 | 欧美激情五月 | 国产免费观看一区 | 国产精品精品视频一区二区三区 | 久久久青草 | 在线视频中文字幕 | 亚洲欧美激情国产综合久久久 | 久久久久国产精品一区二区 | 午夜三区 | 亚洲第1页 | 天堂一区二区三区四区 | 午夜精品在线观看 | 久久国产精品99久久久久久丝袜 | 国产精品久久久久久亚洲调教 | 精品在线看 | 91av在线视频观看 | 91视频在线观看 | 伊久在线| 久草新在线 | 在线日韩视频 | 日韩中文欧美 | 一区二区三区国产 | 亚洲欧美日韩在线不卡 | 国产高清久久 | 日日夜夜精品视频 | 日韩视频―中文字幕 | 91精品在线看 | 天堂久久天堂综合色 | 伊人天堂网 | 天天操夜夜骑 | 伊人网91 | 91在线区| 日韩在线电影 | 亚洲成人av在线播放 |