Getting a SQL Server RowCount Without doing a Table Scan

Problem

Sometimes there is a need to get record counts from every table in your database. One way of doing this is to do a SELECT count(*) on all of your tables, but this could create a lot of overhead especially for large databases and large tables. If you don’t require an exact answer, it isn’t necessary to use a SELECT count(*) query on the rows in a table to get the row count.

Solution

Thanks to Andrew Novick at Novick Software here is the answer. SQL Server keeps the row count in sysindexes and it can be retrieved there. The key is to select the correct record from sysindexes. Sysindexes is a system table that exists in every database.

SQL Server maintains at least one row in sysindexes for every user table. A few of the most important columns are:

ColumnData TypeDescription
idintID of the table referred to by this row
indidintSee the text that follows…
rowcntbigintNumber of rows in the index

The indid column tells us what part of the table structure this row of sysindexes is referring to:

indid valueDescription
0Table data when there is no clustered index
1Refers to the clustered index
2 – 254Non-clustered indexes
255Text or Image data pages

A table will only have an entry in sysindexes with an indid value of for 0 or 1, never both. That’s the entry that we’re interested in because its rowcnt field gives us the number of rows in the table. Here is a query that shows the table, index and indid from the pubs database:

USE pubs
GO
SELECT so.[name] as [table name]
     , CASE WHEN si.indid between 1 and 254
            THEN si.[name] ELSE NULL END 
            AS [Index Name]
     , si.indid 
     FROM sysindexes si
          INNER JOIN sysobjects so
             ON si.id = so.id
     WHERE si.indid < 2 
       AND so.type = 'U' -- Only User Tables
       AND so.[name] != 'dtproperties'
     ORDER BY so.[name]

Here are the results:

table nameIndex Nameindid
authorsUPKCL_auidind1
discountsNULL0
employeeemployee_ind1
jobsPK__jobs__117F9D941
pub_infoUPKCL_pubinfo1
publishersUPKCL_pubind1
royschedNULL0
salesUPKCL_sales1
storesUPK_storeid1
titleauthorUPKCL_taind1
titlesUPKCL_titleidind1

As you can see from the results, most of the indexes are clustered (indid=1) but a few tables such as discounts don’t have a clustered index (indid=0).

I started this tip with “If you don’t need an exact answer…” That’s because there are times when rowcnt is not the exact number of records in the table.

This can be corrected by updating statistics on the table with:

dbcc updateusage
go

Here’s the CREATE FUNCTION script for udf_Tbl_RowCOUNT

CREATE FUNCTION dbo.udf_Tbl_RowCOUNT (
        @sTableName sysname  -- Table to retrieve Row Count
        )
    RETURNS INT -- Row count of the table, NULL if not found.
/*
* Returns the row count for a table by examining sysindexes.
* This function must be run in the same database as the table.
*
* Common Usage:   
SELECT dbo.udf_Tbl_RowCOUNT ('')
* Test   
 PRINT 'Test 1 Bad table ' + CASE WHEN SELECT 
       dbo.udf_Tbl_RowCOUNT ('foobar') is NULL
        THEN 'Worked' ELSE 'Error' END
        
* Copyright 2002 Andrew Novick http://www.NovickSoftware.com
* You may use this function in any of your SQL Server databases
* including databases that you sell, so long as they contain 
* other unrelated database objects. You may not publish this 
* UDF either in print or electronically.
***************************************************************/
AS BEGIN
    
    DECLARE @nRowCount INT -- the rows
    DECLARE @nObjectID int -- Object ID
    SET @nObjectID = OBJECT_ID(@sTableName)
    -- Object might not be found
    IF @nObjectID is null RETURN NULL
    SELECT TOP 1 @nRowCount = rows 
        FROM sysindexes 
        WHERE id = @nObjectID AND indid < 2
    RETURN @nRowCount
END 
GO
GRANT  EXECUTE  ON [dbo].[udf_Tbl_RowCOUNT]  TO PUBLIC
GO

Let’s use it:

use pubs -- assuming the UDF was created in pubs
go
SELECT [name]
     , dbo.udf_Tbl_RowCOUNT ([name]) as [Row Count]
    FROM sysobjects
    WHERE type='U' and name != 'dtproperties'
    ORDER BY [name]
GO

Here are the results:

nameRow Count
authors24
discounts3
employee43
jobs14
pub_info8
publishers8
roysched86
sales21
stores6
titleauthor25
titles18

Next Steps

  • Add this User Defined Function to your database toolkit
  • Continue to learn more about the system tables and how they can help you manage your SQL Servers
  • Check out Novick Software for some other great UDFs and SQL Server Tips

Leave a Reply

Your email address will not be published. Required fields are marked *