Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

A DMV a Day – Day 12

The DMV for Day 12 is sys.dm_db_partition_stats, which is described by BOL as:

Returns page and row-count information for every partition in the current database.

You can find some interesting information from sys.dm_db_partition_stats, including what is in the query shown today. This particular DMV works with SQL Server 2005, 2008, and 2008 R2. It requires VIEW SERVER STATE permission.

-- Table and row count information   
SELECT OBJECT_NAME(ps.[object_id]) AS [TableName], 
i.name AS [IndexName], SUM(ps.row_count) AS [RowCount]
FROM sys.dm_db_partition_stats AS ps
INNER JOIN sys.indexes AS i 
ON i.[object_id] = ps.[object_id] 
AND i.index_id = ps.index_id 
WHERE i.type_desc IN ('CLUSTERED','HEAP')
AND i.[object_id] > 100
AND OBJECT_SCHEMA_NAME(ps.[object_id]) <> 'sys'
GROUP BY ps.[object_id], i.name
ORDER BY SUM(ps.row_count) DESC;

This query as it is written above, will show you which tables in the current database have the most rows. This is useful information to know, especially if you are considering adding an index to a table, or simply doing index maintenance on a table. Knowing that a table has 500 million rows rather 500 thousand rows might cause you to take a different course of action.

Comments

Posted by Dukagjin Maloku on 12 April 2010

Another simple script with lot of info, thanks Glenn!

Posted by Anonymous on 12 April 2010

Pingback from  Dew Drop &ndash; April 12, 2010 | Alvin Ashcraft&#039;s Morning Dew

Posted by Jason Brimhall on 12 April 2010

I like this script.

Just a minor recommendation, use the ObjectProperty function instead of the i.object_id > 100.  I think it would be more easily understood that you were excluding MS Shipped Objects from the results like that.

-- Table and row count information  

SELECT OBJECT_NAME(ps.[object_id]) AS [TableName],

i.name AS [IndexName], SUM(ps.row_count) AS [RowCount]

FROM sys.dm_db_partition_stats AS ps

INNER JOIN sys.indexes AS i

ON i.[object_id] = ps.[object_id]

AND i.index_id = ps.index_id

WHERE i.type_desc IN ('CLUSTERED','HEAP')

AND OBJECTPROPERTY(i.object_id,'IsMSShipped') = 0

AND OBJECT_SCHEMA_NAME(ps.[object_id]) <> 'sys'

GROUP BY ps.[object_id], i.name

ORDER BY SUM(ps.row_count) DESC;

I am also uncertain that the schema comparison is necessary - would need to test more.

Leave a Comment

Please register or log in to leave a comment.