Tuesday, 29 July 2008

Fast row count on large tables

This statement uses Dynamic Management Views to get the row count of a table. It can be considerably faster when we want to get the row count of a very large table, since the conventional way "select count(*) from table" would require a full table scan.

   1:  SELECT
   2:  Total_Rows= SUM(st.row_count)
   3:  FROM
   4:  sys.dm_db_partition_stats st
   5:  WHERE
   6:  object_name(object_id) = 'table_name_here'

No comments: