Link to home
Start Free TrialLog in
Avatar of jl1884
jl1884

asked on

Performance question about select count(*) vs select count(x_id)

Is it worth it to go and change queries that were written:
select count(*) from x_table
to
select count(x_id) from x_table

if x_id is the primary key.  I'm thinking that it's worth it because the pk will be indexed, making the count faster...but i'm not sure if it's really worth the time.  

Any advice?
I have to be compatible with Oracle and Sql Server 2000...if that makes a difference...
Avatar of kaboommm
kaboommm

it does not matter, you can use either.
the difference is:
count(*) will include all the rows.
count(x_id) will not include the row with a null x_id.
In fact, I believe, count(*) is converted to count(primary key) when parsing
I looked up some books, there is nothing that says count(*) is converted to count(primary key) ... my teacher said once it does..., but it says that count(primary key)will work a bit faster

hope this helps
ASKER CERTIFIED SOLUTION
Avatar of jpkemp
jpkemp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jl1884

ASKER

Great. I was just curious! :)  Thanks.  I'll leave the queries as the are!