您可以透過 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 建議的詳細資訊。 這些檢視會公開工作階段識別碼、資料庫名稱、建議程式類型、微調工作階段的啟動和停止時間、建議識別碼、建議類型、建議原因,以及其他相關詳細資料等資料。 用戶可以查詢這些檢視,以輕鬆存取和分析索引微調所產生的索引建議。
sessions 檢視會公開所有索引微調工作階段的所有詳細資料。
| 欄位名稱 |
資料類型 |
描述 |
| session_id |
全域唯一識別碼 (UUID) |
全域唯一識別碼會指派給已起始的每個新微調工作階段。 |
| 資料庫名稱 |
varchar(64) |
在其背景下執行索引微調會話的資料庫名稱。 |
| session_type |
intelligentperformance.recommendation_type |
指出此索引微調工作階段可能產生的建議類型。 可能的值為: CreateIndex、 DropIndex。
CreateIndex類型的會話可能會產生CreateIndex類型的建議。
DropIndex 類型的工作階段可以產生 DropIndex 或 ReIndex 類型的建議。 |
| 執行類型 |
intelligentperformance.recommendation_run_type |
指出此工作階段起始的方式。 可能的值為:Scheduled。 根據 index_tuning.analysis_interval 的值自動執行的工作階段會被指派 Scheduled 的執行類型。 |
| 狀態 |
intelligentperformance.recommendation_state |
指示工作階段的目前狀態。 可能的值為:Error、、SuccessInProgress。 執行失敗的工作階段會設定為 Error。 無論是否產生建議,已正確完成其執行的工作階段都會設定為 Success。 仍在執行的工作階段會設定為 InProgress。 |
| 開始時間 |
不含時區的時間戳記 |
生成此建議的調整會話開始的時間戳記。 |
| stop_time |
不含時區的時間戳記 |
生成此建議的調整會話開始的時間戳記。 如果工作階段正在進行或因某些失敗而中止,則為 NULL。 |
| 推薦數量 |
整數 |
此工作階段中產生的建議總數。 |
對於資料仍在基礎資料表上提供的任何微調工作階段上產生的所有建議,recommendations 檢視會公開其所有詳細資料。
| 欄位名稱 |
資料類型 |
描述 |
| recommendation_id(推薦識別碼) |
整數 |
整個伺服器中可唯一識別建議的數字。 |
| 最後已知的會話識別碼 |
全域唯一識別碼 (UUID) |
每個索引微調工作階段都會被指派全域唯一識別碼。 此欄位中的值代表最近一次生成此推薦結果的會話的值。 |
| 資料庫名稱 |
varchar(64) |
產生建議的資料庫名稱。 |
| 建議類型 |
intelligentperformance.recommendation_type |
指出產生的建議類型。 可能的值為:CreateIndex、、DropIndexReIndex。 |
| 初始推薦時間 |
不含時區的時間戳記 |
生成此建議的調整會話開始的時間戳記。 |
| 最後建議時間 |
不含時區的時間戳記 |
生成此建議的調整會話開始的時間戳記。 |
| times_recommended |
整數 |
生成此建議的調整會話開始的時間戳記。 |
| 原因 |
收發簡訊 |
說明產生此建議的原因。 |
| 推薦情境 |
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.