I see in my JBoss log several lines like
Hibernate: select count as col_0_0_ from ....
I'm not sure how it's handled in mysql, oracle or other databases, but in mssql, indexes are stored in different datapages.
When counting rows, if we use select count (primarykey), we scan a much lower number of pages than we currently do with select
Example : if one row has a 4 bytes long primary key, and one row stores 1000 bytes,
if the table has 20000 rows, sql will store this table in 2500 datapages (one page stores 8096 bytes).
a select will scan the 2500 datapages, when a select (primarykey) will scan 1 (one) datapage.
As you can see, the impact on database server can be important for large tables with a lot of rows.