Is there a better way instead of SELECT COUNT(*)
statement to count the number of records in table?
Sometimes we have to count billions of records from temporary tables that are imported using bcp query.
Is there a better way instead of SELECT COUNT(*)
statement to count the number of records in table?
Sometimes we have to count billions of records from temporary tables that are imported using bcp query.
Using count(*)
or count(some_column)
is the fastest way to check for a tables record count.
If you don't need to filter, the following query works well:
SELECT sum(rows) FROM SYS.PARTITIONS WHERE object_id=object_id('MY_TABLE') and index_id in(0,1)
That checkes the number of rows sql server is storing for that object. It can't return any data with the count, and there is no way to include a group by or where.