Parshii
Parshii

Reputation: 58

Counting the number of rows in sql server 2008

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

Answers (2)

Vulcronos
Vulcronos

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

juergen d
juergen d

Reputation: 204904

Using count(*) or count(some_column) is the fastest way to check for a tables record count.

Upvotes: 1

Related Questions