可以通过 az postgres flexible-server index-tuning list-recommendations 命令列出通过现有服务器中的索引优化生成的索引优化建议。
若要列出所有 CREATE INDEX 建议,请使用以下命令:
az postgres flexible-server index-tuning list-recommendations \
  --resource-group <resource_group> \
  --server-name <server> \
  --recommendation-type createindex
该命令返回有关索引调优生成的 CREATE INDEX 建议的所有信息,并显示类似于以下输出的内容:
[
  {
    "analyzedWorkload": {
      "endTime": "2025-02-26T14:40:18.788628+00:00",
      "queryCount": 18,
      "startTime": "2025-02-26T13:40:18.788628+00:00"
    },
    "details": {
      "databaseName": "<database>",
      "includedColumns": "",
      "indexColumns": "\"<table>\".\"<column>\"",
      "indexName": "<index>",
      "indexType": "BTREE",
      "schema": "<schema>",
      "table": "<table>"
    },
    "estimatedImpact": [
      {
        "absoluteValue": 0.3984375,
        "dimensionName": "IndexSize",
        "queryId": null,
        "unit": "MB"
      },
      {
        "absoluteValue": 62.86969111969111,
        "dimensionName": "QueryCostImprovement",
        "queryId": -555955670159268890,
        "unit": "Percentage"
      }
    ],
    "id": "/subscriptions/<subscription_id>/resourceGroups/<resource_group>/providers/Microsoft.DBforPostgreSQL/flexibleServers/<server>/tuningOptions/index/recommendations/<recommendation_id>",
    "implementationDetails": {
      "method": "SQL",
      "script": "create index concurrently <index> on <schema>.<table>(<column>)"
    },
    "improvedQueryIds": [
      -555955670159268890
    ],
    "initialRecommendedTime": "2025-02-26T14:40:19.707617+00:00",
    "kind": "",
    "lastRecommendedTime": "2025-02-26T14:40:19.707617+00:00",
    "name": "CreateIndex_<database>_<schema>_<column>_idx",
    "recommendationReason": "Column \"<table>\".\"<column>\" appear in Equal Predicate clause(s) in query -555955670159268890;",
    "recommendationType": "CreateIndex",
    "resourceGroup": "<resource_group>",
    "systemData": null,
    "timesRecommended": 1,
    "type": "Microsoft.DBforPostgreSQL/flexibleServers/tuningOptions/index"
  },
  {
    .
    .
    .
  }
]
若要列出所有 DROP INDEX 建议,请使用以下命令:
az postgres flexible-server index-tuning list-recommendations \
  --resource-group <resource_group> \
  --server-name <server> \
  --recommendation-type dropindex
该命令返回关于索引优化生成的 DROP INDEX 建议的所有信息,显示如下所示的输出:
[
  {
    "analyzedWorkload": {
      "endTime": "2025-02-26T19:02:47.522193+00:00",
      "queryCount": 0,
      "startTime": "2025-01-22T19:02:47.522193+00:00"
    },
    "details": {
      "databaseName": "<database>",
      "includedColumns": "",
      "indexColumns": "<column>",
      "indexName": "<index>",
      "indexType": "BTREE",
      "schema": "<schema>",
      "table": "<table>"
    },
    "estimatedImpact": [
      {
        "absoluteValue": 35.0,
        "dimensionName": "Benefit",
        "queryId": null,
        "unit": "Percentage"
      },
      {
        "absoluteValue": 31.28125,
        "dimensionName": "IndexSize",
        "queryId": null,
        "unit": "MB"
      }
    ],
    "id": "/subscriptions/<subscription_id>/resourceGroups/<resource_group>/providers/Microsoft.DBforPostgreSQL/flexibleServers/<server>/tuningOptions/index/recommendations/<recommendation_id>",
    "implementationDetails": {
      "method": "SQL",
      "script": "drop index concurrently \"<schema>\".\"<index>\";"
    },
    "improvedQueryIds": null,
    "initialRecommendedTime": "2025-02-26T19:02:47.556792+00:00",
    "kind": "",
    "lastRecommendedTime": "2025-02-26T19:02:47.556792+00:00",
    "name": "DropIndex_<database>_<sechema>_<index>",
    "recommendationReason": "Duplicate of \"<index>\". The equivalent index \"<index>\" has a shorter length compared to \"<index>\".",
    "recommendationType": "DropIndex",
    "resourceGroup": "<resource_group>",
    "systemData": null,
    "timesRecommended": 1,
    "type": "Microsoft.DBforPostgreSQL/flexibleServers/tuningOptions/index"
  }
]
使用您偏好的任何 PostgreSQL 客户端工具:
使用有权连接到实例的任何角色,连接到服务器中可用的 azure_sys 数据库。 
              public 角色的成员可以从这些视图中读取。
 
在 sessions 视图上执行查询,以检索有关建议会话的详细信息。
 
在 recommendations 视图上执行查询,以检索由索引调优生成的关于 CREATE INDEX 和 DROP INDEX 的建议。
 
浏览量
在 azure_sys 数据库中,视图提供了一种便捷的方法,用于访问和获取由索引调整生成的索引建议。 具体而言,createindexrecommendations 和 dropindexrecommendations 视图包含有关 CREATE INDEX 和 DROP INDEX 建议的详细信息。 这些视图公开了会话 ID、数据库名称、顾问类型、优化会话的启动和停止时间、建议 ID、建议类型、建议原因和其他相关详细信息等数据。 用户可以查询这些视图,以便轻松访问和分析索引优化生成的索引建议。
              sessions 视图展示所有索引调优会话的所有详细信息。
| 列名 | 
数据类型 | 
DESCRIPTION | 
| 会话ID (session_id) | 
UUID(通用唯一识别码) | 
分配给每次启动的优化会话的全局唯一标识符。 | 
| 数据库名称 | 
varchar(64) | 
执行索引优化会话的上下文所属的数据库名称。 | 
| 会话类型 | 
智能性能.推荐类型 | 
指示此索引调优会话可能会生成的建议类型。 可能的值有:CreateIndex、DropIndex。 
              CreateIndex 类型的会话可以生成 CreateIndex 类型的建议。 
              DropIndex 类型的会话可以生成 DropIndex 或 ReIndex 类型的建议。 | 
| 运行类型 | 
智能性能.推荐运行类型 | 
指示此会话的启动方式。 可能的值为:Scheduled 根据 index_tuning.analysis_interval 的值自动执行的会话分配有 Scheduled 的运行类型。 | 
| 状态 | 
智能性能.推荐状态 | 
指示会话的当前状态。 可能的值有:Error、Success、InProgress。 执行失败的会话设置为 Error。 正确完成执行的会话,无论是否生成了建议,都设置为 Success。 仍在执行的会话设置为 InProgress。 | 
| 开始时间 | 
不带时区的时间戳 | 
生成此建议的调优会话启动时的时间戳。 | 
| 停止时间 | 
不带时区的时间戳 | 
生成此建议的调优会话启动时的时间戳。 如果会话正在进行或由于某些故障而中止,则为 NULL。 | 
| 推荐数量 | 
整数 | 
此会话中生成的建议总数。 | 
              recommendations 视图展示了在基础表中仍有可用数据的任何优化会话所生成的所有建议的详细信息。
| 列名 | 
数据类型 | 
DESCRIPTION | 
| 推荐编号 | 
整数 | 
在整个服务器中唯一标识一条建议的数字。 | 
| 最后已知会话ID (last_known_session_id) | 
UUID(通用唯一识别码) | 
每个索引优化会话都分配有一个全局唯一标识符。 此列中的值表示最近生成此建议的会话的值。 | 
| 数据库名称 | 
varchar(64) | 
在其上下文中生成建议的数据库的名称。 | 
| 推荐类型 | 
智能性能.推荐类型 | 
指示生成的建议的类型。 可能的值有:CreateIndex、DropIndex、ReIndex。 | 
| 初始推荐时间 | 
不带时区的时间戳 | 
生成此建议的调优会话启动时的时间戳。 | 
| 最后推荐时间 | 
不带时区的时间戳 | 
生成此建议的调优会话启动时的时间戳。 | 
| 推荐次数 | 
整数 | 
生成此建议的调优会话启动时的时间戳。 | 
| 原因 | 
文本消息 | 
证明生成此建议的原因的理由。 | 
| 推荐上下文 | 
json | 
包含受建议影响的查询的查询标识符列表、建议的索引类型、建议的架构名称和建议索引所在的表的名称、索引列、索引名称以及建议索引的估计大小(以字节为单位)。 | 
创建索引建议的原因
当索引优化建议创建索引时,它会添加以下至少一个原因:
| 原因 | 
Column <column> appear in Join On clause(s) in query <queryId> | 
Column <column> appear in Equal Predicate clause(s) in query <queryId> | 
Column <column> appear in Non-Equal Predicate clause(s) in query <queryId> | 
Column <column> appear in Group By clause(s) in query <queryId> | 
Column <column> appear in Order By clause(s) in query <queryId> | 
删除索引建议的原因
当索引优化识别到任何标记为无效的索引时,它会基于以下原因建议删除该索引:
The index is invalid and the recommended recovery method is to reindex.
要了解有关索引标记为无效的原因和时间的更多信息,请参阅 PostgreSQL 官方文档中的 REINDEX。
删除索引建议的原因
当索引优化检测到一个至少在 index_tuning.unused_min_period 中设置的天数内未被使用的索引时,它会基于以下原因建议删除该索引:
The index is unused in the past <days_unused> days.
当索引优化检测到重复索引时,其中一个重复索引会保留,并且它会建议删除其余的重复索引。 所提供的原因总是有如下起始文本:
Duplicate of <surviving_duplicate>.
后跟另一个文本,其中解释了选择每个重复项的原因以供删除:
| 原因 | 
The equivalent index "<surviving_duplicate>" is a Primary key, while "<droppable_duplicate>" is not. | 
The equivalent index "<surviving_duplicate>" is a unique index, while "<droppable_duplicate>" is not. | 
The equivalent index "<surviving_duplicate>" is a constraint, while "<droppable_duplicate>" is not. | 
The equivalent index "<surviving_duplicate>" is a valid index, while "<droppable_duplicate>" is not. | 
The equivalent index "<surviving_duplicate>" has been chosen as replica identity, while "<droppable_duplicate>" is not. | 
The equivalent index "<surviving_duplicate>" was used to cluster the table, while "<droppable_duplicate>" was not. | 
The equivalent index "<surviving_duplicate>" has a smaller estimated size compared to "<droppable_duplicate>". | 
The equivalent index "<surviving_duplicate>" has more tuples compared to "<droppable_duplicate>". | 
The equivalent index "<surviving_duplicate>" has more index scans compared to "<droppable_duplicate>". | 
The equivalent index "<surviving_duplicate>" has been fetched more times compared to "<droppable_duplicate>". | 
The equivalent index "<surviving_duplicate>" has been read more times compared to "<droppable_duplicate>". | 
The equivalent index "<surviving_duplicate>" has a shorter length compared to "<droppable_duplicate>". | 
The equivalent index "<surviving_duplicate>" has a smaller oid compared to "<droppable_duplicate>". | 
如果索引不仅由于重复而可删除,而且至少在 index_tuning.unused_min_period 中设置的天数内未被使用,那么以下文本会附加到原因之后:
Also, the index is unused in the past <days_unused> days.