Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

FASTCOUNT() User-Defined Function (SQL 2000 only)

By Cade Bryant,

SQL Server's COUNT() function is a slow and expensive way to count a table's rows because it scans the entire table or index.  FASTCOUNT(), which I created, is much more efficient for this purpose - its only drawback being that it may return inaccurate results if a nonlogged (e.g., bulk) data modification operation was recently performed against the table.

Syntax: SELECT dbo.FASTCOUNT('')

Because this is a function, you can use it "inline" in any SQL statement or script, just as you would use COUNT(); however, there are some syntax differences.

Unlike COUNT(), FASTCOUNT() takes a table name as its argument (rather than a column name or wildcard), instead of requiring you to suply the table name in a FROM clause.  The advantage of this is that you can pass a variable as table name without resorting to dynamic SQL.  In other words, the following command is legal:

DECLARE @Tbl sysname
SET @Tbl = 'MyTable'
SELECT dbo.FASTCOUNT(@Tbl)

(If you were to accomplish the same results with COUNT(), you would need to use dynamic SQL to pass the table name into a FROM clause).

Total article views: 830 | Views in the last 30 days: 2
 
Related Articles
FORUM
FORUM
FORUM

COUNT

COUNT Title and not IDs

FORUM

Count

Counting Articles for in Incovoice

FORUM

file count

file count

Tags
miscellaneous    
t-sql    
 
Contribute