August 11, 2010 at 8:28 am
Which one is fast to get count of records?
1)select count(*) from order
2)select count(OrderID) from order (OrderID is primary key)
I tested in 3894000 records table but both them are done in 0 second
August 11, 2010 at 8:34 am
adonetok (8/11/2010)
Which one is fast to get count of records?1)select count(*) from order
2)select count(OrderID) from order (OrderID is primary key)
I tested in 3894000 records table but both them are done in 0 second
They'd have the same execution plans I'd imagine, so the same performance in this particular case.
August 11, 2010 at 8:57 am
http://sqlinthewild.co.za/index.php/2009/04/14/on-counts/
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 11, 2010 at 11:37 am
CELKO (8/11/2010)
The table row count (NOT records! quit thinking this is a mag tape file system) is available at the table level, so three is no need to scan the table.
SQL will scan something to get a row count (from SELECT COUNT(*) or SELECT Count(<not nullable column>)). Not the table, unless there are no nonclustered indexes, but it will scan an index to do the row count, the one with the fewest leaf pages.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply