Tuesday, April 22, 2014

SQL Count of Rows and a FAST Count, that returns immediately even on large database tables.

In SQL you can find out how many rows are in a table by issuing the command:

Select count(1) from TableName

This however can take quite a while on a large table, especially if the Calculate Statistics has not been done in a while.

You may have noticed that this command has Count(1) and not Count(*).  This way the Count command does not have to go to the database disc file to retrieve the information.  But even with this method, the Count(1) command can take up to a few minutes to return.

One way to return the number of rows in a table with a very good level of accuracy, almost immediatly is to use this command:

SELECT rowcnt FROM sysindexes WHERE object_name(id) = 'TableName' AND indid IN (9,1)

Give this a try and find another excuse to go get a cup of coffee.

(c) 2014 RosSQL, all rights reserved.  This publication may not be republished without the expressed written permission of RosSQL.

No comments:

Post a Comment