DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Query Store, Plan Forcing, and Table Variables

Query Store, Plan Forcing, and Table Variables

Explore plan forcing with table variables.

Grant Fritchey user avatar by
Grant Fritchey
·
May. 16, 19 · Tutorial
Like (2)
Save
Tweet
Share
4.65K Views

Join the DZone community and get the full member experience.

Join For Free

This weekend, I was in Stockholm in Sweden talking Query Store and plan forcing with Steinar Anderson, when he mentioned the problems he had while forcing plans that had table variables in them.

Don't panic. Of course you can force a plan with a table variable...most of the time. Steinar had a fairly focused problem. Before I go on to explain the issue, let me be really clear, Steinar figured out the issue all on his own. When he outlined the problem, I saw immediately what his conclusion was going to be. What's spurring this post is that Steinar said he'd searched on the internet and no one had talked about the issue yet. So, let's talk about it.

Plan Forcing With Table Variables

First up, let's show that plan forcing does, in fact, work with table variables. Here's a simple query using a table variable:

DECLARE @MyTableVar TABLE
(
    City VARCHAR(50)
);
 
INSERT INTO @MyTableVar
(
    City
)
VALUES
('London' -- City - varchar(50)
    );
 
SELECT a.AddressID,
       a.AddressLine1,
       a.City,
       sp.Name
FROM Person.Address AS a
    JOIN @MyTableVar AS mtv
        ON a.City = mtv.City
    JOIN Person.StateProvince AS sp
        ON sp.StateProvinceID = a.StateProvinceID;

We can retrieve the query and plan ids from the Query Store and force them:

DECLARE @QueryId INT,
        @PlanID INT;
 
SELECT @QueryId = qsq.query_id,
       @PlanID = qsp.plan_id
FROM sys.query_store_query AS qsq
    JOIN sys.query_store_query_text AS qsqt
        ON qsqt.query_text_id = qsq.query_text_id
    JOIN sys.query_store_plan AS qsp
        ON qsp.query_id = qsq.query_id
WHERE qsqt.query_sql_text LIKE 'SELECT a.AddressID,%';
 
EXEC sys.sp_query_store_force_plan @QueryId, @PlanID;

NOTE: I can only do this because I'm working from a clear Query Store. If you've run other queries that start with 'SELECT a.AddressID,' then this won't work and you have to get more specific.

Regardless, if we then re-run the original script and then either look at the properties for the first operator of the plan (which will show Use Plan = True), or we query the query store, we can tell that the plan forcing worked with a table variable. Here's the code that can tell you:

SELECT query_id,
       plan_id,
       is_forced_plan,
       force_failure_count,
       last_force_failure_reason_desc
FROM sys.query_store_plan
WHERE is_forced_plan = 1;

The output on my system looked like this:

So, where does the problem come up?

Failed Plan Forcing

We're going to modify the original script just a little. I'm going to make the column in the table variable a primary key as follows:

DECLARE @MyTableVar TABLE
(
    City VARCHAR(50) PRIMARY KEY
);
 
INSERT INTO @MyTableVar
(
    City
)
VALUES
('London' -- City - varchar(50)
    );
 
SELECT a.AddressID,
       a.AddressLine1,
       a.City,
       sp.Name
FROM Person.Address AS a
    JOIN @MyTableVar AS mtv
        ON a.City = mtv.City
    JOIN Person.StateProvince AS sp
        ON sp.StateProvinceID = a.StateProvinceID;

Now, I need to look at the query store to identify if a new query has been created, a new plan, or what, so we'll modify the other query too:

SELECT qsq.query_id,
       qsq.query_hash,
       CAST(qsp.query_plan AS XML) AS QueryPlan,
	   qsp.plan_id,
	   qsp.query_plan_hash,
       qsqt.query_sql_text
FROM sys.query_store_query AS qsq
    JOIN sys.query_store_plan AS qsp
        ON qsp.query_id = qsq.query_id
    JOIN sys.query_store_query_text AS qsqt
        ON qsqt.query_text_id = qsq.query_text_id
WHERE qsqt.query_sql_text LIKE 'SELECT a.AddressID,%';

The results are as follows:

You can see that although we haven't, in any way, modified the query in question, the Query Store thinks it has a new query and a new plan. I'll explain why in a minute. First, let's try forcing the plan:

EXEC sys.sp_query_store_force_plan 7,2;

With that in place, let's rerun the query with the primary key and then check the Query Store to see if the plan was forced successfully or not:

Oops! The plan forcing failed and the reason is NO_INDEX. But that can't be right. We created the primary key. It's right there in the script. However, notice that we're relying on default names for the primary key. That gets generated, new, each time that table variable is created. Meaning, you can't force the plan because the plan you're attempting to force is invalid because it has a different primary key.

Conclusion

Steinar is explaining this to me, and I suddenly say, "But you're going to get a different key name." Steinar just smiled. Yeah, he was way ahead of me. Anyway, one of the keys to successfully using plan forcing is the understanding that you can only force a valid plan. Changes to object names, like using default key names, will lead to failures like this.

If you want to learn more about Query Store, I have an upcoming event:

SQLSaturday Columbus Precon: June 7, 2019 in Columbus, OH

Database

Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Most Popular Frameworks for Building Restful APIs
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • How To Check Docker Images for Vulnerabilities
  • Agile Scrum and the New Way of Work in 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: