Tag Archives: count

Row Count on Large Tables Fast

There are ~550 million records in a table I am trying to get a row count on.  Typically I have always used the “COUNT(*)” method to get a row count. I was curious if there is a faster way.  I had read in some forums that “COUNT(1)” or “COUNT(Primary_Key)” could be theoretically faster but there was some argument on the topic.  I decided to run execution plans on all three to see how the query engine is handling it.  According to the execution plan all three operations are treated the same.

row count execution plan

I decided to then prove they are treated the same by running each one three times and taking the average execution time.  Although the results show small differences , this only caused by fluctuations on server load from other processes.   If I took enough measurements they would  converge.

COUNT(*) = 15.0 seconds
COUNT(1) = 16.3 seconds
COUNT(PK) = 18.0 seconds

Change in Direction

For my purposes I am trying to quickly obtain a count for all tables and databases on a server.  This a large production data warehouse with many databases and hundreds of tables.  I want to minimize the impact to the server and in this case that is more important to me than accuracy.  An alternative method to reading the table directly is to get the statistics from sys.dm_db_partition_stats. This is a Dynamic Management View (DMV) that reads the information about the partition, and by summing it up for a given object you can obtain the row count.  This row count is an estimation.  Any transactions that are occurring would not be captured.

dm_db_partition_stats execution plan

Althought the execution plan looks more complicated, it does not have to read a large table and instead only reads 1 or a handful of records via the system DMV.


To stress the point; this is a fast but not necessarily accurate method to get row count.  If you are doing some validation, such as comparing tables, then I recommend using count() and possible checksum().

More Information About dm_db_partition_stats: https://msdn.microsoft.com/en-us/library/ms187737.aspx