Possible Duplicate:
Count(*) vs Count(1)
If I have a table, 'id' is primary key, then these two commands have different performance ?
select count(*) from t;
select count(id) from t;
thanks
Possible Duplicate:
Count(*) vs Count(1)
If I have a table, 'id' is primary key, then these two commands have different performance ?
select count(*) from t;
select count(id) from t;
thanks
These would have the same performance. In most databases, count() results in a scan of the table or available indexes. Whether or not it uses the index instead of the table depends only on the query optimizer. If the optimizer is smart enough to use the index, it should be smart enough in both cases.
Using available metadata tables, you can often get the number of rows in a table much mroe efficiently than by using a count() query.
I think if id is primary Key both count(*) and count(id) are semantically equivalent.
But for readers count(id)
means the intention to count all rows where id is not null. To avoid confusions I would rather use count(*).