0% found this document useful (0 votes)
30 views1 page

7 More Ways To Query Always On Availability Groups - SQL Undercover

Uploaded by

divandann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views1 page

7 More Ways To Query Always On Availability Groups - SQL Undercover

Uploaded by

divandann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

SQL Undercove

The Home of the Undercover DBAs

HOME / FREE TOOLS / PREVIOUS POSTS / PODCASTS / WEEKLY NEWSLETTER / CONTACT US 

7 more ways to Query Always on


Availability groups
Follow

Post Updated: Replaced Query 3 with transactions/sec query.

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

Is this server a primary server for any availability group?

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

Total Transactions/sec and Write transactions/sec per Availability group in a 15 second


snapshot.

1 SET NOCOUNT ON;


2
3 IF OBJECT_ID('tempdb.dbo.#performance_counters') IS NOT NULL
4 DROP TABLE #performance_counters;
5
6 CREATE TABLE #performance_counters (
7 DatetimeChecked DATETIME,
8 instance_name NVARCHAR(128),
9 counter_name NVARCHAR(128),
10 cntr_value BIGINT
11 );
12
13 INSERT INTO #performance_counters (DatetimeChecked,instance_name,count
14 SELECT
15 GETDATE() AS DatetimeChecked,
16 instance_name,
17 counter_name,
18 cntr_value
19 FROM sys.dm_os_performance_counters
20 WHERE counter_name IN ('Write Transactions/sec','Transactions/sec')
21 AND instance_name != '_Total'
22
23 --Wait for 15 seconds then get the deltas
24 WAITFOR DELAY '00:00:15';
25
26 SELECT
27 Groups.name AS AGname,
28 PerSecondDeltas.counter_name,
29 SUM(cntr_delta_per_second) AS Total_per_second
30 FROM
31 (
32 SELECT
33 PerfmonNow.instance_name,
34 PerfmonNow.counter_name,
35 PerfmonNow.cntr_value
36 ,CAST((PerfmonNow.cntr_value - PerfmonSnapShot.cntr_value) * 1.0 / DAT
37 FROM sys.dm_os_performance_counters PerfmonNow
38 INNER JOIN #performance_counters PerfmonSnapShot ON PerfmonNow.instanc
39 AND PerfmonNow.counter_name = PerfmonSnapShot.counter_name
40 WHERE PerfmonNow.counter_name IN ('Write Transactions/sec','Transactio
41 AND PerfmonNow.instance_name != '_Total'
42 ) PerSecondDeltas
43 INNER JOIN sys.availability_databases_cluster AGDatabases ON PerSecond
44 INNER JOIN sys.availability_groups Groups ON AGDatabases.group_id = Gr
45 GROUP BY Groups.name,counter_name
46 ORDER BY
47 Groups.name ASC,
48 counter_name ASC

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

Total Database size in each availability group on this server?

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

Check Availability group health and whether a database is suspended.

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

Set Availability group backup preference.

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;

Thanks for reading.

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"

Published by Adrian Buckman  Feb 19, 2019

https://sqlundercover.wordpress.com/  Uncategorized

 Always On, Database Administration,


 View all posts by Adrian Buckman Query Always on, SQL Query is this
primary, TSQL AG backup preference

 Undercover Catalogue 0.2.1, Fixes An


Issue With The Recording Of Unicode And
MAX Datatype Lengths

 Undercover TV – Sean McCown Joins Us


For a Session on Beginning
Powershell SMO

3 thoughts on “7 more ways to Query Always on


Availability groups”
ADD YOURS

Pingback: 7 ways to Query Always On Availability Groups using SQL – SQL


Undercover

Pingback: Finding the Primary Replica of an Availability Group – SQL


Undercover

Pingback: Happy New Year – Our Top Posts of 2019 – SQL Undercover

Leave a Reply
Enter your comment here...

DAVID FOWLER FOLLOW ME ON TWITTER

David is a DBA with over 14 years production My Tweets


experience of SQL Server, from version 6.5 through
to 2016. He has worked in a number of different
settings and is currently the technical lead at one of
the largest software companies in the UK.
RECENT POSTS FROM THE UNDERCOVER DBAS

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

ARCHIVES Encryption in SQL Server #1 – Column


Level Encryption
» Sep 2021 (1)
Generate a Random Number for Each Row in a Query
» Jul 2021 (2) {a better way}
» Jun 2021 (1) Matt White
» May 2021 (1)
» Mar 2021 (1)
» Feb 2021 (1)
» Dec 2020 (1)
» Nov 2020 (2)
» Sep 2020 (3)
» Aug 2020 (1)
» Jun 2020 (4)
» Apr 2020 (2)
» Mar 2020 (2)
» Feb 2020 (6)
» Jan 2020 (6)
» Dec 2019 (5)
» Nov 2019 (2)
» Oct 2019 (6)
» Sep 2019 (3)
» Aug 2019 (4)
» Jul 2019 (4)
» Jun 2019 (3)
» May 2019 (1)
» Apr 2019 (2)
» Mar 2019 (4)
» Feb 2019 (5)
» Jan 2019 (3)
» Dec 2018 (3)
» Nov 2018 (3)
» Oct 2018 (6)
» Sep 2018 (4)
» Aug 2018 (4)
» Jul 2018 (3)
» Jun 2018 (2)
» May 2018 (2)
» Apr 2018 (2)
» Mar 2018 (5)
» Feb 2018 (7)
» Jan 2018 (7)
» Dec 2017 (3)
» Nov 2017 (5)
» Oct 2017 (6)
» Sep 2017 (10)
» Aug 2017 (6)
» Jul 2017 (8)
» Jun 2017 (10)
» May 2017 (3)
» Jan 2017 (1)

UP ↑

You might also like