如何通过Bicep激活SQL数据库自动调优的创建和删除索引功能
问题:如何通过Bicep启用SQL数据库自动调优的创建/删除索引功能?
我需要通过Bicep配置SQL数据库的自动调优功能,仅启用创建索引和删除索引选项。我找到了Microsoft.Sql/servers/databases/automaticTuning资源类型,但不清楚如何单独指定这两个功能的启用状态。
现有Bicep代码:
resource sqlServer 'Microsoft.Sql/servers@2022-02-01-preview' = { name: '${appName}-${environment}-sql' location: location properties: { administratorLogin: '${environment}-admin' administratorLoginPassword: sqlPassword minimalTlsVersion: '1.2' restrictOutboundNetworkAccess: 'Disabled' publicNetworkAccess: publicNetworkAccess ? 'Enabled' : 'Disabled' } identity: { type: 'SystemAssigned' } }
resource sqlServer 'Microsoft.Sql/servers@2022-02-01-preview' existing = { name: sqlServerName } resource sqlServerDatabase 'Microsoft.Sql/servers/databases@2022-02-01-preview' = { parent: sqlServer name: '${appName}-${environmentName}-db-${dbName}' location: location sku: environmentSettings[toLower(environmentName)].sku }
解决方案
你可以通过Microsoft.Sql/servers/databases/automaticTuning资源的properties.desiredState属性,单独配置每个自动调优选项的状态。具体配置逻辑:
- 将
createIndex和dropIndex设为Enabled以启用目标功能 - 其他不需要的选项(如
forceLastGoodPlan)可设为Default(继承服务器级配置)或Disabled
完整配置示例
在数据库资源下添加自动调优子资源即可实现需求:
// 引用现有SQL服务器资源 resource sqlServer 'Microsoft.Sql/servers@2022-02-01-preview' existing = { name: sqlServerName } // 创建或引用目标数据库 resource sqlServerDatabase 'Microsoft.Sql/servers/databases@2022-02-01-preview' = { parent: sqlServer name: '${appName}-${environmentName}-db-${dbName}' location: location sku: environmentSettings[toLower(environmentName)].sku } // 配置数据库级自动调优 resource sqlDbAutomaticTuning 'Microsoft.Sql/servers/databases/automaticTuning@2022-02-01-preview' = { parent: sqlServerDatabase name: 'default' properties: { desiredState: { createIndex: 'Enabled' dropIndex: 'Enabled' // 其他选项按需设置,示例设为继承服务器配置 forceLastGoodPlan: 'Default' maintainIndex: 'Default' } } }
关键说明
automaticTuning资源的名称固定为default,这是Azure SQL的官方约定desiredState中的每个选项支持三种状态:Enabled(手动启用)、Disabled(手动禁用)、Default(继承服务器级自动调优配置)- 如果需要批量配置多个数据库,可将自动调优逻辑封装为模块复用
内容的提问来源于stack exchange,提问作者Antoine Brisebois-Roy
相关产品推荐
相关产品推荐

