* oracle COUNT与COUNT(列) 谁快谁慢?

COUNT(*)与COUNT(列) 要看是否使用到索引以及索引列不为空,
不考虑索引的时候count(*) 反而更加快 , 因为优化器列偏移算法的原因 , 越靠后的列越慢,
所以设计应用的时候,经常访问的列应该放在前面
表无索引时,count()的性能
drop table t purge;
table t as* from ;
alter table Tnull;
t set= ;
seton
set1000
seton
表无索引时收获不止sql优化pdf , count(*)的性能更好

* oracle COUNT与COUNT(列) 谁快谁慢?

文章插图
* oracle COUNT与COUNT(列) 谁快谁慢?

文章插图
count(*) from t;
表无索引时,count(列)的性能,这个列如果越靠后,性能越不好
count() from t;
表有列索引时,count(列)的性能很好
count() from t;
——————————————————————————————–
表有索引且索引列非空时 , count(*)的结果与count()查询结果一致收获不止sql优化pdf , 优化器自动把count(*)优化成count(),所以这时候count(*)实质就是count(),如果允许为空,这时候count(*)的结果与count()查询结果可能不一致,优化器改写的前提是保证结果不出错 , 会出错的话 , 优化器绝对不会改写 。(为空时不进行计数) , 这时候count(*)不会优化成count(),查询没有count()快 。
alter table Tnot null;
count(*) from t;–与下面的count()效果一致
表有索引且索引列非空时,count(列)的性能
count() from t;
具体可以看看 《不止sql优化》最后一章节
【* oracle COUNT与COUNT(列) 谁快谁慢?】本文到此结束,希望对大家有所帮助 。