Quantcast
Channel: SQL Steve » SQL Server 2008
Viewing all articles
Browse latest Browse all 20

How to: Identify the object stored at a given page location–SQL Server

$
0
0

Often times when troubleshooting performance issues with SQL Server, you will identify a file id and a page id that is the source of your troubles.  The question is: How do I translate this File Id / Page Id combination into an actual object within SQL Server?  Well, with the following script of course:

How to match a File Id / Page Id to a SQL Server object

use tempdb
GO
 
–Set the parameters
DECLARE @FileId INT = 1
DECLARE @PageId INT = 122
 
–Create a temporary table to hold the DBCC PAGE output
DECLARE @PageTable TABLE
(
    ParentObject NVARCHAR(500),
    [Object]     NVARCHAR(500),
    Field        NVARCHAR(500),
    VALUE        NVARCHAR(500)
)
 
–Create the DBCC PAGE command
DECLARE @CommandString NVARCHAR(1000)
SET @CommandString = ‘DBCC PAGE(”’ + DB_NAME() + ”’, ‘ +
                      CONVERT(VARCHAR, @FileId) + ‘, ‘ +
                      CONVERT(VARCHAR, @PageId) +
                      ‘, 0) WITH TABLERESULTS, NO_INFOMSGS’
 
–Execute the DBCC PAGE command into the #PageTable table
INSERT @PageTable EXEC (@CommandString)
 
–Extract the schema and object name based on the object_id
SELECT
    sys.schemas.name + ‘.’ + sys.objects.name AS ObjectName
FROM
    @PageTable
    INNER JOIN
    sys.objects
    ON
    CONVERT(INT, VALUE) = sys.objects.object_id
    INNER JOIN
    sys.schemas
    ON
    sys.objects.schema_id = sys.schemas.schema_id
WHERE
    ParentObject = ‘PAGE HEADER:’
    AND
    Field = ‘Metadata: ObjectId’
GO

In short, you must set the database context (I’m using tempdb in this example)

  1. Set the database context (I am using tempdb in this example).
  2. Specify the file id and the page id.
  3. Create a temporary table to hold the contents of the specified page.
  4. Execute DBCC PAGE to get the contents of the page.
  5. Extract the ObjectId from the page contents and join to sys.objects.

That’s it!  So next time you track your performance issue down to a specific file id and page id you won’t be scratching your head wondering what it all means.


Filed under: SQL Server, SQL Server 2005, SQL Server 2008, SQL Server 2012, SQL Server 2014, T-SQL Tagged: DBCC PAGE, FileId, PageId, SQL Server

Viewing all articles
Browse latest Browse all 20

Trending Articles