A query may take a long time to run if the query optimizer uses the Top operator in SQL Server 2008 R2 or in SQL Server 2012

Article ID: 2667211 - View products that this article applies to.
Expand all | Collapse all

On This Page

Microsoft distributes Microsoft SQL Server 2008 R2 Service Pack 1 (SP1) or Microsoft SQL Server 2012 fixes in one downloadable file. Because the fixes are cumulative, each new release contains all the hotfixes and all the security updates that were included with the previous SQL Server 2008 R2 Service Pack 1 (SP1) or SQL Server 2012 update release.

Symptoms

Consider the following scenario. You run a query in Microsoft SQL Server 2008 R2 or in Microsoft SQL Server 2012, and the query optimizer generates an execution plan that contains the Top operator. In this scenario, the query may take a long time to run.

For example, you run a query that resembles the following:
SELECT TableA.Col1 FROM TableA JOIN TableB ON TableA.Col1 = TableB.Col1 WHERE TableA.Col2 = N'A' AND TableB.Col3 IN (SELECT Col1 FROM TableC )
This query involves two joins. One join is between TableB and TableC. (This is implied by the presence of the IN predicate.) The other join is between Table A and the result of the first join. The IN predicate will return true if the query finds at least one row that satisfies the first join predicate. Therefore, the query optimizer adds the Top operator in the query execution plan.

Such a query may return only a few rows. However, it may take more time to execute this query than would be expected. In the actual query execution plan, the estimated number of rows differs significantly from the actual number of rows. This affects the query optimizer’s ability to accurately estimate the cost for different plan choices.

The issue may also occur if you use the TOP operator explicitly or if you use one of the following operators:
  • OPTION (FAST N)
  • IN
  • EXISTS

Resolution

Cumulative update information

SQL Server 2008 R2 Service Pack 2

The fix for this issue was first released in Cumulative Update 1 for SQL Server 2008 R2 Service Pack 2. For more information about how to obtain this cumulative update package, click the following article number to view the article in the Microsoft Knowledge Base:
2720425 Cumulative Update package 1 for SQL Server 2008 R2 Service Pack 2
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 R2 fix release. We recommend that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
2730301 The SQL Server 2008 R2 builds that were released after SQL Server 2008 R2 Service Pack 2 was released

SQL Server 2008 R2 Service Pack 1

The fix for this issue was first released in Cumulative Update 7 for SQL Server 2008 R2 Service Pack 1. For more information about how to obtain this cumulative update package, click the following article number to view the article in the Microsoft Knowledge Base:
2703282 Cumulative Update package 7 for SQL Server 2008 R2 Service Pack 1
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 R2 fix release. We recommend that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
2567616 The SQL Server 2008 R2 builds that were released after SQL Server 2008 R2 Service Pack 1 was released

SQL Server 2012

The fix for this issue was first released in Cumulative Update 2 for SQL Server 2012. For more information about this cumulative update package, click the following article number to view the article in the Microsoft Knowledge Base:
2703275 Cumulative update package 2 for SQL Server 2012
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2012 fix release. Microsoft recommends that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
2692828 The SQL Server 2012 builds that were released after SQL Server 2012 was released
You must apply a SQL Server 2012 hotfix to an installation of SQL Server 2012.

SQL Server 2008 R2

The fix for this issue was first released in Cumulative Update 13. For more information about how to obtain this cumulative update package for SQL Server 2008 R2, click the following article number to view the article in the Microsoft Knowledge Base:
2679366 Cumulative update package 13 for SQL Server 2008 R2
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 R2 fix release. We recommend that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
981356 The SQL Server 2008 R2 builds that were released after SQL Server 2008 R2 was released

Note
After you install the cumulative update, you should turn on trace flag 4199 to enable the fix that is provided in this cumulative update if the following conditions are true:
  • The query plan has a TOP operator above a Nested Loop Join operator (join 1) at the right side of another apply/join operator (join 2).
  • The outer side of join 1 has an outer reference to the outer side of join 2.
  • Join 1 uses an equality predicate on a left-side column that is unique.
An example of such a query appears in the "Symptoms" section. (This assumes that some uniqueness constraints exist in the database.)

If there are other queries that contain TOP, OPTION (FAST N), IN, or EXISTS operators and produce similar symptoms but do not fit the pattern that is described here, turn on trace flag 4138.

Warning If you enable trace flag 4138, performance may decrease for some other queries that contain TOP, OPTION (FAST N), IN, or EXISTS operators. Trace flag 4138 should be used only when the following conditions are true:
  • Its effect on slow queries that involve these operators is confirmed.
  • There are no other useful workarounds.
  • No regressions are detected on other queries that contain similar operators.


Status

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

Workaround

To work around this issue, use the LEFT OUTER JOIN operator or any of several OPTIMIZER hints.

Properties

Article ID: 2667211 - Last Review: December 14, 2012 - Revision: 4.0
Applies to
  • Microsoft SQL Server 2008 R2 Standard
  • Microsoft SQL Server 2008 R2 Datacenter
  • Microsoft SQL Server 2008 R2 Developer
  • Microsoft SQL Server 2008 R2 Enterprise
  • Microsoft SQL Server 2008 R2 Web
  • Microsoft SQL Server 2008 R2 Workgroup
  • Microsoft SQL Server 2012 Developer
  • Microsoft SQL Server 2012 Enterprise
  • Microsoft SQL Server 2012 Standard
  • Microsoft SQL Server 2012 Web
Keywords: 
kbqfe kbfix kbexpertiseadvanced kbsurveynew KB2667211

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com