Core component of SQL Server for storing, processing, and securing data
The behavior matches a query-plan regression caused by optimizer and Cardinality Estimator (CE) changes at higher compatibility levels. The query itself is unchanged, but the CE and optimizer behavior differ between compat 150 (SQL 2019), 160 (SQL 2022), and 170 (SQL 2025), which can lead to different join strategies and dramatic performance differences.
Key points from the scenario and context:
- Compatibility level drives CE and optimizer behavior
- From SQL Server 2014 onward, new CE and optimizer changes are tied to database compatibility level, not just engine version.
- Different compat levels can choose different join types and plans for the same query and data. In your case:
- Compat 150 (2019) → ~2.5–3 minutes
- Compat 160 (2022) → ~1 minute (Nested Loops join)
- Compat 170 (2025) → >3 hours (Hash Match Right Anti Semi Join)
- This is consistent with known patterns where newer CE assumptions and optimizer changes improve many workloads but regress some specific queries.
- Plan choice differences are a known cause of regressions after upgrade
- Documentation highlights that upgrading compatibility level can introduce CE changes and different plan choices that may degrade performance for some queries, even if most workloads improve.
- The 2016/2017 example in the context (compat 120 vs 130) shows exactly this pattern: a plan flips from parallel to serial and performance collapses for a large result set. The recommended mitigations there apply directly to this scenario as well: revert compat level or force the better plan.
- Recommended mitigation pattern
Based on the documented guidance for similar regressions:
- Run the database at a lower compatibility level as a baseline
- Since compat 160 gives the best performance for this query, one pragmatic option is to keep the database at 160 on SQL Server 2025 while the regression is investigated.
- This is explicitly recommended in similar upgrade regressions (e.g., reverting from 160 to 150 when moving to SQL 2022/2025) to confirm that the issue is optimizer/CE-related rather than hardware or configuration.
- Use Query Store to identify and fix the regression
- Enable Query Store (if not already) and let it capture plans at both compat levels.
- Identify this query in Query Store and compare:
- Runtime statistics
- Plans at compat 160 vs 170
- If the compat 160 plan is consistently better, use Query Store to force that plan while running at compat 170. This is the recommended pattern in the documentation for CE/compat-level regressions.
- Alternatively, force the good plan with a hint
- If Query Store cannot be used (or as a temporary workaround), capture the good plan (from compat 160 or 150) and apply it via
USE PLANquery hint in the specific statement. The context explicitly calls outUSE PLANas a way to keep the older, better plan while running at the newer compatibility level.
- Broader troubleshooting steps
If more queries show regressions as compat 170 is used:
- Confirm environment parity:
- Same data, statistics, and indexes between pre-prod and prod.
- Same hardware characteristics (CPU count, memory, power plan) and SQL Server build level.
- Compare plans and statistics:
- Ensure statistics are up to date with similar sampling on all relevant tables.
- Check whether the new CE assumptions at higher compat levels are leading to misestimates (e.g., join cardinality, filter selectivity) that push the optimizer toward an expensive Hash Match Anti Semi Join.
- If regressions are limited to a small set of queries, use Query Store plan forcing per-query rather than lowering compat level for the entire database.
- Confirm environment parity:
- Strategy for production rollout
- Before upgrading production, run representative workloads in pre-prod with:
- Compat 150 and 160 to establish baselines.
- Compat 170 with Query Store enabled and capture regressions.
- For each regressed query, either:
- Fix the query/indexing if a clear pattern emerges, or
- Force the known-good plan via Query Store.
- If regressions are widespread and cannot be mitigated quickly, consider running production at compat 160 on SQL Server 2025 initially, then gradually move to 170 as specific regressions are addressed.
- Before upgrading production, run representative workloads in pre-prod with:
In summary, the behavior is consistent with a CE/optimizer change at compat 170 that leads to a poor join strategy for this query. The documented and supported mitigations are: temporarily use a lower compatibility level (160), and/or use Query Store or USE PLAN to force the better plan while remaining on SQL Server 2025.
References:
- Performance degradation when you upgrade database compatibility level from 120 to 130
- More information
- Troubleshoot a query that shows a significant performance difference between two servers
- Troubleshoot slow query performance in SSIS or ETL jobs
- Join containment assumption in the New Cardinality Estimator degrades query performance
- Performance degrade after migrating to SQL server 2022 - Microsoft Q&A
- Queries have been running really slowly since upgrading the database to SQL Server 2025. - Microsoft Q&A