CSS SQL Server Engineers

This is the official team Web Log for Microsoft Customer Service and Support (CSS) SQL Support. Posts are provided by the CSS SQL Escalation Services

High CPU after upgrading to SQL Server 2005 from 2000 due to batch sort

High CPU after upgrading to SQL Server 2005 from 2000 due to batch sort

  • Comments 2

We have had some customers who reported to us that their queries consumed more CPU in SQL Server 2005 after they upgraded from 2000.   When you examine the plans.  Both are very similar.   But SQL Server 2005 has something extra.  It has "OPTIMIZED" keyword.  Here is an example plan segment:

 |--Nested Loops(Left Outer Join, OUTER REFERENCES:([B].[id_Table02Binding], [B].[si_HashBucket], [Expr1113]) OPTIMIZED WITH UNORDERED PREFETCH)                   
      |--Filter(WHERE:([testDB01].[dbo].[Table02].[dt_DeleteDatetime] as [R].[dt_DeleteDatetime] IS NULL))                                                      
      |    |--Nested Loops(Inner Join, OUTER REFERENCES:([Uniq1039], [R].[vc_Table02Name], [Expr1112]) WITH UNORDERED PREFETCH)                                    
      |         |--Nested Loops(Inner Join, OUTER REFERENCES:([C].[bi_Table02Definition], [Expr1111]) WITH UNORDERED PREFETCH)                                     
      |         |    |--Nested Loops(Inner Join, OUTER REFERENCES:([B].[bi_Table02Definition], [Expr1110]) WITH UNORDERED PREFETCH)                                
      |         |    |    |--Clustered Index Seek(OBJECT:([testDB01].[dbo].[Table02Binding].[UIX_Table02BindingScope] AS [B]), SEEK:([B].[ti_Inversion]=[@ti_Inversi
      |         |    |    |--Clustered Index Seek(OBJECT:([testDB01].[dbo].[Table03].[IX_Table03] AS [C]), SEEK:([C].[bi_Table02Definition]=[AuthZ_FSS0
      |         |    |--Index Seek(OBJECT:([testDB01].[dbo].[Table02].[PK_Table02] AS [R]), SEEK:([R].[bi_Table02Definition]=[testDB01].[dbo].[Table03].[bi_Rol
      |         |--Clustered Index Seek(OBJECT:([testDB01].[dbo].[Table02].[IX_Table02Name] AS [R]), SEEK:([R].[vc_Table02Name]=[testDB01].[dbo].[Table02].[vc_Table02Name
      |--Clustered Index Seek(OBJECT:([testDB01].[dbo].[Table01].[PK_Table01] AS [E]), SEEK:([E].[id_Table02Binding]=[testDB01].[dbo].[Table02Binding

This is a SQL 2005 optimization feature called batch sort.  What it does is try to order rows retrieved from the outer side of the nested loop to minimize random seek on the inner side of the join.  It can reduce IO cost of random seek.  If you use profiler trace and turn on SQLTransaction event, you will see many small transactions with transaction name as 'sort_init before the query is finished with this kind of plan.   More details can be found in blog:  http://blogs.msdn.com/craigfr/archive/2009/03/18/optimized-nested-loops-joins.aspx.

This feature generally improves performance if large number of rows are touched.  But in situations where only smaller number of rows are touched, overhead of this optimization can overweigh benefit.   If you have very queries that touch small number of rows, you can CPU consumptions going up in SQL 2005 following upgrade from 2000.  SQL Server 2008 behaves the same.

So what's the solution?

The solution is that you turn off this feature by using trace flag 2340.  This will reduce CPU consumption. Having said that, you need to do testing.  As mentioned earlier, if large number of rows are touched by the plan, the feature can be very beneficial.    

 

Jack Li | Senior Escalation Engineer | Microsoft SQL Server Support

Leave a Comment
  • Please add 6 and 8 and type the answer here:
  • Post
  • Can you give us an example to replicate this. Thanks

  • Instead of turning off this feature for ALL queries by using a trace flag, wouldn't it be better to provide a new query hint?  That way, it can be tailored to meet the needs of specific queries.

Page 1 of 1 (2 items)