select count(*)优化 快速得到总记录数

1、select count(*) from table_name

比select count(主键列) from table_name和select count(1) from table_name 要快一些

但是对于千万级来说这还是很慢

2、非聚集索引比聚集索引要快

假如表里只有一个聚集索引,你在建立一个非聚集索引,然后查询时指定使用该非聚集索引,速度将会得到提高

如果聚集索引需要1800毫秒的话 用非聚集索引会降到1300毫秒

 select count(*) from tj_ClientActionIP with (index(IX_tj_ClientActionIP_2)) where dateandtime>='2013-2-2'

3、bit类型的非聚集索引要比其他数据类型的非聚集索引要快

假如你在该表中添加一个列,数据类型是bit ,不可为空,默认为0.然后为其创建非聚集索引,速度也将得到提到。

假如你用第二步的方法 耗时1300毫秒的话,使用BIT类型,会降到800毫秒。

耗时三天,总结出以上方案。立贴记录

原文地址:https://www.cnblogs.com/xdoudou/p/3320939.html