–创建测试表–drop table tb_CountTestcreate table tb_CountTest([uniqueidentifier] [uniqueidentifier] not null,[bigint] [bigint] not null,[tinyint] [tinyint] not null,[int] [int] not null,[int0] [int] null)go–uniqueidentifier(16 字节),bigint(8 字节),int(4 字节),smallint(2 字节),tinyint(1 字节)–插入1000行测试数据insert into tb_CountTest([uniqueidentifier],[bigint],[tinyint],[int],[int0])select NEWID(),number*3-1,number*2%256,number,case when number%6=0 then null else number endfrom (select distinct number from master.dbo.spt_values where number between 1 and 2000)tabgo–创建聚集索引 ([uniqueidentifier])–drop index ix_tb_CountTest_uniqueidentifier on tb_CountTestcreate clustered index ix_tb_CountTest_uniqueidentifier on tb_CountTest([uniqueidentifier])go–创建非聚集索引 ([int])–drop index ix_tb_CountTest_int on tb_CountTestcreate index ix_tb_CountTest_int on tb_CountTest([int])go
–执行以下语句,查看执行计划.结果如下:select count(*) from dbo.tb_counttestselect count(1) from dbo.tb_counttestselect count([uniqueidentifier]) from dbo.tb_counttestselect count([bigint]) from dbo.tb_counttestselect count([tinyint]) from dbo.tb_counttestselect count([int]) from dbo.tb_counttest
select count([int0]) from dbo.tb_counttest
两个问题:
Q1.为什么都是使用非聚集索引扫描?
A1. 为什么都是使用非聚集索引扫描?
因为使用非聚集索引返回的数据页更少。使用使用的都是索引,下面可以搜索到,按索引查询时,返回的数据页有多少。
如:
3604,-1)
1)
1,590,3)
1,959,3)
当我们使用【set statistics
2 IAM
–现在再创建另一个索引:–创建非聚集索引([tinyint])–drop index ix_tb_CountTest_tinyint on tb_CountTestcreate index ix_tb_CountTest_tinyint on tb_CountTest([tinyint])go
附:
COUNT 与COUNT_BIG函数类似。两个函数唯一的差别是它们的返回值。COUNT
总结:
1. Count统计,,只要该列不含空值,统计性能都是一样的,系统默认选择最优索引。
2. 如果表中有更小的字段做索引,统计将使用它并更快统计。
参考:(个人与楼主分析有出入)
属于自己的不要放弃,已经失去的留作回忆。