SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to obtain quick counts of rows in all tables?

The following Microsoft SQL Server T-SQL queries will yield fast (not real time) row counts in each table in the database:

-- SQL quick table row counts

USE Northwind;

 

SELECT   TableName = o.name,

         Rows = max(i.rows)

FROM     sysobjects o

         INNER JOIN sysindexes i

           ON o.id = i.id

WHERE    xtype = 'u'

         AND OBJECTPROPERTY(o.id,N'IsUserTable') = 1

GROUP BY o.name

ORDER BY Rows DESC

GO

/*  Results

 

TableName               Rows

Order Details           2155

Orders                  830

Customers               91

Products                77

Territories             53

EmployeeTerritories     49

Suppliers               29

Employees               9

Categories              8

Region                  4

Shippers                3

CustomerCustomerDemo    0

CustomerDemographics    0

*/

 

-- SQL quick table row counts - SQL Server 2005, SQL Server 2008

USE AdventureWorks2008;

 

SELECT   TableName = SCHEMA_NAME(schema_id)+'.'+o.name,

         Rows = max(i.rows)

FROM     sys.sysobjects o

         INNER JOIN sys.sysindexes i

           ON o.id = i.id

         INNER JOIN sys.objects oo

           ON o.id = oo.object_id

WHERE    xtype = 'u'

         AND OBJECTPROPERTY(o.id,N'IsUserTable') = 1

GROUP BY schema_id, o.name

ORDER BY Rows DESC

GO

/* Partial results

 

TableName                                 Rows

Sales.SalesOrderDetail                    121317

Production.TransactionHistory             113443

Production.TransactionHistoryArchive      89253

Production.WorkOrder                      72591

Production.WorkOrderRouting               67131

Sales.SalesOrderHeader                    31465

Sales.SalesOrderHeaderSalesReason         27647

Person.BusinessEntity                     20777

Person.EmailAddress                       19972

Person.Password                           19972

Person.Person                             19972

Person.PersonPhone                        19972

Sales.Customer                            19820

*/

 

Related link:

http://www.developmentalmadness.com/archive/2008/04/01/fast-rowcount-for-sql-2005.aspx

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.