Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

This blog is for SQL SERVER developers. I am trying to publish the things that i face in my development career, so other developers can get help out of this BLOG.
Browse by Tags · View All
Tejas Shah 30
SQL SERVER 2005 11
SQL Server 10
SSIS 8
T-SQL 8
SQL Yoga 7
SSIS Task 7
SQLYoga 5
SQL Developer 5
DBA 5

Archive · View All
October 2012 5
October 2009 5
November 2012 3
December 2009 3
November 2009 3
September 2011 2
August 2010 2
April 2011 2
March 2010 2
September 2012 1

Tejas Shah's Blog

SQL SERVER: Display Row count of all the tables

Aug 23 2010 2:02AM by Tejas Shah   

Recently we have performance issue in SQL SERVER and we decided to move some large tables to different file group, so we can improve SQL SERVER performance, as it uses other disks I/O. Now challenge is to identify large tables from many tables (approx 700 tables).

There are few ways to display row count of all the tables:

  1. Use Procedure: sp_msForEachTable (Simplest way)

  2. Read information from system tables ( approximate count, Preffered)

Let me explain both of the method:

1. Use Procedure: sp_msForEachTable:

This is the simplest way that we can make COUNT(*) to each table to find number of rows from each table. This is undocumented stored procedure,"sp_msForEachTable" gives ability to make a query to all tables.

EXEC sp_msForEachTable 'SELECT ''?'' AS TableName,COUNT(*) AS NoOfRows FROM ?'

Its nothing but just a simple dynamic SQL statement where "?" will be replaced by table name. So query will be like:

SELECT COUNT(*) FROM BuildVersion

This procedure, "sp_msForEachTable", will loop to each table for current database and count rows from each table. So it gives result like:



By this way, we can get row count from all the tables for current database. But question is: How to use this result set? How to find out which table has maximum rows? To find out this, I need to check each table's rows manually and then only I can find out which table has maximum rows. This is time consuming and not the good way to find out table with maximum rows. Let see another method on which we can find it out it easily.

2. Read information from system tables:

By reading system tables, we can also find out rows from each table. We can use following query to find out number of rows from each table.
SELECT
 st.Name AS TableName,
 SUM(  
  CASE 
   WHEN (p.index_id < 2) AND (a.type = 1) THEN p.rows 
   ELSE 0  
  END 
 ) AS NoOfRows
FROM sys.partitions p
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
INNER JOIN sys.tables st ON st.object_id = p.Object_ID
INNER JOIN sys.schemas sch ON sch.schema_id = st.schema_id
GROUP BY st.name

NOTE: This method is used to get an *approximate* count for all tables.

This query solved my purpose. In this query, it will give result in single result set, so we can manipulate it easily.



E.g. List out all tables and sort it out based on the NumberOfRows.
So, my query will be:
SELECT
 st.Name AS TableName,
 SUM(  
  CASE 
   WHEN (p.index_id < 2) AND (a.type = 1) THEN p.rows 
   ELSE 0  
  END 
 ) AS NoOfRows
FROM sys.partitions p
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
INNER JOIN sys.tables st ON st.object_id = p.Object_ID
INNER JOIN sys.schemas sch ON sch.schema_id = st.schema_id
GROUP BY st.name
ORDER BY NoOfRows DESC

Let me know your opinions.


Republished from SQL Yoga [33 clicks].  Read the original version here [32134 clicks].

Tejas Shah
134 · 1% · 364
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

1  Comments  

  • We can, however, store the result of sp_msforeachtable into a temp table and use it later. In this way we can avoid manual work

    e.g.

    -- Create a temp table for storing the information
    CREATE TABLE #temp 
    (            
        [Table Name] VARCHAR(100),            
        [Number Of Rows] INT            
    ) 
    --Execute the query and insert into the temp table
    EXEC sp_msForEachTable 'INSERT INTO #temp SELECT ''?'' AS TableName,COUNT(*) AS NoOfRows FROM ?'
    
    -- Project the result
    SELECT * FROM #temp ORDER BY [Number Of Rows] DESC
    
    -- Drop the temp table once the operation is over
    DROP TABLE #temp
    
    commented on Mar 28 2012 4:55AM
    Niladri Biswas
    7 · 21% · 6710

Your Comment


Sign Up or Login to post a comment.

"SQL SERVER: Display Row count of all the tables" rated 5 out of 5 by 2 readers
SQL SERVER: Display Row count of all the tables , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]