Skip to content

Commit

Permalink
Merge pull request #48 from microsoft/dev
Browse files Browse the repository at this point in the history
Improved tips for readable replicas, bug fixes
  • Loading branch information
dimitri-furman authored Feb 2, 2021
2 parents d8ca235 + 247077c commit f68dff5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
34 changes: 25 additions & 9 deletions sqldb-tips/get-sqldb-tips-compat-level-100-only.sql
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ SELECT 1320 AS tip_id,
'server: ', @@SERVERNAME,
', database: ', DB_NAME(),
', SLO: ', rg.slo_name,
', updateability: ', CAST(DATABASEPROPERTYEX(DB_NAME(), 'Updateability') AS nvarchar(10)),
', logical database GUID: ', rg.logical_database_guid,
', physical database GUID: ', rg.physical_database_guid,
@CRLF, @CRLF,
Expand Down Expand Up @@ -1094,6 +1095,8 @@ CROSS JOIN sys.database_scoped_configurations AS dsc
WHERE iro.state_desc = 'PAUSED'
AND
dsc.name = 'PAUSED_RESUMABLE_INDEX_ABORT_DURATION_MINUTES'
AND
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
)
INSERT INTO @DetectedTip (tip_id, details)
SELECT 1280 AS tip_id,
Expand Down Expand Up @@ -1651,7 +1654,7 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1160) AND execute_indic
WITH oom AS
(
SELECT SUM(duration_ms) / 60000 AS recent_history_duration_minutes,
SUM(delta_out_of_memory_count) AS count_oom
SUM(IIF(delta_out_of_memory_count >= 0, delta_out_of_memory_count, 0)) AS count_oom
FROM sys.dm_resource_governor_resource_pools_history_ex
WHERE @EngineEdition = 5
AND
Expand Down Expand Up @@ -1685,8 +1688,8 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1165) AND execute_indic
WITH memgrant AS
(
SELECT SUM(duration_ms) / 60000 AS recent_history_duration_minutes,
SUM(delta_memgrant_waiter_count) AS count_memgrant_waiter,
SUM(delta_memgrant_timeout_count) AS count_memgrant_timeout
SUM(IIF(delta_memgrant_waiter_count >= 0, delta_memgrant_waiter_count, 0)) AS count_memgrant_waiter,
SUM(IIF(delta_memgrant_timeout_count >= 0, delta_memgrant_timeout_count, 0)) AS count_memgrant_timeout
FROM sys.dm_resource_governor_resource_pools_history_ex
WHERE @EngineEdition = 5
AND
Expand Down Expand Up @@ -1863,6 +1866,8 @@ WHERE i.type_desc IN ('CLUSTERED','NONCLUSTERED','HEAP')
AND
t.is_external = 1
)
AND
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
),
partition_compression AS
(
Expand Down Expand Up @@ -2459,24 +2464,29 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1270) AND execute_indic
BEGIN TRY

WITH
db_allocated_size AS
db_size AS
(
SELECT SUM(CAST(size AS bigint) * 8.) AS db_allocated_size_kb
SELECT SUM(CAST(FILEPROPERTY(name, 'SpaceUsed') AS bigint) * 8 / 1024. / 1024) AS space_used_gb,
SUM(CAST(size AS bigint) * 8 / 1024. / 1024) AS space_allocated_gb,
NULLIF(CAST(DATABASEPROPERTYEX(DB_NAME(), 'MaxSizeInBytes') AS bigint), -1) / 1024. / 1024 / 1024 AS max_size_gb
FROM sys.database_files
WHERE type_desc = 'ROWS'
),
pvs_db_stats AS
(
SELECT pvss.persistent_version_store_size_kb / 1024. / 1024 AS persistent_version_store_size_gb,
pvss.online_index_version_store_size_kb / 1024. / 1024 AS online_index_version_store_size_gb,
ds.space_used_gb,
ds.space_allocated_gb,
ds.max_size_gb,
pvss.current_aborted_transaction_count,
pvss.aborted_version_cleaner_start_time,
pvss.aborted_version_cleaner_end_time,
dt.database_transaction_begin_time AS oldest_transaction_begin_time,
asdt.session_id AS active_transaction_session_id,
asdt.elapsed_time_seconds AS active_transaction_elapsed_time_seconds
FROM sys.dm_tran_persistent_version_store_stats AS pvss
CROSS JOIN db_allocated_size AS das
CROSS JOIN db_size AS ds
LEFT JOIN sys.dm_tran_database_transactions AS dt
ON pvss.oldest_active_transaction_id = dt.transaction_id
AND
Expand All @@ -2488,12 +2498,13 @@ ON pvss.min_transaction_timestamp = asdt.transaction_sequence_num
WHERE pvss.database_id = DB_ID()
AND
(
persistent_version_store_size_kb > @PVSMinimumSizeThresholdGB * 1024 * 1024 -- PVS is larger than n GB
pvss.persistent_version_store_size_kb > @PVSMinimumSizeThresholdGB * 1024 * 1024 -- PVS is larger than n GB
OR
(
persistent_version_store_size_kb > @PVSToMaxSizeMinThresholdRatio * das.db_allocated_size_kb -- PVS is larger than n% of database allocated size
-- compare PVS size to database MAXSIZE, or to allocated size when MAXSIZE is not defined (Hyperscale, Managed Instance)
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
AND
persistent_version_store_size_kb > 1048576 -- for small databases, don't consider PVS smaller than 1 GB as large
pvss.persistent_version_store_size_kb > 1048576 -- don't consider PVS smaller than 1 GB as large
)
)
)
Expand All @@ -2503,6 +2514,9 @@ SELECT 1270 AS tip_id,
@NbspCRLF,
'PVS size (GB): ', FORMAT(persistent_version_store_size_gb, 'N'), @CRLF,
'online index version store size (GB): ', FORMAT(online_index_version_store_size_gb, 'N'), @CRLF,
'used data size (GB): ', FORMAT(space_used_gb, 'N'), @CRLF,
'allocated data size (GB): ', FORMAT(space_allocated_gb, 'N'), @CRLF,
'maximum database size (GB): ' + FORMAT(max_size_gb, 'N') + @CRLF, -- omit for Hyperscale and MI as not applicable
'current aborted transaction count: ', FORMAT(current_aborted_transaction_count, '#,0'), @CRLF,
'aborted transaction version cleaner start time (UTC): ', ISNULL(CONVERT(varchar(20), aborted_version_cleaner_start_time, 120), '-'), @CRLF,
'aborted transaction version cleaner end time (UTC): ', ISNULL(CONVERT(varchar(20), aborted_version_cleaner_end_time, 120), '-'), @CRLF,
Expand Down Expand Up @@ -2561,6 +2575,8 @@ WHERE data_compression_desc IN ('NONE','ROW','PAGE')
object_has_columnstore_indexes = 0
AND
object_has_columnstore_compressible_partitions = 1
AND
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
),
table_operational_stats AS -- summarize operational stats for heap, CI, and NCI
(
Expand Down
34 changes: 25 additions & 9 deletions sqldb-tips/get-sqldb-tips.sql
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ SELECT 1320 AS tip_id,
'server: ', @@SERVERNAME,
', database: ', DB_NAME(),
', SLO: ', rg.slo_name,
', updateability: ', CAST(DATABASEPROPERTYEX(DB_NAME(), 'Updateability') AS nvarchar(10)),
', logical database GUID: ', rg.logical_database_guid,
', physical database GUID: ', rg.physical_database_guid,
@CRLF, @CRLF,
Expand Down Expand Up @@ -1086,6 +1087,8 @@ CROSS JOIN sys.database_scoped_configurations AS dsc
WHERE iro.state_desc = 'PAUSED'
AND
dsc.name = 'PAUSED_RESUMABLE_INDEX_ABORT_DURATION_MINUTES'
AND
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
)
INSERT INTO @DetectedTip (tip_id, details)
SELECT 1280 AS tip_id,
Expand Down Expand Up @@ -1646,7 +1649,7 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1160) AND execute_indic
WITH oom AS
(
SELECT SUM(duration_ms) / 60000 AS recent_history_duration_minutes,
SUM(delta_out_of_memory_count) AS count_oom
SUM(IIF(delta_out_of_memory_count >= 0, delta_out_of_memory_count, 0)) AS count_oom
FROM sys.dm_resource_governor_resource_pools_history_ex
WHERE @EngineEdition = 5
AND
Expand Down Expand Up @@ -1680,8 +1683,8 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1165) AND execute_indic
WITH memgrant AS
(
SELECT SUM(duration_ms) / 60000 AS recent_history_duration_minutes,
SUM(delta_memgrant_waiter_count) AS count_memgrant_waiter,
SUM(delta_memgrant_timeout_count) AS count_memgrant_timeout
SUM(IIF(delta_memgrant_waiter_count >= 0, delta_memgrant_waiter_count, 0)) AS count_memgrant_waiter,
SUM(IIF(delta_memgrant_timeout_count >= 0, delta_memgrant_timeout_count, 0)) AS count_memgrant_timeout
FROM sys.dm_resource_governor_resource_pools_history_ex
WHERE @EngineEdition = 5
AND
Expand Down Expand Up @@ -1858,6 +1861,8 @@ WHERE i.type_desc IN ('CLUSTERED','NONCLUSTERED','HEAP')
AND
t.is_external = 1
)
AND
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
),
partition_compression AS
(
Expand Down Expand Up @@ -2456,24 +2461,29 @@ IF EXISTS (SELECT 1 FROM @TipDefinition WHERE tip_id IN (1270) AND execute_indic
BEGIN TRY

WITH
db_allocated_size AS
db_size AS
(
SELECT SUM(CAST(size AS bigint) * 8.) AS db_allocated_size_kb
SELECT SUM(CAST(FILEPROPERTY(name, 'SpaceUsed') AS bigint) * 8 / 1024. / 1024) AS space_used_gb,
SUM(CAST(size AS bigint) * 8 / 1024. / 1024) AS space_allocated_gb,
NULLIF(CAST(DATABASEPROPERTYEX(DB_NAME(), 'MaxSizeInBytes') AS bigint), -1) / 1024. / 1024 / 1024 AS max_size_gb
FROM sys.database_files
WHERE type_desc = 'ROWS'
),
pvs_db_stats AS
(
SELECT pvss.persistent_version_store_size_kb / 1024. / 1024 AS persistent_version_store_size_gb,
pvss.online_index_version_store_size_kb / 1024. / 1024 AS online_index_version_store_size_gb,
ds.space_used_gb,
ds.space_allocated_gb,
ds.max_size_gb,
pvss.current_aborted_transaction_count,
pvss.aborted_version_cleaner_start_time,
pvss.aborted_version_cleaner_end_time,
dt.database_transaction_begin_time AS oldest_transaction_begin_time,
asdt.session_id AS active_transaction_session_id,
asdt.elapsed_time_seconds AS active_transaction_elapsed_time_seconds
FROM sys.dm_tran_persistent_version_store_stats AS pvss
CROSS JOIN db_allocated_size AS das
CROSS JOIN db_size AS ds
LEFT JOIN sys.dm_tran_database_transactions AS dt
ON pvss.oldest_active_transaction_id = dt.transaction_id
AND
Expand All @@ -2485,12 +2495,13 @@ ON pvss.min_transaction_timestamp = asdt.transaction_sequence_num
WHERE pvss.database_id = DB_ID()
AND
(
persistent_version_store_size_kb > @PVSMinimumSizeThresholdGB * 1024 * 1024 -- PVS is larger than n GB
pvss.persistent_version_store_size_kb > @PVSMinimumSizeThresholdGB * 1024 * 1024 -- PVS is larger than n GB
OR
(
persistent_version_store_size_kb > @PVSToMaxSizeMinThresholdRatio * das.db_allocated_size_kb -- PVS is larger than n% of database allocated size
-- compare PVS size to database MAXSIZE, or to allocated size when MAXSIZE is not defined (Hyperscale, Managed Instance)
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
AND
persistent_version_store_size_kb > 1048576 -- for small databases, don't consider PVS smaller than 1 GB as large
pvss.persistent_version_store_size_kb > 1048576 -- don't consider PVS smaller than 1 GB as large
)
)
)
Expand All @@ -2500,6 +2511,9 @@ SELECT 1270 AS tip_id,
@NbspCRLF,
'PVS size (GB): ', FORMAT(persistent_version_store_size_gb, 'N'), @CRLF,
'online index version store size (GB): ', FORMAT(online_index_version_store_size_gb, 'N'), @CRLF,
'used data size (GB): ', FORMAT(space_used_gb, 'N'), @CRLF,
'allocated data size (GB): ', FORMAT(space_allocated_gb, 'N'), @CRLF,
'maximum database size (GB): ' + FORMAT(max_size_gb, 'N') + @CRLF, -- omit for Hyperscale and MI as not applicable
'current aborted transaction count: ', FORMAT(current_aborted_transaction_count, '#,0'), @CRLF,
'aborted transaction version cleaner start time (UTC): ', ISNULL(CONVERT(varchar(20), aborted_version_cleaner_start_time, 120), '-'), @CRLF,
'aborted transaction version cleaner end time (UTC): ', ISNULL(CONVERT(varchar(20), aborted_version_cleaner_end_time, 120), '-'), @CRLF,
Expand Down Expand Up @@ -2558,6 +2572,8 @@ WHERE data_compression_desc IN ('NONE','ROW','PAGE')
object_has_columnstore_indexes = 0
AND
object_has_columnstore_compressible_partitions = 1
AND
DATABASEPROPERTYEX(DB_NAME(), 'Updateability') = 'READ_WRITE' -- only produce this on primary
),
table_operational_stats AS -- summarize operational stats for heap, CI, and NCI
(
Expand Down

0 comments on commit f68dff5

Please sign in to comment.