Blog Post

Physical Row Location

,

SQL Server 2008 has presented us a couple of options to aid in becoming better DBA’s.  You can see this evidenced in many ways in the product.  A couple of the things that make me think this is the case boils down to two functions that are new in SQL 2008.  I learned about these while trying to learn how to do something else.  It just so happens that these functions could possibly help me in the other process (I’ll write more about that later when I have finished it).

These new functions are: sys.fn_PhysLocFormatter and sys.fn_PhysLocCracker.  The two functions are really very similar.  The first of the two does as the name implies and formats the physical location, while the second of the two provides a table output of the location.  If you look at the sp_helptext of both, you can see that they only have minor differences.

[codesyntax lang=”tsql” title=”physlocformat”]

-------------------------------------------------------------------------------  
-- Name: sys.fn_PhysLocFormatter   
--  
-- Description:  
-- Formats the output of %%physloc%% virtual column  
--  
-- Notes:  
-------------------------------------------------------------------------------  
create function sys.fn_PhysLocFormatter (@physical_locator binary (8))  
returns varchar (128)  
as  
begin  
 declare @page_id binary (4)  
 declare @file_id binary (2)  
 declare @slot_id binary (2)  
 -- Page ID is the first four bytes, then 2 bytes of page ID, then 2 bytes of slot  
 --  
 select @page_id = convert (binary (4), reverse (substring (@physical_locator, 1, 4)))  
 select @file_id = convert (binary (2), reverse (substring (@physical_locator, 5, 2)))  
 select @slot_id = convert (binary (2), reverse (substring (@physical_locator, 7, 2)))  
 return '(' + cast (cast (@file_id as int) as varchar) + ':' +   
     cast (cast (@page_id as int) as varchar) + ':' +   
     cast (cast (@slot_id as int) as varchar) + ')'  
end

[/codesyntax]

and

[codesyntax lang=”tsql” title=”physloccracker”]

-------------------------------------------------------------------------------  
-- Name: sys.fn_PhysLocCracker   
--  
-- Description:  
-- Cracks the output of %%physloc%% virtual column  
--  
-- Notes:  
-------------------------------------------------------------------------------  
create function sys.fn_PhysLocCracker (@physical_locator binary (8))  
returns @dumploc_table table  
(  
 [file_id] int not null,  
 [page_id] int not null,  
 [slot_id] int not null  
)  
as  
begin  
 declare @page_id binary (4)  
 declare @file_id binary (2)  
 declare @slot_id binary (2)  
 -- Page ID is the first four bytes, then 2 bytes of page ID, then 2 bytes of slot  
 --  
 select @page_id = convert (binary (4), reverse (substring (@physical_locator, 1, 4)))  
 select @file_id = convert (binary (2), reverse (substring (@physical_locator, 5, 2)))  
 select @slot_id = convert (binary (2), reverse (substring (@physical_locator, 7, 2)))  
 insert into @dumploc_table values (@file_id, @page_id, @slot_id)  
 return  
end

[/codesyntax]

When you look at these two functions, you can easily say that they are similar right up until the end where they diverge in functionality.  The first casts the data into the “formatted” version, while the cracker simply outputs to a table.

Use of these functions is also quite easy.

[codesyntax lang=”tsql” title=”usage”]

select top (10) 
yt.*,
pl.file_id, pl.page_id, pl.slot_id
from yourtable as yt
cross apply sys.fn_PhysLocCracker(%%physloc%%) as pl;
SELECT top 10
sys.fn_PhysLocFormatter (%%physloc%%) AS [Physical RID], yt.* 
FROM yourtable yt;

[/codesyntax]

These functions can prove to be very helpful in your troubleshooting or dives into Internals.  Check them out and enjoy.

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating