table size and database size

  • Hi,

    i use this script that show me the size of each table and do the sum of all the table size.

    SELECT

    X.[name],

    REPLACE(CONVERT(varchar, CONVERT(money, X.[rows]), 1), '.00', '') AS [rows],

    REPLACE(CONVERT(varchar, CONVERT(money, X.[reserved]), 1), '.00', '') AS [reserved],

    REPLACE(CONVERT(varchar, CONVERT(money, X.[data]), 1), '.00', '') AS [data],

    REPLACE(CONVERT(varchar, CONVERT(money, X.[index_size]), 1), '.00', '') AS [index_size],

    REPLACE(CONVERT(varchar, CONVERT(money, X.[unused]), 1), '.00', '') AS [unused]

    FROM

    (SELECT

    CAST(object_name(id) AS varchar(50)) AS [name],

    SUM(CASE WHEN indid < 2 THEN CONVERT(bigint, [rows]) END) AS [rows],

    SUM(CONVERT(bigint, reserved)) * 8 AS reserved,

    SUM(CONVERT(bigint, dpages)) * 8 AS data,

    SUM(CONVERT(bigint, used) - CONVERT(bigint, dpages)) * 8 AS index_size,

    SUM(CONVERT(bigint, reserved) - CONVERT(bigint, used)) * 8 AS unused

    FROM sysindexes WITH (NOLOCK)

    WHERE sysindexes.indid IN (0, 1, 255)

    AND sysindexes.id > 100

    AND object_name(sysindexes.id) <> 'dtproperties'

    GROUP BY sysindexes.id WITH ROLLUP) AS X

    ORDER BY X.[name]

    the problem is that the sum of all tables is not the same size when i make a full database backup.

    example of this is when i run this query against my database i see a sum of 111,899 KB that they are 111MB,but when

    i do full backup to that database the size of this full backup is 1.5GB,why is that and where this size come from?

    THX

  • I'm not sure this script captures everything. don't forget you have a fillfactor, so pages are not completely full. I haven't checked your calculations, but that seems like a very big difference to me as well. I'm guessing either the math is wrong, you are extremely heavily fragmented, or you are missing something.

  • How big is your log file?

  • sys.indexes is NOT maintained accurately. Try running dbcc updateusage first. Also, check out the new DMVs in SQL 2005 (such as sys.dm_db_index_physical_stats) and some of the partition DMVs which could provide better data.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply