The Curious Case of… how to find which page an index row is on

(The Curious Case of… used to be part of our bi-weekly newsletter but we decided to make it a regular blog post instead so it can sometimes be more frequent. It covers something interesting one of us encountered when working with a client, doing some testing, or were asked in a random question from the community.)

I was working recently on a client’s corruption issue where I needed to know how to find which page an index row is on. The client was getting weird errors from DBCC CHECKDB around missing nonclustered index records, but SELECTs using that index on the supposedly missing key values would work. I suspected a stale read problem from the I/O subsystem as part of building the database snapshot that DBCC CHECKDB uses, but needed to prove which page the issue was on.

There’s a cool undocumented column that you can select called %%PHYSLOC%% that gives the page:record:slot of the selected record, and I’ve used it many times when working on corruption/data recovery issues for clients. It gives the information back in hex, so there’s a companion function called sys.fn_PhysLocCracker that formats the output nicely. Both of these have been in the product since SQL Server 2005.

Let me show you an example using the old AdventureWorks sample database.

Let’s say there’s a corruption issue where it says there’s a missing row for StateProvinceID = 1 and AddressID = 519 in the IX_Address_StateProvinceID nonclustered index (which is index ID 4) of the Person.Address table. If I want to prove that that row is NOT missing, here’s what I can do:

SELECT
	[StateProvinceID]
	, [AddressID]
	, [physloc].*
FROM [Person].[Address]
WITH (INDEX = 4)
CROSS APPLY sys.fn_PhysLocCracker (%%physloc%%) AS [physloc]
WHERE [StateProvinceID] = 1
	AND [AddressID] = 519;
GO

I’m selecting the index keys for the nonclustered index and using an index hint to force that index to be used.

And if I get any output, the row exists. In this case, I get:

StateProvinceID AddressID   file_id     page_id     slot_id
--------------- ----------- ----------- ----------- -----------
1               519         1           8120        9

Pretty cool!

I’ve also used this to find rows that I need to bit-twiddle using DBCC WRITEPAGE to work around corruptions to allow data recovery from trashed databases – tedious, but possible if you know what you’re doing.

Bottom line: there are quite a few little undocumented columns and functions you can use to easily get internals information about SQL Server, with real-world applications.

Honored to receive a Patriot Award

This morning I had the great honor to be presented with a Patriot Award for supporting the U.S. Army Reserve activities of Sergeant First Class Jonathan Kehayias.

It’s an award for me as I’m Jonathan’s supervisor; and for SQLskills as a company, Kimberly signed the statement of continuing support for the Guard and Reserve. I accepted the award from Phil Sanders, the State Vice Chair of the Washington Employer Support of the Guard and Reserve, and we flew Jonathan out to Redmond to be with us for the award ceremony.

Jonathan joined the Army Reserve 19.5 years ago, was mobilized in support of Operation Iraqi Freedom, and deployed to Iraq for a year between 2003 and 2004. Since then he’s continued serving as a Drill Sergeant and is currently a Career Counselor, responsible for helping guide service members on whether to continue serving. Since Jonathan joined SQLskills in March 2011, we’ve supported him in every way possible and encouraged him to continue to serve.

Jonathan, thank you for nominating us for this award, and sincere thanks for your past and future service.

The Curious Case of… very long failover times with a large ad hoc workload

(The Curious Case of… used to be part of our bi-weekly newsletter but we decided to make it a regular blog post instead so it can sometimes be more frequent. It covers something interesting one of us encountered when working with a client, doing some testing, or were asked in a random question from the community.)

Erin worked with a client recently who was experiencing very long failover times with a large ad hoc workload and needed to understand why. By very long, we’re talking more than 30 minutes for an availability group failover, and they’d tested failovers under load before and never had it take more than a few minutes. They wondered if their workload had changed or there was some configuration setting that was off because they’d also recently upgraded.

Obviously Erin didn’t want them to do another failover and take more downtime and luckily they have a process in place to capture wait statistics all the time so Erin could see what waits happened while the failover was in progress. One stood out to her right away: QDS_LOADDB.

The QDS_LOADDB wait happens when a database is bring brought online, Query Store is enabled, and the Query Store data is being loaded from disk. No queries can execute until that data has finished loading. In this case, the client had enabled Query Store after upgrading and with their large workload of ad hoc queries, there was 100GB of Query Store data to load. That was the cause of the long failover time and they didn’t realize that Query Store has this behavior.

Luckily there is a workaround other than disabling Query Store. You can enable documented trace flag 7752 which makes the Query Store data load asynchronous with the database startup. This means that queries can run before the data load completes, but Query Store won’t capture any information about them (Query Store is essentially read-only until the data load completes). Erin discusses this and other Query Store trace flags in her post here, and in general about Query Store settings and data size in this post.

Bottom line: make sure you understand how a feature changes behavior before enabling it, and HA/DR testing should be performed regularly to catch unexpected behaviors such as this.