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

Applies to: SQL Server 2014 DeveloperSQL Server 2014 DeveloperSQL Server 2014 Enterprise


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.


Learn about the terminology that Microsoft uses to describe software updates.