SQL Server 2022 vs 2019 Query Performance gap
4
votes
0
answers
213
views
I am facing a performance issue with an SQL query.
The query is generated dynamically via the webapp, I am testing with a static version to make things simpler.
The SQL query includes temp tables and columns which get their values recalculated with Scalar Valued functions.
To be more precise, the query loads data into temporary tables from a table.
It's 350 columns, rows do not matter in our scenario, they are 0.
I have two identical machines, first one with SQL Server 2019 and second one with SQL Server 2022 and I have a huge gap in performance.
- 2019: takes 2 seconds to execute the query on first execution
- 2022: takes 14 seconds to execute the query on first execution
Query plans below:
2019
2022
Query plans were huge (150mb each). I have removed a bunch of columns to make them less than 2mb and also anonymised them. Performance gap remains. Links below:
2019
https://www.brentozar.com/pastetheplan/?id=LVoA1mZyLx
2022
https://www.brentozar.com/pastetheplan/?id=eP7Q7f1msx
Database compatibility level is 100 in both cases.
SQL Server OS and specs are identical.
As you can see, it looks like the difference is in the compilation time where there is a huge gap.
----------
Things I tried:
1. Disable scalar UDF inlining -
2022


ALTER DATABASE SCOPED CONFIGURATION SET TSQL_SCALAR_UDF_INLINING = OFF;
2. Set Legacy Cardinality Estimator (CE) - ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = ON;
3. Trace Flags: 4199, 9481, 10204, 2312
4. Disable parameter sniffing
5. Disabling batch mode on rowstore - ALTER DATABASE SCOPED CONFIGURATION SET BATCH_MODE_ON_ROWSTORE = OFF;
6. optimize for ad hoc workloads - true
7. Clear cache plans + updates stats
8. PARAMETERIZATION - tried SIMPLE and FORCE
9. Changing database compatibility level
10. Installed SQL Server 2025 Evaluation - same performance as 2022
----------
Things I expect:
Any further suggestions to what I should look into. I have a lot of scalar valued functions, I can rewrite them if absolutely necessary, just want to find out if anyone has faced similar performance issues and if there are any more settings I can adjust.
Both SQL servers are using the latest CUs:
SQL Server 2019 CU32 (15.0.4430.1, February 2025)
SQL Server 2022 CU20 (16.0.4205.1, July 2025)
**EDIT**
Statistics:
2019


Asked by faithY
(41 rep)
Aug 13, 2025, 02:54 PM
Last activity: Sep 8, 2025, 12:31 PM
Last activity: Sep 8, 2025, 12:31 PM