FIX: Queries that run against secondary databases always get recompiled in SQL Server

Assume that you use the AlwaysOn Availability Groups feature in Microsoft SQL Server 2012, 2014, or 2016. Every time you execute a query against a secondary database, the query is recompiled. This issue occurs because the statistics are not propagated correctly from the primary server to the secondary server. If you run the profiler trace, you will find something similar with the following about StatMan which means SQL server is querying statistics:

SELECT StatMan([SC0], [SB0000]) FROM (SELECT TOP 100 PERCENT [SC0], step_direction([SC0]) over (order by NULL) AS [SB0000]  FROM (SELECT [xxx] AS [SC0] FROM [dbo].[xxx] TABLESAMPLE SYSTEM (7.033584e-001 PERCENT) WITH (READUNCOMMITTED) ) AS _MS_UPDSTATS_TBL_HELPER ORDER BY [SC0], [SB0000] ) AS _MS_UPDSTATS_TBL  OPTION (MAXDOP 1)

Note If you disable the Auto Update Statistics option or enable the Auto Update Statistics Asynchronously option, this issue does not occur.

The issue was first fixed in the following cumulative update of SQL Server: Recommendation: Install the latest cumulative update for SQL Server
Each new cumulative update for SQL Server contains all the hotfixes and all the security fixes that were included with the previous cumulative update. We recommend that you download and install the latest cumulative updates for SQL Server:
Learn about the terminology that Microsoft uses to describe software updates.

Article ID: 3181444 - Last Review: 09/22/2016 03:50:00 - Revision: 3.0

Microsoft SQL Server 2014 Service Pack 1

  • kbqfe kbfix kbsurveynew kbexpertiseadvanced KB3181444