FIX: Long compilation time for a query that contains many distinct operators in SQL Server 2016

Applies to: SQL Server 2016 DeveloperSQL Server 2016 EnterpriseSQL Server 2016 Enterprise Core


The cardinality estimation logic that's known as the cardinality estimator (CE) was redesigned in SQL Server 2014 (compatibility levels 120) and SQL Server 2016 (compatibility levels 130) to improve the quality of query plans.

However, when you compile a complex query that contains many joins with a DISTINCT or GROUP BY operator, compilation may take much longer than expected.


This update that fixes this issue is included in Service Pack 1 for SQL Server 2016.

Note After you install this update, you must set trace flag 4199 to enable the fix. To set trace flag 4199, see the Trace Flags (Transact-SQL) topic at MSDN.


To work around this issue, use one of the following methods:
  • Use trace flag 9481 to set the query optimizer cardinality estimation model to SQL Server 2012 or an earlier version.
  • Change the compatibility level of the database to SQL Server 2012 (110) or an earlier version.


Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.


Learn about the terminology  that Microsoft uses to describe software updates.