如何检查 Azure SQL 数据库上次自动暂停的时间以及持续时间?

huangapple go评论85阅读模式
英文:

How to check last time an Azure SQL Database went on Auto-Pause and for how long?

问题

我正在测试带有Serverless功能的Azure SQL数据库。

我想了解SQL数据库昨天是否进入了自动暂停模式,以及持续了多长时间。

我请ChatGPT给我一个关于可以使用的代码的提示,它建议我使用PowerShell:

az sql db show -g $resourceGroupName -s $serverName -n $databaseName --query "automaticPauseTime"

但是它返回了错误:

(ResourceGroupNotFound) Resource group 'MyResourceGroup' could not be found.
Code: ResourceGroupNotFound
Message: Resource group 'MyResourceGroup' could not be found.

即使我输入了正确的资源组名称。

所以我尝试了T-SQL:

USE master;

DECLARE @databaseName NVARCHAR(128) = 'YourDatabaseName'; -- Replace with your database name
DECLARE @lastConnectionTime DATETIME;

-- Get the last connection time from the sys.dm_exec_sessions DMV
SELECT @lastConnectionTime = MAX(login_time)
FROM sys.dm_exec_sessions
WHERE database_id = DB_ID(@databaseName);

IF @lastConnectionTime IS NULL
BEGIN
    PRINT 'The database has never been connected since the last restart or creation.';
END
ELSE
BEGIN
    PRINT 'The database last had a connection at: ' + CONVERT(NVARCHAR, @lastConnectionTime);
END

它返回了The database has never been connected since the last restart or creation.,这是错误的,因为我现在正在连接它。

那么我该如何检查Azure SQL数据库上次进入自动暂停的时间以及持续时间?

英文:

I'm testing Azure SQL Database with the Serverless feature.

I would like to understand if the SQL Database went into Auto-Pause mode yesterday and for how long.

I asked to ChatGPT to give me a hint about what code I can use and it suggested PowerShell:

az sql db show -g $resourceGroupName -s $serverName -n $databaseName --query "automaticPauseTime"

But it's returning the error:

(ResourceGroupNotFound) Resource group 'MyResourceGroup' could not be found.
Code: ResourceGroupNotFound
Message: Resource group 'MyResourceGroup' could not be found.

Even if I put the right Resource Group name.

So I tried with T-SQL:

USE master;

DECLARE @databaseName NVARCHAR(128) = 'YourDatabaseName'; -- Replace with your database name
DECLARE @lastConnectionTime DATETIME;

-- Get the last connection time from the sys.dm_exec_sessions DMV
SELECT @lastConnectionTime = MAX(login_time)
FROM sys.dm_exec_sessions
WHERE database_id = DB_ID(@databaseName);

IF @lastConnectionTime IS NULL
BEGIN
    PRINT 'The database has never been connected since the last restart or creation.';
END
ELSE
BEGIN
    PRINT 'The database last had a connection at: ' + CONVERT(NVARCHAR, @lastConnectionTime);
END

Which returns The database has never been connected since the last restart or creation. which is a lie because I'm connected to it right now.

So how can I check the last time an Azure SQL Database went on Auto-Pause and for how long?

答案1

得分: 1

我找到的唯一解决方案是使用 Azure 门户中的指标:成功连接计数

否则,如果你在服务器级别启用了审计,你可以针对存储帐户查询日志,如下所示:

SELECT *
FROM sys.fn_get_audit_file('https://MyDomain.blob.core.windows.net/sqldbauditlogs/weu-cust-mssql-srvr/MyDatabase/SqlDbAuditing_ServerAudit/2023-07-30/', DEFAULT, DEFAULT)

如何检查 Azure SQL 数据库上次自动暂停的时间以及持续时间?

英文:

The only solution I found was to use the Metrics from the Azure Portal: Successful Connections Count

如何检查 Azure SQL 数据库上次自动暂停的时间以及持续时间?

Otherwise if you have the audit enabled at server level you can target the Strage Account and query the logs this way:

SELECT *
FROM sys.fn_get_audit_file('https://MyDomain.blob.core.windows.net/sqldbauditlogs/weu-cust-mssql-srvr/MyDatabase/SqlDbAuditing_ServerAudit/2023-07-30/', DEFAULT, DEFAULT)

答案2

得分: 1

我有同样的问题,但后来我注意到了活动日志。可以深入了解数据库的更改详细信息。

在Azure门户中,转到数据库 > 活动日志 > 选择一个事件(暂停和恢复都是事件,我发现的)。查找Properties.Status、properties.pausedDate、properties.ResumedDAte或类似的内容,以获取最后一次暂停或恢复的时间。

这些详细信息是记录在Azure自身的数据库配置中的更改活动。如果您没有权限查看已更改的历史和值的信息,可能无法查看该信息。

英文:

<p>I had the same question, but then I noticed Activity Log. It's possible to drill into the change details for the DB. </p>
<p>in the Azure Portal, go to the DB > Activity Log > choose an event (pausing & resuming are events, I found out). look for Properties.Status, properties.pausedDate, properties.ResumedDAte or something similar to get the last time paused or resumed.</p> <p>These details are change activities logged to Azure itself in the DB config. if you don't have permissions to see what history and values that have changed, you may not have the ability to see that information.</p>

huangapple
  • 本文由 发表于 2023年7月31日 18:13:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定