Find the number of data pages in a tempdb table.

Benjamin Shaw 141 Reputation points
2021-06-11T10:53:26.757+00:00

Hi,

sys.allocation_units can return the amount of data pages used in a regular table.

Is there a way in which I can find out how many data pages a temp table is using?

Thanks :)

Developer technologies Transact-SQL
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 47,436 Reputation points
    2021-06-11T12:07:54.343+00:00

    There is not really a difference between user and temporary tables, only diff is that SQL Server engine manage temp table
    Therefore you can get temp table size in the same way, just by accessing TempDB:

    SELECT TBL.name AS ObjName 
          ,STAT.row_count AS StatRowCount 
          ,STAT.used_page_count * 8 AS UsedSizeKB 
          ,STAT.reserved_page_count * 8 AS RevervedSizeKB 
    FROM tempdb.sys.partitions AS PART 
         INNER JOIN tempdb.sys.dm_db_partition_stats AS STAT 
             ON PART.partition_id = STAT.partition_id 
                AND PART.partition_number = STAT.partition_number 
         INNER JOIN tempdb.sys.tables AS TBL 
             ON STAT.object_id = TBL.object_id 
    ORDER BY TBL.name;
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.