FIX: Query with UNION ALL and a row goal may run slower in SQL Server 2014 or later versions when it's compared to SQL Server 2008 R2

Applies to: SQL Server 2014 DeveloperSQL Server 2014 EnterpriseSQL Server 2014 Enterprise Core More

Symptoms


When you query a view in Microsoft SQL Server 2014 or 2016, it might take much longer time to complete if you compare it to the execution in SQL Server 2008 R2. 

When you execute a query that uses UNION ALL and a row goal (such as TOP N clause, FAST N, or EXISTS predicate), you may notice that the query runs slower in SQL Server 2014 or later versions if you compare it to SQL Server 2008 R2.

Cause


In SQL Server 2008 R2, the Query Optimizer changes the order of different parts under UNION ALL if a row goal is present, so that the cheapest part goes first. This allows the query to finish execution earlier if the cheaper parts produce required number of rows, without continuing to execute more expensive parts. Such reordering does not occur in later versions of SQL Server.

Resolution


This issue is fixed in the following cumulative update for SQL Server:

       Cumulative Update 7 for SQL Server 2016 RTM

       Cumulative Update 4 for SQL Server 2016 SP1

       Cumulative Update 6 for SQL Server 2014 SP2

Note  The Query Optimiser must be enabled for this issue. 

For more information about query optimizer and trace flags, refer to the following links:

SQL Server Query Optimizer hotfix TF 4199 servicing model

DBCC console commands and Trace flags

Status


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

References


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