Skip to content

Instantly share code, notes, and snippets.

@sirsql
Created December 20, 2018 17:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirsql/947e5b498d69d1ee8fc1bf31986b24a2 to your computer and use it in GitHub Desktop.
Save sirsql/947e5b498d69d1ee8fc1bf31986b24a2 to your computer and use it in GitHub Desktop.
Grabs AG latency information. Run on primary to get information for all replicas and all databases, run on a secondary to get information for just that server. *note: MintoRedo is approximate based on the size of the queue and rates, this fluctuates so it not always accurate
SELECT DB_NAME(database_id),
a.replica_server_name,
database_id AS di,
last_redone_time,
DATEDIFF(MINUTE, last_sent_time, last_received_time) AS ReceiveLatencyS,
DATEDIFF(SECOND, last_sent_time, last_redone_time) AS RedoLatencyS,
CASE
WHEN redo_queue_size = 0 THEN
0
WHEN redo_rate = 0 THEN
0
ELSE
CAST(redo_queue_size / (redo_rate * 1.0) AS NUMERIC(12, 3))
END AS MinToRedo,
CASE
WHEN log_send_queue_size = 0 THEN
0
WHEN log_send_rate = 0 THEN
0
ELSE
CAST(log_send_queue_size / (log_send_rate * 1.0) AS NUMERIC(12, 3))
END AS SecToLogSend,
log_send_queue_size AS SendQueue,
log_send_rate,
redo_queue_size,
redo_rate,
synchronization_state_desc,
synchronization_health_desc,
database_state_desc,
is_suspended,
suspend_reason_desc,
last_sent_time,
last_received_time,
last_hardened_time,
last_redone_time,
end_of_log_lsn,
last_commit_lsn,
last_commit_time
FROM sys.dm_hadr_database_replica_states d
JOIN sys.availability_replicas a
ON d.replica_id = a.replica_id
WHERE d.last_redone_time IS NOT NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment