Reputation: 58
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.
Upvotes: 0
Views: 3069
Reputation: 3456
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.
Upvotes: 0
Reputation: 204904
Using count(*)
or count(some_column)
is the fastest way to check for a tables record count.
Upvotes: 1