關於表數據行統計的問題和相關誤區

數據庫及其版本:SQL Server2008 原以爲針對聚集索引列會快點,結果卻是錯誤的。

 

--一個300多萬記錄的表,無任何索引,進行表掃描

select COUNT(*) from testcount

--'testcount'。掃描計數3,邏輯讀取89181 次,物理讀取1001 次,預讀87602 次,lob 邏輯讀取0 次,lob 物理讀取0 次,lob 預讀0 次。

 

select COUNT(ProductID) from testcount

--'testcount'。掃描計數3,邏輯讀取89181 次,物理讀取915 次,預讀82713 次,lob 邏輯讀取0 次,lob 物理讀取0 次,lob 預讀0 次。

 

create index testcount_pk on testcount(clusterPK)

select COUNT(*) from testcount

--count(*)count(索引列)是差不多的,會自動選擇非聚集索引掃描

select COUNT(ProductID) from testcount

--'testcount'。掃描計數3,邏輯讀取9135 次,物理讀取0 次,預讀14 次,lob 邏輯讀取0 次,lob 物理讀取0 次,lob 預讀0 次。

 

--創建聚集索引後,聚集索引掃描和全表掃描差不多

drop index testcount.testcount_pk

create clustered index testcount_pk on testcount(clusterPK)

select COUNT(*) from testcount

--'testcount'。掃描計數3,邏輯讀取84541 次,物理讀取1287 次,預讀84311 次,lob 邏輯讀取0 次,lob 物理讀取0 次,lob 預讀0 次。

select COUNT(ProductID) from testcount

--'testcount'。掃描計數3,邏輯讀取84541 次,物理讀取1647 次,預讀84098 次,lob 邏輯讀取0 次,lob 物理讀取0 次,lob 預讀0 次。

 

--本以爲聚集索引掃描會走非葉子節點即可,結果卻不是

--所以還是統計非空的非聚集索引快點

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章