先記錄下,回頭再補內容
legacy CE off 的時候,不同的兼容級別SQL優化器的表現不一樣,而且在測試中確實執行時間差別很大
legacy CE on的時候,不同的兼容級別也會對優化器有影響,同樣的legacy CE on,在2019使用140和150的兼容級別,某些SQL執行時間差別也很大
升級后數據庫使用的compatibility level從140變成150, 部分語句變慢,添加查詢提示 OPTION (USE HINT('QUERY_OPTIMIZER_COMPATIBILITY_LEVEL_140')) 強制使用140的優化器,問題得到解決
不改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