Get Tables Operational & Physical Statistics

Here is the small query that displays each table’s statistics on the operations INSERT, UPDATE and DELETE of current Database. Additionally, you can use this as these operations have led, in terms of growth of rows of data held by the pages and fragmentation.

SELECT
    t.name AS [TableName],
    fi.page_count AS [Pages],
    fi.record_count AS [Rows],
    CAST(fi.avg_record_size_in_bytes AS INT) AS [AverageRecordBytes],
    CAST(fi.avg_fragmentation_in_percent AS INT) AS [AverageFragmentationPercent],
    SUM(iop.leaf_insert_count) AS [Inserts],
    SUM(iop.leaf_delete_count) AS [Deletes],
    SUM(iop.leaf_update_count) AS [Updates],
    SUM(iop.row_lock_count) AS [RowLocks],
    SUM(iop.page_lock_count) AS [PageLocks]
FROM
    sys.dm_db_index_operational_stats(DB_ID(), NULL, NULL, NULL) AS iop
    JOIN sys.indexes AS i
        ON ( ( iop.index_id = i.index_id )
             AND ( iop.object_id = i.object_id )
           )
    JOIN sys.tables AS t
        ON i.object_id = t.object_id
           AND i.type_desc IN ( 'CLUSTERED', 'HEAP' )
    JOIN sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'SAMPLED') AS fi
        ON fi.object_id = CAST(t.object_id AS INT)
           AND fi.index_id = CAST(i.index_id AS INT)
GROUP BY
    t.name,
    fi.page_count,
    fi.record_count,
    fi.avg_record_size_in_bytes,
    fi.avg_fragmentation_in_percent
ORDER BY
    [TableName]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: