4
votes

Problem : We are experiencing some performance issues when read only replica is enabled on a premium tier in azure SQL which is very strange and unexplainable.

I can find if a query is being executed on a read only replica by using the following inbuilt function

DATABASEPROPERYEX(DB_NAME(),'Updateability')

However there is no way to monitor read only queries.

As per the following article, Extended events and query store is not supported in preview for read only replica https://docs.microsoft.com/en-us/azure/sql-database/sql-database-read-scale-out

whereas the following article suggests that read scale out feature is now generally available. Still I cannot monitor queries executed on read only replica in premium SQL database tier

https://azure.microsoft.com/en-in/roadmap/azure-sql-database-announces-preview-of-read-scale-out-support-in-premium-service/

I can see a user voice item

https://feedback.azure.com/forums/217321-sql-database/suggestions/34337935-monitor-queries-on-secondary-database-when-read-sc

Is there any other option to monitor read only workload?

1
Without the Query Store or Extended Events, I'd say currently, there isn't a way to get it done.Grant Fritchey

1 Answers

2
votes

As you mentioned, you cannot use Query Store or Extended Events to monitor queries on read-only replicas but still can use sys.dm_exec_query_stats dynamic view, which can give you for example top queries by CPU consumption

SELECT TOP (25) MIN(query_stats.statement_text) AS [Statement Text],
SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS [Avg CPU Time],
query_stats.query_hash AS [Query Hash]
FROM (SELECT QS.*, SUBSTRING(ST.[text], (QS.statement_start_offset/2) + 1,
    ((CASE statement_end_offset
        WHEN -1 THEN DATALENGTH(st.[text])
        ELSE QS.statement_end_offset END
            - QS.statement_start_offset)/2) + 1) AS statement_text
     FROM sys.dm_exec_query_stats AS QS
     CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) AS ST) AS query_stats
GROUP BY query_stats.query_hash
ORDER BY [Avg CPU Time] DESC;

You can change the ORDER BY clause on below query to monitor other resources:

SELECT TOP 10
    (total_logical_reads/execution_count) AS avg_logical_reads,
    (total_logical_writes/execution_count) AS avg_logical_writes,
    (total_physical_reads/execution_count) AS avg_phys_reads,
    (total_worker_time/execution_count) AS avg_cpu_over_head,
total_logical_reads, total_logical_writes, total_physical_reads,
total_worker_time, execution_count, total_elapsed_time AS Duration,
plan_generation_num AS num_recompiles,
statement_start_offset AS stmt_start_offset,
    (SELECT SUBSTRING(text, statement_start_offset/2 + 1,
        (CASE WHEN statement_end_offset = -1
            THEN LEN(CONVERT(nvarchar(MAX),text)) * 2
                ELSE statement_end_offset
            END - statement_start_offset)/2)
     FROM sys.dm_exec_sql_text(sql_handle)) AS query_text,
      (SELECT query_plan FROM sys.dm_exec_query_plan(plan_handle)) AS query_plan
FROM sys.dm_exec_query_stats a
--JUST CHANGE THE ORDER BY TO GET THE OTHER RESOURCES
ORDER BY (total_logical_reads + total_logical_writes)/execution_count DESC

You may want to store the result of queries like the above for later analysis.