TSQL to return (display) all tables row count in database - think about performance

Published 16 July 07 02:05 AM | SQL Master 

In the forums and newsgroups this is a very common question that how to get all the tables rowcount or to get optimum values.

You may be aware using SELECT COUNT(*) statement, but be aware it will make full table scan to return the rowcount and think performance if you are running against a database-table that has millions of rows. Alternative way is to use sysindexes system table, in this case from the last updated statistics and to get updated values you have to use DBCC UPDATEUSAGE statement. Again this will have another overhead running that DBCC statement on a huge volume of tables.

To get immediate response and fine tune the performance for such requirement you can use the statement SELECT rows FROM sysindexes WHERE id = OBJECT_ID('table_name') AND indid < 2 instead of SELECT COUNT(*); you will definetly see an improvement in getting the speed of such queries in several times.

DisplayRowCount:

create procedure [dbo].[DisplayRowCount] as
declare @tabcnt int
declare @printline char (60)
select @tabcnt = count (*) from sysobjects where type = 'U'

If @tabcnt != 0
BEGIN
  select "TABLE NAME"= convert (varchar (50), o.name), ROWS=i.rows
    from sysobjects o, sysindexes i
    where o.type = 'U'
      and o.id = i.id
      and i.indid in (0,1)
  order by o.name
END

select @printline = "(" + convert (varchar(10), @tabcnt) +
" tables in " + DB_NAME() + ")"

print ""
print @printline
GO

Comments

# SSQA - SqlServer-QA.net said on July 16, 2007 2:21 AM:

In the forums and newsgroups this is a very common question that how to get all the tables rowcount or

Anonymous comments are disabled

About SQL Master

**__________________________________** SQL Server MVP, Sr. DBA & industry expert. - Knowledge is of two kinds. We know a subject ourselves or we know where we can find information on it. It is also a power and you will gain by sharing it.

Search

Go

This Blog

«July 2007»
SMTWTFS
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

Syndication