Skip to Main Content
  • Questions
  • which is better count(rowid) , count(1) , count(*)

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Rajendra.

Asked: February 12, 2016 - 9:53 am UTC

Last updated: February 12, 2016 - 10:35 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi Tom,

I am a newbie in oracle and few of my friends are arguing that count(rowid) is faster than count(1) or count(*)

But when i ran the below queries in 2 different sessions

select count(1) from table
and select count(rowid) from table. Both are taking almost the same time

I read one of your posts where you mentioned the below point

"count(rowid) askes for the count of non-null rowids. why bother to check the non-nullness if all you want is a count."

FYR here is the older post which i am reffering
https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:1156151916789

I am sure you are right always but how come rowid be null as it is the physical address. Can you please explain me this, please correct my understanding if i was wrong

Thanks and Regards,
Rajendra Kalepu.

and Chris said...

Although all rows have a rowid, there's no guarantee table.rowid will be non-null in your results.

For example, if you outer join two tables, then you can have results where the rowid is null (for the outer joined table):

create table t1 as
  select rownum x from dual connect by level <= 2;

create table t2 as
  select * from t1 where rownum <= 1;

select count(*), count(t1.rowid) t1rid, count(t2.rowid) t2rid
from   t1 left join t2 
on     t1.x = t2.x;

  COUNT(*)      T1RID      T2RID
---------- ---------- ----------
         2          2          1

So count(rowid) is like count(col) - how many non-null rows are there?

Count(1) is not faster than count(*). Oracle internally changes count(1) -> count(*)!

https://jonathanlewis.wordpress.com/2015/01/06/count-4/

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

rajendra, February 12, 2016 - 1:47 pm UTC

Very well explained