SQL Server 2017 升级2019 后部分语句变慢解决方法 QUERY_OPTIMIZER_COMPATIBILITY_LEVEL_140

先记录下,回头再补内容

legacy CE off 的时候,不同的兼容级别SQL优化器的表现不一样,而且在测试中确实执行时间差别很大

https://support.microsoft.com/en-us/topic/kb4342424-improvement-update-to-support-query-optimizer-compatibility-level-n-in-use-hint-option-in-sql-server-2017-b326790c-9a0e-b76b-0b52-aa30f2f79f91

legacy CE on的时候,不同的兼容级别也会对优化器有影响,同样的legacy CE on,在2019使用140和150的兼容级别,某些SQL执行时间差别也很大
升级后数据库使用的compatibility level从140变成150, 部分语句变慢,添加查询提示 OPTION (USE HINT('QUERY_OPTIMIZER_COMPATIBILITY_LEVEL_140')) 强制使用140的优化器,问题得到解决

参考链接
https://support.microsoft.com/en-us/topic/kb4342424-improvement-update-to-support-query-optimizer-compatibility-level-n-in-use-hint-option-in-sql-server-2017-b326790c-9a0e-b76b-0b52-aa30f2f79f91

不改CE设置,改优化器行为的方法:
In SQL Server 2014 and later versions, you can use trace flag 9476 to force the New CE to use the Simple Containment assumption instead of the default Base Containment assumption.
https://docs.microsoft.com/en-us/troubleshoot/sql/performance/cardinality-estimator-degrades-query-performance

原文地址:https://www.cnblogs.com/sqlzh/p/14431250.html