Quick row count

When COUNT(*) was too slow to get total rows from a billion row heap …

SELECT partition_number p#, MAX(rows) rows 
FROM sys.partitions 
WHERE OBJECT_NAME(object_id) = 'TableName'
GROUP BY partition_number;

or the fancy pants version …

SELECT partition_number p#, MAX(REPLACE(CONVERT(VARCHAR(20), CONVERT(MONEY, rows), 1), '.00', '')) rows
FROM sys.partitions 
WHERE OBJECT_NAME(object_id) = 'TableName'
GROUP BY partition_number;

Original code from here …

https://www.i-programmer.info/programming/database/6576-sql-server-quickly-get-row-counts-for-tables-heaps-indexes-and-partitions.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s