Estimating Row Counts in SQL Server Without COUNT(*)

Working with large datasets in SQL Server often brings a simple question: “How many rows are in this table?”

The intuitive solution — running SELECT COUNT(*) FROM my_table — might work fine on small tables. But on large tables with millions (or billions) of records, this query can become painfully slow and resource-intensive, especially if you’re querying a production environment.

Luckily, there’s a much more efficient alternative: sp_spaceused.


🔍 Why COUNT(*) Can Hurt Performance

Let’s quickly understand why COUNT(*) is costly:

This is where approximate counting becomes essential.


⚡ The Power of sp_spaceused

The built-in stored procedure sp_spaceused gives you a fast, metadata-based estimate of row count — without scanning the entire table.

```sql EXEC sp_spaceused ‘pcmi.ClaimDetails’;

This command is for MSSQL but there is equilavent types for other database Technologies too.