sybase的Transact-SQL用户指南是这样说的: count 得出表达式中非空值的数量,而 count(*) 得出表中的总行数。
select count(LAAY) from K_ZS.. B_MS
select count(
distinct LAAY) from K_ZS.. B_MS
select count(*) from K_ZS..B_MS
上面的三条
sql在2.106开发库上执行结果如下:
----------- 
      61940 
----------- 
        353           
----------- 
      72127 
    以前老以为对同一个查询条件,count(fieldname)和count(*)的结果是一样的……所以首先要对count函数的定义清楚,
从我们一般使用count函数的角度,这里最好就是用count(*) 或者count(主键),或者是一个肯定不为null的列,不然结果就可能少了。
另外从
性能上,在K_MS.. B_MS上做实验,
数据为1472435条(其中BH为主键):
引用:
select count(*) from K_MS.. B_MS 
  Execution Time 2. 
  SQL Server cpu time: 200 ms.  SQL Server elapsed time: 283 ms.
  select count(BH) from K_MS.. B_MS  
  Execution Time 2. 
  SQL Server cpu time: 200 ms.  SQL Server elapsed time: 233 ms.
 
如果用表中的其他非主键字段(其中LAAY、JBFY为
索引字段,SPCX普通字段),比如
引用:
select count(LAAY) from K_MS.. B_MS 
  Execution Time 4. 
  SQL Server cpu time: 400 ms.  SQL Server elapsed time: 326 ms.
  select count(SPCX) from K_MS.. B_MS 
  Execution Time 194. 
  SQL Server cpu time: 19400 ms.  SQL Server elapsed time: 28373 ms. 
  select count(JBFY) from K_MS.. B_MS 
  Execution Time 16. 
  SQL Server cpu time: 1600 ms.  SQL Server elapsed time: 2436 ms.
 
多次执行以上sql可以得出初步结论:count(主键)最快,count(*)次之,并且两者性能差异不大;但是如果count其他
任何非主键字段,则速度一般会比较慢,尤其是哪些非索引字段。
 
	posted on 2008-08-30 14:51 
岁月如歌 阅读(827) 
评论(1)  编辑  收藏  所属分类: 
db