Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining page count on each SQL table without using DBCC

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.

like image 321
Cole Mietzner Avatar asked Oct 12 '12 23:10

Cole Mietzner


People also ask

Can you list the ways to get the count of records in a table?

With the help of the SQL count statement, you can get the number of records stored in a table.

How do I count counts in SQL query?

In SQL, you can make a database query and use the COUNT function to get the number of rows for a particular group in the table. Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; COUNT(column_name) will not include NULL values as part of the count.


1 Answers

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;
like image 185
Sebastian Meine Avatar answered Nov 11 '22 16:11

Sebastian Meine