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

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:

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.