7 More Ways To Query Always On Availability Groups - SQL Undercover
7 More Ways To Query Always On Availability Groups - SQL Undercover
When we rst published 7 ways to Query Always On Availability Groups using SQL we had no
idea it would so popular! [Kada smo prvi put objavili 7 načina za postavljanje upita uvijek
dostupnim grupama pomoću SQL -a nismo imali pojma da će biti toliko popularan!] So here is
a quick post with 7 more ways to query Always on availability groups using TSQL, its always
handy to have a few little snippets like these stashed away for when you need them! [Evo
kratkog posta sa još 7 načina za postavljanje upita uvijek dostupnim grupama pomoću TSQL -
a, uvijek je zgodno imati nekoliko malih isječaka sakrivenih kad vam zatrebaju!]
Check which replicas have read only con g in place to allow them to be readable when set
within an AG/s: [Provjerite koje su replike imale samo kon guraciju za čitanje kako bi im se
omogućilo čitanje kada su postavljene unutar AG/a:]
1 SELECT
2 PrimaryServer.replica_server_name AS PrimaryServer,
3 Groups.name AS AGname,
4 ReadOnlyReplica.replica_server_name AS ReadOnlyReplica,
5 ReadOnlyReplica.read_only_routing_url AS RoutingURL,
6 RoutingList.routing_priority AS RoutingPriority
7 FROM sys.availability_read_only_routing_lists RoutingList
8 INNER JOIN sys.availability_replicas PrimaryServer ON RoutingList.repl
9 INNER JOIN sys.availability_replicas ReadOnlyReplica ON RoutingList.re
10 INNER JOIN sys.availability_groups Groups ON Groups.group_id = Primary
11 WHERE PrimaryServer.replica_server_name != ReadOnlyReplica.replica_ser
12 ORDER BY
13 PrimaryServer ASC,
14 AGname ASC
1 SELECT [Groups].[name]
2 FROM sys.dm_hadr_availability_group_states States
3 INNER JOIN sys.availability_groups Groups ON States.group_id = Groups.g
4 WHERE primary_replica = @@Servername
How many databases are there in each availability group on this server?
1 SELECT
2 Groups.name,
3 COUNT([AGDatabases].[database_name]) AS DatabasesInAG
4 FROM master.sys.availability_groups Groups
5 INNER JOIN Sys.availability_databases_cluster AGDatabases ON Groups.gro
6 GROUP BY Groups.name
7 ORDER BY Groups.name ASC
1 SELECT
2 Groups.name,
3 SUM(CAST((CAST([master_files].[size] AS BIGINT )*8) AS MONEY)/1024/1024
4 FROM master.sys.availability_groups Groups
5 INNER JOIN Sys.availability_databases_cluster AGDatabases ON Groups.gro
6 INNER JOIN sys.databases ON AGDatabases.database_name = databases.name
7 INNER JOIN sys.master_files ON databases.database_id = master_files.dat
8 GROUP BY Groups.name
9 ORDER BY Groups.name ASC
1 SELECT DISTINCT
2 Groups.name AS AGname,
3 Replicas.replica_server_name,
4 States.role_desc,
5 States.synchronization_health_desc,
6 ISNULL(ReplicaStates.suspend_reason_desc,'N/A') AS suspend_reason_desc
7 FROM sys.availability_groups Groups
8 INNER JOIN sys.dm_hadr_availability_replica_states as States ON States
9 INNER JOIN sys.availability_replicas as Replicas ON States.replica_id
10 INNER JOIN sys.dm_hadr_database_replica_states as ReplicaStates ON Rep
1 USE [master];
2
3 --Set Backup preference to Primary replica only
4 ALTER AVAILABILITY GROUP [AG name here] SET(AUTOMATED_BACKUP_PREFERENC
5
6 --Set Backup preference to Secondary only
7 ALTER AVAILABILITY GROUP [AG name here] SET(AUTOMATED_BACKUP_PREFERENC
8
9 --Set Backup preference to Prefer secondary
10 ALTER AVAILABILITY GROUP [AG name here] SET(AUTOMATED_BACKUP_PREFERENC
11
12 --Set Backup preference to Any replica (no preference)
13 ALTER AVAILABILITY GROUP [AG name here] SET(AUTOMATED_BACKUP_PREFERENC
14
15 --Backup preference via TSQL can be found here
16 SELECT
17 name AS AGname,
18 automated_backup_preference_desc
19 FROM sys.availability_groups;
SHARE THIS:
Twitter Facebook
7 ways to Query Always On Finding the Primary Replica of an Dude where’s my access?
Availability Groups using SQL Availability Group Jun 3, 2017
Sep 19, 2017 Oct 24, 2019 In "Database"
In "AG Dashboard" In "PowerShell"
https://sqlundercover.wordpress.com/ Uncategorized
Pingback: Happy New Year – Our Top Posts of 2019 – SQL Undercover
Leave a Reply
Enter your comment here...
Adrian Buckman
ADRIAN BUCKMAN
Inspector V2.6 now available
Inspector 2.4 now available
After working in the motor trade for over 11 years
Adrian decided to give it all up to persue a dream of SQL Server login – default database and
failed logins
working in I.T. Adrian has over 5 years of experience
working with SQL server and loves all things SQL, BlitzFileStats custom module available for
Inspector V2
Adrian currently works as a Database Administrator
for one of the UK’s Largest Software Companies Inspector 2.1 now available
Edward Benson
Chris
T-SQL Tuesday #96 How did I get started?
David Fowler
sp_RestoreScript 1.9 – a dirty little bug squished!
My Application is Getting SQL Timeout Errors, But
What Query is the Problem?
When Read Committed Snapshot Goes Bad –
Version Store Filling TempDB
UP ↑