You are currently offline, waiting for your internet to reconnect

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

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.
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)goinsert into #x values (1)goselect * from #x where col1 = 1go
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:

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.textfrom sys.dm_exec_cached_plans cpcross apply sys.dm_exec_sql_text(cp.plan_handle) as stgroup by cp.bucketid, st.texthaving count(cp.bucketid) >1order 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

temp tables
Properties

Article ID: 2526959 - Last Review: 07/18/2011 17:33:00 - Revision: 4.0

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

  • kbqfe kbexpertiseadvanced kbsurveynew kbfix KB2526959
Feedback
="var m=document.createElement('meta');m.name='ms.dqp0';m.content='true';document.getElementsByTagName('head')[0].appendChild(m);" onload="var m=document.createElement('meta');m.name='ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="http://c1.microsoft.com/c.gif?"> United States (English)
香港特別行政區 - 繁體中文
El Salvador - Español
Panamá - Español
Uruguay - Español
대한민국 - 한국어
España - Español
Paraguay - Español
Venezuela - Español
://c1.microsoft.com/c.gif?DI=4050&did=1&t=">= 1; var varCustomerTracking = 1; var Route = "76500"; var Ctrl = ""; document.write(" appendChild(m);" onload="var m=document.createElement('meta');m.name='ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="http://c1.microsoft.com/c.gif?"> ne; " src="https://c1.microsoft.com/c.gif?DI=4050&did=1&t=">