
-- Get times asssociated with secondary databases
SELECT 
	adc.database_name, 
	ag.name AS ag_name,
	ar.replica_server_name,
	drs.last_commit_time,
	drs.last_hardened_time,
	drs.last_received_time,
	drs.last_redone_time,
	drs.last_sent_time
FROM sys.dm_hadr_database_replica_states AS drs
INNER JOIN sys.availability_databases_cluster AS adc 
     ON drs.group_id = adc.group_id AND 
          drs.group_database_id = adc.group_database_id
INNER JOIN sys.availability_groups AS ag
     ON ag.group_id = drs.group_id
INNER JOIN sys.availability_replicas AS ar
	ON ar.group_id = drs.group_id AND
			ar.replica_id = drs.replica_id
WHERE drs.is_primary_replica = 0;


SELECT 
	adc.database_name, 
	ag.name AS ag_name,
	ar.replica_server_name,
	drs.log_send_queue_size,
	drs.log_send_rate,
	drs.redo_queue_size,
	drs.redo_rate
FROM sys.dm_hadr_database_replica_states AS drs
INNER JOIN sys.availability_databases_cluster AS adc 
     ON drs.group_id = adc.group_id AND 
          drs.group_database_id = adc.group_database_id
INNER JOIN sys.availability_groups AS ag
     ON ag.group_id = drs.group_id
INNER JOIN sys.availability_replicas AS ar
	ON ar.group_id = drs.group_id AND
			ar.replica_id = drs.replica_id;
