Skip to content

Commit f68dff5

Browse files
Merge pull request #48 from microsoft/dev
Improved tips for readable replicas, bug fixes
2 parents d8ca235 + 247077c commit f68dff5

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

sqldb-tips/get-sqldb-tips-compat-level-100-only.sql

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ SELECT 1320 AS tip_id,
523523
'server: ', @@SERVERNAME,
524524
', database: ', DB_NAME(),
525525
', SLO: ', rg.slo_name,
526+
', updateability: ', CAST(DATABASEPROPERTYEX(DB_NAME(), 'Updateability') AS nvarchar(10)),
526527
', logical database GUID: ', rg.logical_database_guid,
527528
', physical database GUID: ', rg.physical_database_guid,
528529
@CRLF, @CRLF,
@@ -1094,6 +1095,8 @@ CROSS JOIN sys.database_scoped_configurations AS dsc
10941095
WHERE iro.state_desc = 'PAUSED'
10951096
AND
10961097
dsc.name = 'PAUSED_RESUMABLE_INDEX_ABORT_DURATION_MINUTES'
1098+
AND
1099+
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
10971100
)
10981101
INSERT INTO @DetectedTip (tip_id, details)
10991102
SELECT 1280 AS tip_id,
@@ -1651,7 +1654,7 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1160) AND execute_indic
16511654
WITH oom AS
16521655
(
16531656
SELECT SUM(duration_ms) / 60000 AS recent_history_duration_minutes,
1654-
SUM(delta_out_of_memory_count) AS count_oom
1657+
SUM(IIF(delta_out_of_memory_count >= 0, delta_out_of_memory_count, 0)) AS count_oom
16551658
FROM sys.dm_resource_governor_resource_pools_history_ex
16561659
WHERE @EngineEdition = 5
16571660
AND
@@ -1685,8 +1688,8 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1165) AND execute_indic
16851688
WITH memgrant AS
16861689
(
16871690
SELECT SUM(duration_ms) / 60000 AS recent_history_duration_minutes,
1688-
SUM(delta_memgrant_waiter_count) AS count_memgrant_waiter,
1689-
SUM(delta_memgrant_timeout_count) AS count_memgrant_timeout
1691+
SUM(IIF(delta_memgrant_waiter_count >= 0, delta_memgrant_waiter_count, 0)) AS count_memgrant_waiter,
1692+
SUM(IIF(delta_memgrant_timeout_count >= 0, delta_memgrant_timeout_count, 0)) AS count_memgrant_timeout
16901693
FROM sys.dm_resource_governor_resource_pools_history_ex
16911694
WHERE @EngineEdition = 5
16921695
AND
@@ -1863,6 +1866,8 @@ WHERE i.type_desc IN ('CLUSTERED','NONCLUSTERED','HEAP')
18631866
AND
18641867
t.is_external = 1
18651868
)
1869+
AND
1870+
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
18661871
),
18671872
partition_compression AS
18681873
(
@@ -2459,24 +2464,29 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1270) AND execute_indic
24592464
BEGIN TRY
24602465

24612466
WITH
2462-
db_allocated_size AS
2467+
db_size AS
24632468
(
2464-
SELECT SUM(CAST(size AS bigint) * 8.) AS db_allocated_size_kb
2469+
SELECT SUM(CAST(FILEPROPERTY(name, 'SpaceUsed') AS bigint) * 8 / 1024. / 1024) AS space_used_gb,
2470+
SUM(CAST(size AS bigint) * 8 / 1024. / 1024) AS space_allocated_gb,
2471+
NULLIF(CAST(DATABASEPROPERTYEX(DB_NAME(), 'MaxSizeInBytes') AS bigint), -1) / 1024. / 1024 / 1024 AS max_size_gb
24652472
FROM sys.database_files
24662473
WHERE type_desc = 'ROWS'
24672474
),
24682475
pvs_db_stats AS
24692476
(
24702477
SELECT pvss.persistent_version_store_size_kb / 1024. / 1024 AS persistent_version_store_size_gb,
24712478
pvss.online_index_version_store_size_kb / 1024. / 1024 AS online_index_version_store_size_gb,
2479+
ds.space_used_gb,
2480+
ds.space_allocated_gb,
2481+
ds.max_size_gb,
24722482
pvss.current_aborted_transaction_count,
24732483
pvss.aborted_version_cleaner_start_time,
24742484
pvss.aborted_version_cleaner_end_time,
24752485
dt.database_transaction_begin_time AS oldest_transaction_begin_time,
24762486
asdt.session_id AS active_transaction_session_id,
24772487
asdt.elapsed_time_seconds AS active_transaction_elapsed_time_seconds
24782488
FROM sys.dm_tran_persistent_version_store_stats AS pvss
2479-
CROSS JOIN db_allocated_size AS das
2489+
CROSS JOIN db_size AS ds
24802490
LEFT JOIN sys.dm_tran_database_transactions AS dt
24812491
ON pvss.oldest_active_transaction_id = dt.transaction_id
24822492
AND
@@ -2488,12 +2498,13 @@ ON pvss.min_transaction_timestamp = asdt.transaction_sequence_num
24882498
WHERE pvss.database_id = DB_ID()
24892499
AND
24902500
(
2491-
persistent_version_store_size_kb > @PVSMinimumSizeThresholdGB * 1024 * 1024 -- PVS is larger than n GB
2501+
pvss.persistent_version_store_size_kb > @PVSMinimumSizeThresholdGB * 1024 * 1024 -- PVS is larger than n GB
24922502
OR
24932503
(
2494-
persistent_version_store_size_kb > @PVSToMaxSizeMinThresholdRatio * das.db_allocated_size_kb -- PVS is larger than n% of database allocated size
2504+
-- compare PVS size to database MAXSIZE, or to allocated size when MAXSIZE is not defined (Hyperscale, Managed Instance)
2505+
pvss.persistent_version_store_size_kb >= @PVSToMaxSizeMinThresholdRatio * COALESCE(ds.max_size_gb, ds.space_allocated_gb) * 1024 * 1024 -- PVS is larger than n% of database max/allocated size
24952506
AND
2496-
persistent_version_store_size_kb > 1048576 -- for small databases, don't consider PVS smaller than 1 GB as large
2507+
pvss.persistent_version_store_size_kb > 1048576 -- don't consider PVS smaller than 1 GB as large
24972508
)
24982509
)
24992510
)
@@ -2503,6 +2514,9 @@ SELECT 1270 AS tip_id,
25032514
@NbspCRLF,
25042515
'PVS size (GB): ', FORMAT(persistent_version_store_size_gb, 'N'), @CRLF,
25052516
'online index version store size (GB): ', FORMAT(online_index_version_store_size_gb, 'N'), @CRLF,
2517+
'used data size (GB): ', FORMAT(space_used_gb, 'N'), @CRLF,
2518+
'allocated data size (GB): ', FORMAT(space_allocated_gb, 'N'), @CRLF,
2519+
'maximum database size (GB): ' + FORMAT(max_size_gb, 'N') + @CRLF, -- omit for Hyperscale and MI as not applicable
25062520
'current aborted transaction count: ', FORMAT(current_aborted_transaction_count, '#,0'), @CRLF,
25072521
'aborted transaction version cleaner start time (UTC): ', ISNULL(CONVERT(varchar(20), aborted_version_cleaner_start_time, 120), '-'), @CRLF,
25082522
'aborted transaction version cleaner end time (UTC): ', ISNULL(CONVERT(varchar(20), aborted_version_cleaner_end_time, 120), '-'), @CRLF,
@@ -2561,6 +2575,8 @@ WHERE data_compression_desc IN ('NONE','ROW','PAGE')
25612575
object_has_columnstore_indexes = 0
25622576
AND
25632577
object_has_columnstore_compressible_partitions = 1
2578+
AND
2579+
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
25642580
),
25652581
table_operational_stats AS -- summarize operational stats for heap, CI, and NCI
25662582
(

sqldb-tips/get-sqldb-tips.sql

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ SELECT 1320 AS tip_id,
512512
'server: ', @@SERVERNAME,
513513
', database: ', DB_NAME(),
514514
', SLO: ', rg.slo_name,
515+
', updateability: ', CAST(DATABASEPROPERTYEX(DB_NAME(), 'Updateability') AS nvarchar(10)),
515516
', logical database GUID: ', rg.logical_database_guid,
516517
', physical database GUID: ', rg.physical_database_guid,
517518
@CRLF, @CRLF,
@@ -1086,6 +1087,8 @@ CROSS JOIN sys.database_scoped_configurations AS dsc
10861087
WHERE iro.state_desc = 'PAUSED'
10871088
AND
10881089
dsc.name = 'PAUSED_RESUMABLE_INDEX_ABORT_DURATION_MINUTES'
1090+
AND
1091+
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
10891092
)
10901093
INSERT INTO @DetectedTip (tip_id, details)
10911094
SELECT 1280 AS tip_id,
@@ -1646,7 +1649,7 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1160) AND execute_indic
16461649
WITH oom AS
16471650
(
16481651
SELECT SUM(duration_ms) / 60000 AS recent_history_duration_minutes,
1649-
SUM(delta_out_of_memory_count) AS count_oom
1652+
SUM(IIF(delta_out_of_memory_count >= 0, delta_out_of_memory_count, 0)) AS count_oom
16501653
FROM sys.dm_resource_governor_resource_pools_history_ex
16511654
WHERE @EngineEdition = 5
16521655
AND
@@ -1680,8 +1683,8 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1165) AND execute_indic
16801683
WITH memgrant AS
16811684
(
16821685
SELECT SUM(duration_ms) / 60000 AS recent_history_duration_minutes,
1683-
SUM(delta_memgrant_waiter_count) AS count_memgrant_waiter,
1684-
SUM(delta_memgrant_timeout_count) AS count_memgrant_timeout
1686+
SUM(IIF(delta_memgrant_waiter_count >= 0, delta_memgrant_waiter_count, 0)) AS count_memgrant_waiter,
1687+
SUM(IIF(delta_memgrant_timeout_count >= 0, delta_memgrant_timeout_count, 0)) AS count_memgrant_timeout
16851688
FROM sys.dm_resource_governor_resource_pools_history_ex
16861689
WHERE @EngineEdition = 5
16871690
AND
@@ -1858,6 +1861,8 @@ WHERE i.type_desc IN ('CLUSTERED','NONCLUSTERED','HEAP')
18581861
AND
18591862
t.is_external = 1
18601863
)
1864+
AND
1865+
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
18611866
),
18621867
partition_compression AS
18631868
(
@@ -2456,24 +2461,29 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1270) AND execute_indic
24562461
BEGIN TRY
24572462

24582463
WITH
2459-
db_allocated_size AS
2464+
db_size AS
24602465
(
2461-
SELECT SUM(CAST(size AS bigint) * 8.) AS db_allocated_size_kb
2466+
SELECT SUM(CAST(FILEPROPERTY(name, 'SpaceUsed') AS bigint) * 8 / 1024. / 1024) AS space_used_gb,
2467+
SUM(CAST(size AS bigint) * 8 / 1024. / 1024) AS space_allocated_gb,
2468+
NULLIF(CAST(DATABASEPROPERTYEX(DB_NAME(), 'MaxSizeInBytes') AS bigint), -1) / 1024. / 1024 / 1024 AS max_size_gb
24622469
FROM sys.database_files
24632470
WHERE type_desc = 'ROWS'
24642471
),
24652472
pvs_db_stats AS
24662473
(
24672474
SELECT pvss.persistent_version_store_size_kb / 1024. / 1024 AS persistent_version_store_size_gb,
24682475
pvss.online_index_version_store_size_kb / 1024. / 1024 AS online_index_version_store_size_gb,
2476+
ds.space_used_gb,
2477+
ds.space_allocated_gb,
2478+
ds.max_size_gb,
24692479
pvss.current_aborted_transaction_count,
24702480
pvss.aborted_version_cleaner_start_time,
24712481
pvss.aborted_version_cleaner_end_time,
24722482
dt.database_transaction_begin_time AS oldest_transaction_begin_time,
24732483
asdt.session_id AS active_transaction_session_id,
24742484
asdt.elapsed_time_seconds AS active_transaction_elapsed_time_seconds
24752485
FROM sys.dm_tran_persistent_version_store_stats AS pvss
2476-
CROSS JOIN db_allocated_size AS das
2486+
CROSS JOIN db_size AS ds
24772487
LEFT JOIN sys.dm_tran_database_transactions AS dt
24782488
ON pvss.oldest_active_transaction_id = dt.transaction_id
24792489
AND
@@ -2485,12 +2495,13 @@ ON pvss.min_transaction_timestamp = asdt.transaction_sequence_num
24852495
WHERE pvss.database_id = DB_ID()
24862496
AND
24872497
(
2488-
persistent_version_store_size_kb > @PVSMinimumSizeThresholdGB * 1024 * 1024 -- PVS is larger than n GB
2498+
pvss.persistent_version_store_size_kb > @PVSMinimumSizeThresholdGB * 1024 * 1024 -- PVS is larger than n GB
24892499
OR
24902500
(
2491-
persistent_version_store_size_kb > @PVSToMaxSizeMinThresholdRatio * das.db_allocated_size_kb -- PVS is larger than n% of database allocated size
2501+
-- compare PVS size to database MAXSIZE, or to allocated size when MAXSIZE is not defined (Hyperscale, Managed Instance)
2502+
pvss.persistent_version_store_size_kb >= @PVSToMaxSizeMinThresholdRatio * COALESCE(ds.max_size_gb, ds.space_allocated_gb) * 1024 * 1024 -- PVS is larger than n% of database max/allocated size
24922503
AND
2493-
persistent_version_store_size_kb > 1048576 -- for small databases, don't consider PVS smaller than 1 GB as large
2504+
pvss.persistent_version_store_size_kb > 1048576 -- don't consider PVS smaller than 1 GB as large
24942505
)
24952506
)
24962507
)
@@ -2500,6 +2511,9 @@ SELECT 1270 AS tip_id,
25002511
@NbspCRLF,
25012512
'PVS size (GB): ', FORMAT(persistent_version_store_size_gb, 'N'), @CRLF,
25022513
'online index version store size (GB): ', FORMAT(online_index_version_store_size_gb, 'N'), @CRLF,
2514+
'used data size (GB): ', FORMAT(space_used_gb, 'N'), @CRLF,
2515+
'allocated data size (GB): ', FORMAT(space_allocated_gb, 'N'), @CRLF,
2516+
'maximum database size (GB): ' + FORMAT(max_size_gb, 'N') + @CRLF, -- omit for Hyperscale and MI as not applicable
25032517
'current aborted transaction count: ', FORMAT(current_aborted_transaction_count, '#,0'), @CRLF,
25042518
'aborted transaction version cleaner start time (UTC): ', ISNULL(CONVERT(varchar(20), aborted_version_cleaner_start_time, 120), '-'), @CRLF,
25052519
'aborted transaction version cleaner end time (UTC): ', ISNULL(CONVERT(varchar(20), aborted_version_cleaner_end_time, 120), '-'), @CRLF,
@@ -2558,6 +2572,8 @@ WHERE data_compression_desc IN ('NONE','ROW','PAGE')
25582572
object_has_columnstore_indexes = 0
25592573
AND
25602574
object_has_columnstore_compressible_partitions = 1
2575+
AND
2576+
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
25612577
),
25622578
table_operational_stats AS -- summarize operational stats for heap, CI, and NCI
25632579
(

0 commit comments

Comments
 (0)