Select count(*)
Select COUNT(*) from a table usually does a full table scan to return the total row count. The time taken can be considerable for large tables. Instead it is advisable to use the other approach in most cases. You can use sysindexes system table. The ROWS column in the sysindexes table contains the total row count for each table in your database. So, you can use the following select statement instead of SELECT COUNT(*):
SELECT rows FROM sysindexes
WHERE id = OBJECT_ID('table_name')
AND indid < 2
The speed can be improved manifold.
SELECT rows FROM sysindexes
WHERE id = OBJECT_ID('table_name')
AND indid < 2
The speed can be improved manifold.
Thank you!
ReplyDelete