sql-server - 如何设置 Azure SQL 自动重建索引?

标签 sql-server azure azure-sql-database azure-sql-server

在本地 SQL 数据库中,通常有一个维护计划来不时重建索引,而此时它并没有被太多使用。

如何在 Azure SQL DB 中进行设置?

P.S:我之前尝试过,但由于我找不到任何选项,我想他们可能会自动执行此操作,直到我阅读 this post并尝试:

SELECT
 DB_NAME() AS DBName
 ,OBJECT_NAME(ps.object_id) AS TableName
 ,i.name AS IndexName
 ,ips.index_type_desc
 ,ips.avg_fragmentation_in_percent
 FROM sys.dm_db_partition_stats ps
 INNER JOIN sys.indexes i
 ON ps.object_id = i.object_id
 AND ps.index_id = i.index_id
 CROSS APPLY sys.dm_db_index_physical_stats(DB_ID(), ps.object_id, ps.index_id, null, 'LIMITED') ips
 ORDER BY ps.object_id, ps.index_id

并发现我有需要维护的索引
enter image description here

最佳答案

您可以使用 Azure 自动化来安排索引维护任务,如下所述:Rebuilding SQL Database indexes using Azure Automation

以下是步骤:

1) 如果您没有任何自动化帐户,请转至 https://portal.azure.com并选择新建 > 管理 > 自动化帐户

enter image description here

2) 创建自动化帐户后,打开详细信息,现在单击 Runbooks > Browse Gallery

enter image description here

在搜索框中键入“索引”一词,然后运行手册“如果 Azure 数据库中的表具有高碎片,则为它们编制索引”:

enter image description here

4) 请注意,Runbook 的作者是 Microsoft 的 SC 自动化产品团队。点击导入:

enter image description here

5) 导入 Runbook 后,现在让我们将数据库凭据添加到 Assets 中。单击 Assets > 凭据,然后单击“添加凭据...”按钮。
enter image description here

6) 设置凭据名称(稍后将在运行手册中使用)、数据库用户名和密码:

enter image description here

7) 现在再次单击 Runbooks,然后从列表中选择“Update-SQLIndexRunbook”,然后单击“Edit...”按钮。您将能够看到将要执行的 PowerShell 脚本:

enter image description here

8) 如果要测试脚本,只需单击“测试 Pane ”按钮,测试窗口就会打开。引入需要的参数,点击开始执行索引重建。如果发生任何错误,则会在结果窗口中记录该错误。请注意,根据数据库和其他参数,这可能需要很长时间才能完成:

enter image description here

9) 现在返回编辑器,单击“发布”按钮启用运行手册。如果我们单击“开始”,则会出现一个窗口,询问参数。但由于我们要安排此任务,我们将点击“安排”按钮:

enter image description here

10) 单击计划链接为运行手册创建一个新计划。我已指定每周一次,但这取决于您的工作量以及您的索引如何随着时间的推移增加其碎片。您将需要根据您的需要并通过在执行之间执行初始查询来调整计划:

enter image description here

11)现在介绍参数和运行设置:

enter image description here

注意:您可以使用具有不同设置的不同时间表,即为特定表设置特定时间表。

这样,你就完成了。请记住根据需要更改日志记录设置:

enter image description here

关于sql-server - 如何设置 Azure SQL 自动重建索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48681024/

相关文章:

c# - Entity Framework 模型-第一个可为空外键

sql - 如何创建硬编码日期参数以在查询中使用?

azure - 无法使用 mcr.microsoft.com/windows/servercore :ltsc when building from azure cloudshell (powershell)

asp.net - Azure 和独立 SQL Server 上的 SqlMembership

c# - 无法将 'int' 隐式转换为 'bool'

sql - 通过 SQL 连接到 azure 会使它崩溃

Azure VM 可用区和负载平衡

Azure函数应用程序: Can I put maxConcurrentSessions settings in Application settings Configuration like service bus configuration

sql - 使用索引器在 Azure 搜索服务中创建集合

azure-sql-database - Sql Azure , 截断和重新设定所有表