久久久久久久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 中刪除行)
主站蜘蛛池模板: 亚洲欧美另类在线观看 | 成人福利片| 91资源在线观看 | 亚洲精品一区av在线播放 | 欧美成人第一页 | 欧美成人精品一区二区男人看 | 99国产在线| 欧美一区二区三区日韩 | 国产欧美一区二区三区另类精品 | 亚洲一区二区三区四区五区午夜 | 羞羞网站在线免费观看 | 综合久| 91大神xh98xh系列全部 | 国产精品污污视频 | 96av麻豆蜜桃一区二区 | 一区二区三区亚洲 | 亚洲国产成人精品久久久国产成人一区 | 精产国产伦理一二三区 | 久久网站免费视频 | 中文字幕在线免费观看 | 96av麻豆蜜桃一区二区 | 综合中文字幕 | 久久精品一 | 亚洲午夜精品一区二区三区他趣 | 精品国产99 | 国产日韩av一区二区 | 4hu最新网址| 青青草在线视频免费观看 | 干一干操一操 | 久久av网 | 国产精品九九九 | 黄视频网站在线 | 精品国产一区一区二区三亚瑟 | 亚洲欧洲精品一区 | 国产精品久久久久久久久久久久久 | 久草.com| 亚洲欧美综合精品另类天天更新 | 国产精品精品视频一区二区三区 | 亚洲成人精品免费 | www国产成人免费观看视频,深夜成人网 | 成人免费在线观看 |