Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Server Support Windows Phone App releasedListing all your SQL Server databases ordered by size »
    comments

    Sometimes you want to quickly see what tables have the most rows in your database. This is especially true if you inherited a new database and you want to know some stats about this database. Instead of doing a count(*) against every table, I usually just use the sp_spaceused stored procedure. this will run many times faster, usually it is instantaneous.

    Just be aware of these remarks from Books On Line

    When you drop or rebuild large indexes, or drop or truncate large tables, the Database Engine defers the actual page deallocations, and their associated locks, until after the transaction commits. Deferred drop operations do not release allocated space immediately. Therefore, the values returned by sp_spaceused immediately after dropping or truncating a large object may not reflect the actual disk space available.

    You could use updateusage but I don't bother, I just want to get a rough count

    Here is the query that will give you that

    1. CREATE TABLE #temp (name varchar(100), rows int,reserved varchar(100), data varchar(100),index_size  varchar(100),unused  varchar(100))
    2.  
    3. CREATE TABLE #loop (id int identity, name varchar(1000))
    4. INSERT #loop
    5. SELECT SCHEMA_NAME(schema_id) +'.' + name
    6. FROM sys.tables
    7. WHERE type = 'U'
    8.  
    9.  
    10. SET NOCOUNT ON
    11. DECLARE @LoopId int
    12. DECLARE @MaxID int
    13. DECLARE @cmd varchar(1100)
    14. DECLARE @TableName varchar(1000)
    15.  
    16.  
    17. SELECT @LoopId= 1
    18. SELECT @MaxID = max(id) from #loop
    19.  
    20. WHILE @LoopId <= @MaxID
    21. BEGIN
    22.     SELECT @cmd = 'insert #temp exec sp_spaceused '''
    23.     SELECT @TableName = name from #loop where id = @LoopId
    24.     SELECT @cmd = @cmd + @TableName + ''''
    25.  
    26.     EXEC( @cmd )
    27.  
    28.  
    29.     SET @LoopId = @LoopId + 1
    30. END
    31.  
    32.  
    33. SELECT TOP 10 name, rows
    34. FROM #temp
    35. ORDER BY rows DESC
    36.  
    37. DROP TABLE #temp, #loop

    This post is part of the informational section of SQLCop. I have written the SQL in this way because I want SQLCop to be able to run this against a SQL Server 2000 instance

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    613 views
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)