Reader small image

You're reading from  Learn T-SQL Querying - Second Edition

Product typeBook
Published inFeb 2024
PublisherPackt
ISBN-139781837638994
Edition2nd Edition
Right arrow
Authors (2):
Pedro Lopes
Pedro Lopes
author image
Pedro Lopes

Pedro Lopes is a Program Manager in the Database Systems group, based in Redmond, WA, USA. He has over 19 years of industry experience and has been with Microsoft for 9 years. He is currently responsible for program management of Database Engine features for in-market and vNext versions of SQL Server, with a special focus on the Relational Engine. He has extensive experience with query performance troubleshooting and is a regular speaker at numerous conferences such as SQLBits, PASS Summit, SQLIntersection, Microsoft Ignite, and Microsoft Build. He blogs about SQL on the SQL Server Team blog. He has authored several tools in the Tiger toolbox on GitHub: AdaptiveIndexDefrag maintenance solution, BPCheck, and usp_WhatsUp.
Read more about Pedro Lopes

Pam Lahoud
Pam Lahoud
author image
Pam Lahoud

Pam Lahoud is a Program Manager in the Database Systems group, based in Redmond, WA, USA. She has been with Microsoft for 13 years and is currently responsible for program management of Database Engine features for in-market and vNext versions of SQL Server, with a special focus on the Storage Engine area. She is passionate about SQL Server performance and has focused on performance tuning and optimization, particularly from the developer's perspective, throughout her career. She is a SQL Server 2008 Microsoft Certified Master (MCM) with over 20 years of experience working with SQL Server.
Read more about Pam Lahoud

View More author details
Right arrow

Managing Optimizer Changes

In this chapter, we will discuss how users can manage Query Optimizer changes throughout versions of the SQL Database Engine. We will cover a client-side feature in SQL Server Management Studio (SSMS) – the Query Tuning Assistant (QTA) – and a new feature for the SQL Server 2022 release – CE Feedback. Both features aim at addressing some of the most common causes of cardinality estimation (CE)-related performance regressions that may affect our T-SQL queries after an upgrade from an older version of SQL Server to a newer version, namely SQL Server 2016 and above.

At the time of writing, SQL Server 2014 is months away from completing its 10-year life cycle and reaching end of support. Also, SQL Server 2016 and SQL Server 2017 no longer have mainstream support. This can raise concerns for all those still running applications supported by these legacy SQL Server versions.

However, modernizing the database platform (a synonym for upgrading...

Technical requirements

The examples used in this chapter are designed for use on SQL Server 2022 and Azure SQL Database, but they should work on any version of SQL Server, 2012 or later. The Developer edition of SQL Server is free for development environments and can be used to run all the code samples. There is also a free tier of Azure SQL Database that you can use for testing at https://aka.ms/freedb.

You will need the AdventureWorks2016_EXT (referred to as AdventureWorks) and AdventureWorksDW2016_EXT (referred to as AdventureWorksDW) sample databases, which can be found on GitHub at https://github.com/Microsoft/sql-server-samples/releases/tag/adventureworks. Code samples for this chapter can also be found on GitHub at https://github.com/PacktPublishing/Learn-T-SQL-Querying-Second-Edition/tree/main/ch12.

Understanding where QTA and CE Feedback are needed

The CE version that our databases use directly influences how query plans are created for queries that will be executed in those databases. And we have seen first-hand the effects of the CE every time we compared estimated number of rows with actual number of rows throughout the book – for example, in the Query plan comparison section of Chapter 9, Comparative Analysis of Query Plans, where we dealt with the Row Goal optimization scenario.

When upgrading from older versions of the SQL Database Engine to newer versions (for example, an older SQL Server version to Azure SQL Database or SQL Server 2022), we need to be conscious of how upgrading from an older CE version to a newer CE can affect our workloads – benefits are expected for the most part, but regressions can happen. For example, we discussed in Chapter 6, Discovering T-SQL Anti-Patterns in Depth, how the latest versions of the SQL Database Engine solve classic...

Understanding QTA fundamentals

While guiding us through the recommended process, QTA doesn’t follow it exactly. The very last step, step 5, will not have the same outcome we saw in the previous section; instead of providing options to revert to a last known good plan, QTA helps to find a new state that is not the pre-CE upgrade or post-CE upgrade plan but a new plan that will hopefully outperform both of the previous plans.

The following diagram summarizes the recommended steps to minimize risk with CE upgrades using QTA, which replaces the very last step of the process described in the previous Understanding where QTA and CE Feedback are needed section:

Figure 12.4: The recommended steps to minimize risk with CE upgrades using QTA

Figure 12.4: The recommended steps to minimize risk with CE upgrades using QTA

How does QTA find a better query plan for regressed queries? Starting with the same data that’s available in Query Store’s Regressed Queries report, QTA will look for query patterns that may be affected...

Exploring the QTA workflow

We’ve briefly described what QTA does and, in greater depth, how QTA works internally. But now, it’s time to actually run through the recommended database compatibility upgrade we discussed in the Understanding QTA fundamentals section.

QTA is a session-based tool, which means we can open and close it at will while the database compatibility upgrade process progresses. This is useful, given that the recommended database compatibility upgrade process can run for days, depending on the business cycle that our workload serves.

Tip

QTA doesn’t need to run from an SSMS installed on the server. It can execute the workflow against the server from our laptop, desktop, or another designated management machine that you have available.

The way QTA stores our session’s state and analysis data is by creating a few tables in the targeted user database in the msqta schema, as shown in the following screenshot. This schema will remain...

Summary

From the set of regressed workloads that the SQL Database Engine team has handled over the years, the initial scenarios covered by QTA and CE Feedback are some of the most common after a database compatibility level upgrade (and, therefore, a CE upgrade), which can make users question whether to upgrade. But that is just because when upgrading from an old version, such as SQL Server 2008 or 2012, our T-SQL queries were fully tuned to the only CE model set that existed at the time. When some aspects of cardinality estimation changed, there was a possibility that some queries would have to be tuned for the new models. Fortunately, the SQL Database Engine team believes that backward compatibility is an asset in the SQL Database Engine and included these hints, which allow selective tuning opportunities for the scenarios covered by QTA and CE Feedback, as well as others less common not covered by QTA nor CE Feedback.

Whether you choose to use QTA or not, following the recommended...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn T-SQL Querying - Second Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781837638994
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Pedro Lopes

Pedro Lopes is a Program Manager in the Database Systems group, based in Redmond, WA, USA. He has over 19 years of industry experience and has been with Microsoft for 9 years. He is currently responsible for program management of Database Engine features for in-market and vNext versions of SQL Server, with a special focus on the Relational Engine. He has extensive experience with query performance troubleshooting and is a regular speaker at numerous conferences such as SQLBits, PASS Summit, SQLIntersection, Microsoft Ignite, and Microsoft Build. He blogs about SQL on the SQL Server Team blog. He has authored several tools in the Tiger toolbox on GitHub: AdaptiveIndexDefrag maintenance solution, BPCheck, and usp_WhatsUp.
Read more about Pedro Lopes

author image
Pam Lahoud

Pam Lahoud is a Program Manager in the Database Systems group, based in Redmond, WA, USA. She has been with Microsoft for 13 years and is currently responsible for program management of Database Engine features for in-market and vNext versions of SQL Server, with a special focus on the Storage Engine area. She is passionate about SQL Server performance and has focused on performance tuning and optimization, particularly from the developer's perspective, throughout her career. She is a SQL Server 2008 Microsoft Certified Master (MCM) with over 20 years of experience working with SQL Server.
Read more about Pam Lahoud