Search This Blog

Friday, June 17, 2011

Alternate way of RowCount using sys.partitions

Usually we use COUNT(1) to get the number of rows in a table which is time consuming in case of large tables.

Here is the alternate method to get the Row Count using system view in efficient manner:

SELECT OBJECT_NAME(OBJECT_ID), Rows FROM sys.partitions
WHERE INDEX_ID IN (0,1) -- 0 = Heap;1 = Clustered
AND OBJECT_ID = OBJECT_ID('TableName')

To know more about sys.partitions