7

I am trying to compress the largest tables in my database. I will do this by running the SP_ForEachDB stored procedure. However I cannot figure out how to view the total page count. I can get the row count with this query...

USE DEVELOP04_HiltonUS

GO

SELECT 
    [TableName] = so.name, 
    [RowCount] = MAX(si.rows) 
FROM 
    sysobjects so, 
    sysindexes si 
WHERE 
    so.xtype = 'U' 
    AND 
    si.id = OBJECT_ID(so.name) 
GROUP BY 
    so.name 
ORDER BY 
    2 DESC

Which returns:

            TABLE NAME   ROW COUNT
           PlannedShift  38268660
        BudgetStaffStat  19353104
          BudgetKBIStat  14142631
EmployeeShiftAdjustment  13493745
            Requirement  11020921
     EmployeeShiftError  6857235
      JobclassLaborData  5638692

and so on for all my tables.

I am looking for the same thing but returning page Count instead.

Breeze
  • 2,010
  • 2
  • 32
  • 43
Cole Mietzner
  • 303
  • 1
  • 5
  • 13

1 Answers1

14
SELECT  OBJECT_SCHEMA_NAME(s.object_id) schema_name,
        OBJECT_NAME(s.object_id) table_name,
        SUM(s.used_page_count) used_pages,
        SUM(s.reserved_page_count) reserved_pages
FROM    sys.dm_db_partition_stats s
JOIN    sys.tables t
        ON s.object_id = t.object_id
GROUP BY s.object_id
ORDER BY schema_name,
        table_name;
Sebastian Meine
  • 11,260
  • 29
  • 41