I use SQL COUNT function to get the total number or rows from a table. Is there any difference between the two following statements?
SELECT COUNT(*) FROM Table
and
SELECT COUNT(TableId) FROM Table
Also, is there any difference in terms of performance and execution time?
Thilo nailed the difference precisely…
COUNT( column_name )can return a lower number thanCOUNT( * )ifcolumn_namecan beNULL.However, if I can take a slightly different angle at answering your question, since you seem to be focusing on performance.
First, note that issuing
SELECT COUNT(*) FROM table;will potentially block writers, and it will also be blocked by other readers/writers unless you have altered the isolation level (knee-jerk tends to beWITH (NOLOCK)but I’m seeing a promising number of people finally starting to believe in RCSI). Which means that while you’re reading the data to get your “accurate” count, all these DML requests are piling up, and when you’ve finally released all of your locks, the floodgates open, a bunch of insert/update/delete activity happens, and there goes your “accurate” count.If you need an absolutely transactionally consistent and accurate row count (even if it is only valid for the number of milliseconds it takes to return the number to you), then
SELECT COUNT( * )is your only choice.On the other hand, if you are trying to get a 99.9% accurate ballpark, you are much better off with a query like this:
(The
SUMis there to account for partitioned tables – if you are not using table partitioning, you can leave it out.)This DMV maintains accurate row counts for tables with the exception of rows that are currently participating in transactions – and those very transactions are the ones that will make your
SELECT COUNTquery wait (and ultimately make it inaccurate before you have time to read it). But otherwise this will lead to a much quicker answer than the query you propose, and no less accurate than usingWITH (NOLOCK).