Estimating Rows per Page

February 10, 2009 by Michelle Ufford
Filed under: Syndication, T-SQL Scripts 

Ever wonder how many rows you store per page? Me too. So here’s the query I use to investigate this:

SELECT OBJECT_NAME(i.OBJECT_ID) AS 'tableName'
    , i.name AS 'indexName'
    , i.type_desc
    , MAX(p.partition_number) AS 'partitions'
    , SUM(p.ROWS) AS 'rows'
    , SUM(au.data_pages) AS 'dataPages'
    , SUM(p.ROWS) / SUM(au.data_pages) AS 'rowsPerPage'
FROM sys.indexes AS i
Join sys.partitions AS p
    ON i.OBJECT_ID = p.OBJECT_ID
    And i.index_id = p.index_id
Join sys.allocation_units AS au
    ON p.hobt_id = au.container_id
WHERE OBJECT_NAME(i.OBJECT_ID) Not Like 'sys%'
    And au.type_desc = 'IN_ROW_DATA'
GROUP BY OBJECT_NAME(i.OBJECT_ID)
    , i.name
    , i.type_desc
HAVING SUM(au.data_pages) > 100
ORDER BY rowsPerPage;

What does this tell you? Well, the more rows you can fit on a page, the less IO you need to consume to retrieve those rows. It’s also a good way to improve your buffer cache hit ratio (i.e. retrieve data from memory instead of disk, which is more efficient). So take a good look at those first few rows… do you have a small number of [rowsPerPage] but a large number of [rows]? If so, it may be time to look at re-designing your tables.

Happy Coding!

Michelle Ufford (aka SQLFool)

Source: http://sqlfool.com/2009/02/estimating-rows-per-page/

Comments

2 Comments on Estimating Rows per Page

  1. Michael Swart on Wed, 11th Feb 2009 9:15 am
  2. If you’re joining on sys.allocation_units and a partition has lob data, then the rows are being counted twice, but only data pages for in_row_data is being counted. This can throw off the rowsPerPages.

    To fix it you could add the filter
    au.type_desc = ‘IN_ROW_DATA’

    or better, use a dmv:
    SELECT OBJECT_NAME(i.OBJECT_ID) AS ‘tableName’
    , i.name AS ‘indexName’
    , i.type_desc
    , MAX(p.partition_number) AS ‘partitions’
    , SUM(p.row_count) AS ‘rows’
    , SUM(p.in_row_data_page_count) AS ‘dataPages’
    , SUM(p.row_count) / SUM(p.in_row_data_page_count) AS ‘rowsPerPage’
    FROM sys.indexes AS i
    JOIN sys.dm_db_partition_stats AS p
    ON i.OBJECT_ID = p.OBJECT_ID
    And i.index_id = p.index_id
    WHERE OBJECT_NAME(i.OBJECT_ID) Not Like ’sys%’
    GROUP BY OBJECT_NAME(i.OBJECT_ID)
    , i.name
    , i.type_desc
    HAVING SUM(p.in_row_data_page_count) > 100
    ORDER BY rowsPerPage;

    And for *further* understanding of the structure of a database, I like to use SQL Internals Viewer by Danny Gould: http://www.sqlinternalsviewer.com It tells you where everything is down to the very last bit.

  3. Michelle Ufford on Wed, 11th Feb 2009 10:19 am
  4. Thanks, Michael! I appreciate the comments. I’ve updated my script to include type_desc = ‘IN_ROW_DATA’, and I just downloaded SQL Internals Viewer. It looks very interesting. :)

Tell me what you're thinking...
and oh, if you've enjoyed the post, please consider subscribing to my blog.