Edit

Share via


Persisted statistics for readable secondaries

Applies to: Applies to: SQL Server 2025 (17.x) Preview Azure SQL Database SQL database in Microsoft Fabric

Note

Query Store for readable secondaries was introduced in SQL Server 2022 and the Persisted statistics for readable secondaries feature uses the infrastructure that Query Store for readable secondaries has in place. Query Store for readable secondaries is on by default in SQL Server 2025, whereas in SQL Server 2022 it was off and required trace flag 12606 to enable. Trace flag 12606 is not required on SQL Server 2025 and later versions and if used, will result in turning the Query Store for readable secondaries feature off.

Background

On readable secondary replicas, statistics can also be automatically created when the auto create statistics option is enabled but those statistics are temporary and disappear when an instance is restarted. When statistics on a read-only database or read-only snapshot are missing or stale, the Database Engine creates and maintains temporary statistics in tempdb. When the Database Engine creates temporary statistics, the statistics name is appended with the suffix _readonly_database_statistic to differentiate the temporary statistics from the permanent statistics. The suffix _readonly_database_statistic is reserved for statistics generated by SQL Server. The reason that this approach was taken was to address workloads that execute against readable secondary replicas that might require distinct statistics that don't exist on the primary replica.

With the addition of the Query Store for readable secondaries, secondary replicas have a communication channel back to the primary replica, which is currently used to send query store data (queries, plans, execution statistics, etc.) to the primary to persist them in the database and make them available for querying.

A similar mechanism is being used to send statistics information back to the primary and subsequently send back to all secondary replicas. Temporary statistics that are created on any secondary replica which are stored in tempdb will be sent to and persisted on the primary replica, which will make them usable by all readable secondary replicas.

Support catalog views

To support comparing stats creation/update between secondary and primary, and to assist with understanding where statistics were created, three new columns have been added to the sys.stats catalog view:

Column name Data Type Description
replica_role_id tinyint Identifies the replica set number for this replica. 1 = Primary, 2 = Secondary, 3 = Geo Replication Forwarder, 4 = Geo HA Secondary
replica_role_desc nvarchar(60) Primary, Secondary, Geo Replication Forwarder, Geo HA Secondary
replica_name sysname Instance name of the replica in the availability group. Will be NULL for the primary replica