关于表数据行统计的问题和相关误区

数据库及其版本: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 次。

 

--本以为聚集索引扫描会走非叶子节点即可,结果却不是

--所以还是统计非空的非聚集索引快点

 

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