Overview
Upgrading SQL Server involves two separate decisions: the engine version you run, and each database’s compatibility level, which controls optimizer and T-SQL behaviour. Separating them lets you upgrade the engine first, then raise compatibility in a controlled way with Query Store as a safety net.
Core concepts
- Compatibility level per database: 130 (2016), 140 (2017), 150 (2019), 160 (2022). The test database ran at 160. Higher levels unlock the newer cardinality estimator behaviour and Intelligent Query Processing features (adaptive joins, memory grant feedback, table-variable deferred compilation, scalar UDF inlining, PSP optimization at 160).
- Upgrade paths: in-place (fast, hard to roll back), side-by-side (new instance, restore or attach, switch over), and rolling with Availability Groups or log shipping for minimal downtime.
- Downgrading is not supported: a backup from a newer version cannot be restored on an older one. Keep a rollback plan based on the old environment.
- Lifecycle: each version has mainstream and extended support dates on the Microsoft lifecycle page. Check it when planning; older versions (2016 and earlier) are at or past the end of support.
- Tools: Data Migration Assistant / Azure Migrate for assessment, Query Tuning Assistant and Query Store for regression handling.
Worked example
A safe compatibility-level upgrade, using Query Store (sqlserver-query-store):
-- 1. Baseline: keep Query Store on at the OLD compatibility level and collect a representative workload.
ALTER DATABASE app SET QUERY_STORE = ON;
-- 2. Raise the level
ALTER DATABASE app SET COMPATIBILITY_LEVEL = 160;
-- 3. Compare: SSMS "Regressed Queries" report; force the last good plan where needed
EXEC sp_query_store_force_plan @query_id = 42, @plan_id = 7;
-- Rollback of behaviour is a single statement:
ALTER DATABASE app SET COMPATIBILITY_LEVEL = 150;
SELECT name, compatibility_level FROM sys.databases WHERE name = 'kb'; -- 160 on SQL Server 2022Plan regressions come mostly from cardinality estimator changes, which you can fine-tune per query (USE HINT ('FORCE_LEGACY_CARDINALITY_ESTIMATION')) or per database (LEGACY_CARDINALITY_ESTIMATION = ON) while you fix statistics and queries (sqlserver-statistics-cardinality).
Pitfalls and production notes
- Always test with production-like data and workload (capture and replay with Distributed Replay or your own harness).
- After an engine upgrade update statistics (full scan on important tables) and rebuild nothing blindly.
- Check removed or deprecated features and breaking T-SQL changes in the release notes; scan code for deprecated syntax.
- Linked servers, SSIS, logins, Agent jobs and certificates live outside the user database; move them too.
- Drivers: keep clients current; newer drivers default to
Encrypt=True, which can break connections that used untrusted certificates. - Keep cumulative updates current; the test image is SQL Server 2022 CU28 (16.0.4325).
Practice questions
- Why upgrade the engine first and raise the compatibility level later?
- What regression tools does Query Store give you after the change?
- Outline a near-zero-downtime upgrade with an Availability Group.
References
- Microsoft Learn, ALTER DATABASE compatibility level; Intelligent query processing; Microsoft Lifecycle
Related
sqlserver-query-store · sqlserver-statistics-cardinality · sqlserver-always-on · sqlserver-execution-plans