Archive

Archive for the ‘PASS Summit 2013’ Category

Update to Downgrading SQL Server Enterprise to Standard Scripts

October 24, 2013 Leave a comment

It’s hard to believe that I wrote this post almost a year ago. It’s been my most popular post, so I wanted to let people know about a change to one of the scripts. Many thanks to reader Francesco for pointing out an error. The Attach Script uses sp_msforeachdb and in the original script, I failed to wrap the ? wildcard in square brackets. As a result, database names containing dashes (I’m looking at you SharePoint), would not resolve properly. The script has been updated.

Having seen Kevin Kline (b|t) and Aaron Bertrand (b|t) present 10 Query Tuning Techniques Every SQL Programmer Should Know at this year’s PASS Summit, I know I should update the code to use Aaron’s sp_foreachdb. I will put that on the list of things to learn. That is why I love this job, because there is always something new to learn.

Strange Results from sys.fn_physlocformatter

October 17, 2013 2 comments

I’m writing from Charlotte, North Carolina where I’m attending my fifth PASS Summit. I hope to write a series of posts recapping my experiences, but I wanted to write about something I learned in Paul White’s (b|t) pre-conference session, Understanding the Optimizer and Interpreting Execution Plans. The session was amazing and I think I’ll spend the next year going through his scripts.

If you’ve read my blog you know I love internals. Paul introduced me to a function called sys.fn_physlocformatter. In SQL Server 2008 and beyond, %%physloc%% is a virtual column that returns the file, page and slot of each row of a result set in byte-reversed, hex format. That means it is hard to read. If you don’t believe me, here is an example of the query and its result:

USE MYTEST
GO

SELECT %%physloc%% AS [physloc],*
FROM MYTestTable

 

physloc results

 

fn_physlocformatter takes that hex value and formats it into a colon-delimited value that is much more readable. Here’s the query above, replacing %%physloc%% with fn_physlocformatter:

SELECT sys.fn_PhysLocFormatter(%%physloc%%),*
FROM MYTestTable

 

fn_physlocformatter results

 

After Paul showed us the function, I did a search to get more information and came across this post. Using the example provided, I wrote the following query to return a count of the number of rows written to each page:

SELECT SUBSTRING(sys.fn_PhysLocFormatter(%%physloc%%),4,(CHARINDEX(':',sys.fn_PhysLocFormatter(%%physloc%%),4) - 4)) [Page ID], COUNT(*) AS [Row Count]
FROM MYTestTable
GROUP BY SUBSTRING(sys.fn_PhysLocFormatter(%%physloc%%),4,(CHARINDEX(':',sys.fn_PhysLocFormatter(%%physloc%%),4) - 4))
ORDER BY [Row Count]

The sample code from the link above contains code to insert nine rows to MYTestTable2 at a time. I’ve listed the table definition and insert code below.

USE MYTEST
GO

CREATE TABLE MYTestTable2
(MYID INT IDENTITY, 
NAME CHAR(500), 
LNAME CHAR(500))
GO

INSERT INTO MYTestTable2 VALUES ('NAME-1','LNAME-1');
GO
INSERT INTO MYTestTable2 VALUES ('NAME-2','LNAME-2');
GO
INSERT INTO MYTestTable2 VALUES ('NAME-3','LNAME-3');
GO
INSERT INTO MYTestTable2 VALUES ('NAME-4','LNAME-4');
GO
INSERT INTO MYTestTable2 VALUES ('NAME-5','LNAME-5');
GO
INSERT INTO MYTestTable2 VALUES ('NAME-6','LNAME-6');
GO
INSERT INTO MYTestTable2 VALUES ('NAME-7','LNAME-7');
GO
INSERT INTO MYTestTable2 VALUES ('NAME-8','LNAME-8');
GO
INSERT INTO MYTestTable2 VALUES ('NAME-9','LNAME-9');
GO

Because the rows are 1008 bytes long, seven rows will fit on one 8kb data page. After the initial insert of nine rows, I expected to see a page containing seven rows followed by a page with two rows. Here are the results after the initial insert:

 

Initial Insert Results

 

I continued running the nine inserts, to confirm that my code was working as expected. And for the next five runs, it did. The strange results didn’t show up until the seventh run, where I saw this:

 

strange results

 

After the seventh insert, something was causing seven pages to be allocated each with a single row inserted. This didn’t make any sense to me, so I continued investigating. I’ll show you the results of those investigations in my next post, sys.fn_physlocformatter – The Mystery Deepens!