FIX: Application performance issue when a query references temporary tables that are created in a session in SQL Server 2008 and in SQL Server 2008 R2

Article translations Article translations
Article ID: 2526959 - View products that this article applies to.
Microsoft distributes Microsoft SQL Server 2008 R2 fixes as one downloadable file. Because the fixes are cumulative, each new release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 R2 fix release.
Expand all | Collapse all

SYMPTOMS

Consider the following scenario:
  • An application runs a query that references some temporary tables in Microsoft SQL Server 2008 and in Microsoft SQL Server 2008 R2. The temporary tables are created in a session instead of in a stored procedure.
  • The query plan is cached.
  • Many users run the same query in a highly concurrent environment.

    For example, many users use an application that runs the following query:
    create table #x (col1 int) go insert into #x values (1) go select * from #x where col1 = 1 go
In this scenario, the application encounters performance issues.

Note For more information about how to identify this issue, see the "More information" section.

CAUSE

This issue occurs because the same query plans are inserted into cache in the same hash bucket for each user when the query text is the same.

When a large number of plans are hashed to the same bucket (also known as a hash chain), the time to look up a plan in the cache increases significantly, and the performance issues may occur. Additionally, worker threads may encounter severe spinlock contention, and other threads are affected when these worker threads do not perform the correct scheduler yields.

RESOLUTION

Note Only the update for SQL Server 2008 R2 is currently available. For SQL Server 2008, see the "Workaround" section to work around this issue.

Cumulative update information

SQL Server 2008 R2 Service Pack 1

The fix for this issue was first released in Cumulative Update 1 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:
2544793 Cumulative Update package 1 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 2008 R2

The fix for this issue was first released in Cumulative Update 7. 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:
2507770 Cumulative Update package 7 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

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 in SQL Server 2008 and in SQL Server 2008 R2, move any references of temporary tables to inside stored procedures.

MORE INFORMATION

For more information about how to create and modify table basics, visit the following MSDN website:
How to create and modify table basics

Spinlock contention can be identified from the sys.dm_os_spinlock_stats dynamic management view (DMV) that has a high contention rate on the SOS_CACHESTORE spinlock.

To identify this issue, run a query that resembles the following:
select cp.bucketid, count(cp.bucketid) as BucketCount, st.text from sys.dm_exec_cached_plans cp cross apply sys.dm_exec_sql_text(cp.plan_handle) as st group by cp.bucketid, st.text having count(cp.bucketid) >1 order by BucketCount desc
If the following conditions are true when you run this query, you may encounter the performance issues:
  • This query generates the same query text.
  • The count for the same bucketid is large.

    Note A large count for the same bucketid is usually over 1000.
  • The queries reference temporary tables

Properties

Article ID: 2526959 - Last Review: July 18, 2011 - Revision: 4.0
APPLIES TO
  • Microsoft SQL Server 2008 R2 Datacenter
  • Microsoft SQL Server 2008 R2 Developer
  • Microsoft SQL Server 2008 R2 Enterprise
  • Microsoft SQL Server 2008 R2 Express
  • Microsoft SQL Server 2008 R2 Express with Advanced Services
  • Microsoft SQL Server 2008 R2 Standard
  • Microsoft SQL Server 2008 R2 Standard Edition for Small Business
  • Microsoft SQL Server 2008 R2 Web
  • Microsoft SQL Server 2008 R2 Workgroup
  • Microsoft SQL Server 2008 Developer
  • Microsoft SQL Server 2008 Enterprise
  • Microsoft SQL Server 2008 Express
  • Microsoft SQL Server 2008 Express with Advanced Services
  • Microsoft SQL Server 2008 Standard
  • Microsoft SQL Server 2008 Standard Edition for Small Business
  • Microsoft SQL Server 2008 Web
  • Microsoft SQL Server 2008 Workgroup
Keywords: 
kbqfe kbexpertiseadvanced kbsurveynew kbfix KB2526959

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