

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 分析 Amazon DocumentDB 操作
<a name="profiling"></a>

您可以使用 Amazon DocumentDB 中的分析器 （與 MongoDB 相容） 來記錄叢集上執行的操作的執行時間和詳細資訊。Profiler 適用於監控叢集上最慢的操作，以協助您改善個別查詢效能和整體叢集效能。

根據預設會停用 Profiler 功能。啟用時，分析器會將花費超過客戶定義閾值 （例如 100 毫秒） 的操作記錄到 Amazon CloudWatch Logs。記錄的詳細資訊包括已分析的命令、時間、計劃摘要和用戶端中繼資料。操作記錄到 CloudWatch Logs 之後，您可以使用 CloudWatch Logs Insights 來分析、監控和封存 Amazon DocumentDB 分析資料。[常見查詢](#profiling.common-queries)小節提供常用的查詢。

啟用時，Profiler 會使用叢集的額外資源。我們建議您從高閾值 (例如 500 毫秒) 開始，再逐漸降低該值，以識別慢速操作。對於高輸送量應用程式，從閾值 50 毫秒開始可能會在叢集上造成效能問題。分析器會在叢集層級啟用，並在叢集中的所有執行個體和資料庫上運作。Amazon DocumentDB 會盡力將操作記錄到 Amazon CloudWatch Logs。

雖然 Amazon DocumentDB 不收取額外費用來啟用分析器，但您需要支付使用 CloudWatch Logs 的標準費率。如需 CloudWatch Logs 定價的資訊，請參閱 [Amazon CloudWatch 定價](https://aws.amazon.com/cloudwatch/pricing/)。

**Topics**
+ [受支援的操作](#profiling.supported-commands)
+ [限制](#profiling.limitations)
+ [啟用分析器](#profiling.enable-profiling)
+ [停用分析器](#profiling.disable-profiling)
+ [停用分析器日誌匯出](#profiling.disabling-logs-export)
+ [存取您的分析器日誌](#profiling.accessing)
+ [常見查詢](#profiling.common-queries)

## 受支援的操作
<a name="profiling.supported-commands"></a>

Amazon DocumentDB Profiler 支援下列操作：
+ `aggregate`
+ `count`
+ `delete`
+ `distinct`
+ `find` (OP\$1QUERY 和命令)
+ `findAndModify`
+ `insert`
+ `update`

## 限制
<a name="profiling.limitations"></a>

慢查詢分析器只有在查詢的整個結果集能夠容納在一個批次中，且結果集低於 16MB （最大 BSON 大小） 時，才能發出分析器日誌。大於 16MB 的結果集會自動分割成多個批次。

大多數驅動程式或 Shell 可能會設定較小的預設批次大小。您可以在查詢中指定批次大小。為了擷取慢查詢日誌，我們建議批次大小超過預期結果集的大小。如果您不確定結果集大小，或是大小不同，也可以將批次大小設定為較大的數字 （例如 100k)。

不過，使用較大的批次大小表示在將回應傳送到用戶端之前，必須從資料庫擷取更多結果。對於某些查詢，這可能會在取得結果之前產生較長的延遲。如果您不打算使用整個結果集，則可能會花費更多 I/O 來處理查詢並捨棄結果。

## 啟用 Amazon DocumentDB 分析器
<a name="profiling.enable-profiling"></a>

在叢集上啟用 Profiler 包含三個步驟。確保所有步驟都已完成，否則分析日誌將不會傳送至 CloudWatch Logs。Profiler 是在叢集層級設定，並在叢集的所有資料庫和執行個體上執行。

**在叢集上啟用 Profiler**

1. 由於您無法修改預設叢集參數群組，請確保您有可用的自訂叢集參數群組。如需詳細資訊，請參閱[建立 Amazon DocumentDB 叢集參數群組](cluster_parameter_groups-create.md)。

1. 使用可用的自訂叢集參數群組，修改下列參數：`profiler`、`profiler_threshold_ms` 和 `profiler_sampling_rate`。如需詳細資訊，請參閱[修改 Amazon DocumentDB 叢集參數群組](cluster_parameter_groups-modify.md)。

1. 建立或修改叢集以使用自訂叢集參數群組，並啟用將`profiler`日誌匯出至 CloudWatch Logs。

下列各節說明如何使用 AWS 管理主控台 和 AWS Command Line Interface () 實作這些步驟AWS CLI。

------
#### [ Using the AWS 管理主控台 ]

1. 開始之前，如果您還沒有 Amazon DocumentDB 叢集和自訂叢集參數群組，請建立叢集參數群組。如需詳細資訊，請參閱[建立 Amazon DocumentDB 叢集參數群組](cluster_parameter_groups-create.md)及[建立 Amazon DocumentDB 叢集](db-cluster-create.md)。

1. 使用可用的自訂叢集參數群組，修改下列參數。如需詳細資訊，請參閱[修改 Amazon DocumentDB 叢集參數群組](cluster_parameter_groups-modify.md)。
   + `profiler` — 啟用或停用查詢分析。允許值為 `enabled ` 和 `disabled`。預設值為 `disabled`。若要啟用分析，請將值設定為 `enabled`。
   + `profiler_threshold_ms` — 當 `profiler` 設定為 時`enabled`，所有花費超過 的命令` profiler_threshold_ms`都會記錄到 CloudWatch。允許值為 `[50-INT_MAX]`。預設值為 `100`。
   + `profiler_sampling_rate` — 應分析或記錄的慢速操作部分。允許值為 `[0.0-1.0]`。預設值為 `1.0`。

1. 修改您的叢集以使用自訂叢集參數群組，並將分析器日誌匯出設定為發佈至 Amazon CloudWatch。

   1. 在導覽窗格中，選擇 **Clusters (叢集)** 來將自訂參數群組新增至叢集。

   1. 選擇參數群組要與其建立關聯之叢集名稱左側的按鈕。選取 **Actions (動作)**，然後選取 **Modify (修改) **以修改您的叢集。

   1. 在 **Cluster (叢集) 選項**下，從上述步驟中選擇自訂參數群組，以將其新增至您的叢集。

   1. 在**日誌匯出**下，選取要發佈至 Amazon CloudWatch 的**分析器日誌**。

   1. 選擇 **Continue (繼續)** 以檢視修改摘要。

   1. 確認變更後，即可立即套用，或是在下一個維護時段在 **Scheduling of modifications (修改的排程)** 下套用。

   1. 選擇 **Modify cluster (修改叢集)** 來以新的參數群組更新叢集。

------
#### [ Using the AWS CLI ]

下列程序會在叢集 `sample-cluster` 的所有受支援操作上啟用 Profiler。

1. 在開始之前，請執行下列命令，並檢閱名稱中沒有 `default` 且具有 `docdb3.6` 作為參數群組系列之叢集參數群組的輸出，以確定您擁有可用的自訂叢集參數群組。如果您沒有非預設的叢集參數群組，請參閱[建立 Amazon DocumentDB 叢集參數群組](cluster_parameter_groups-create.md)。

   ```
   aws docdb describe-db-cluster-parameter-groups \
       --query 'DBClusterParameterGroups[*].[DBClusterParameterGroupName,DBParameterGroupFamily]'
   ```

   在下列輸出中，僅 `sample-parameter-group ` 符合這兩個條件。

   ```
   [
          [
              "default.docdb3.6",
              "docdb3.6"
          ],
          [
              "sample-parameter-group",
              "docdb3.6"
          ]
   ]
   ```

1. 使用您的自訂叢集參數群組，修改下列參數：
   + `profiler` — 啟用或停用查詢分析。允許值為 `enabled ` 和 `disabled`。預設值為 `disabled`。若要啟用分析，請將值設定為 `enabled`。
   + `profiler_threshold_ms` — 當 `profiler` 設為 時`enabled`，所有花費超過 的命令`profiler_threshold_ms`都會記錄到 CloudWatch。允許值為 `[50-INT_MAX]`。預設值為 `100`。
   + `profiler_sampling_rate` — 應分析或記錄的慢速操作部分。允許值為 `[0.0-1.0]`。預設值為 `1.0`。

   ```
   aws docdb modify-db-cluster-parameter-group \
       --db-cluster-parameter-group-name sample-parameter-group \
       --parameters ParameterName=profiler,ParameterValue=enabled,ApplyMethod=immediate \
                    ParameterName=profiler_threshold_ms,ParameterValue=100,ApplyMethod=immediate \
                    ParameterName=profiler_sampling_rate,ParameterValue=0.5,ApplyMethod=immediate
   ```

1. 修改 Amazon DocumentDB 叢集，使其使用上一個步驟的`sample-parameter-group`自訂叢集參數群組，並將 參數`--enable-cloudwatch-logs-exports`設定為 `profiler`。

   下列程式碼會將叢集修改`sample-cluster`為使用上一個步驟`sample-parameter-group`的 ，並新增至已啟用`profiler`的 CloudWatch Logs 匯出。

   ```
   aws docdb modify-db-cluster \
          --db-cluster-identifier sample-cluster \
          --db-cluster-parameter-group-name sample-parameter-group \
          --cloudwatch-logs-export-configuration '{"EnableLogTypes":["profiler"]}'
   ```

   此操作的輸出將會如下所示。

   ```
   {
       "DBCluster": {
           "AvailabilityZones": [
               "us-east-1c",
               "us-east-1b",
               "us-east-1a"
           ],
           "BackupRetentionPeriod": 1,
           "DBClusterIdentifier": "sample-cluster",
           "DBClusterParameterGroup": "sample-parameter-group",
           "DBSubnetGroup": "default",
           "Status": "available",
           "EarliestRestorableTime": "2020-04-07T02:05:12.479Z",
           "Endpoint": "sample-cluster.node.us-east-1.docdb.amazonaws.com",
           "ReaderEndpoint": "sample-cluster.node.us-east-1.docdb.amazonaws.com",
           "MultiAZ": false,
           "Engine": "docdb",
           "EngineVersion": "3.6.0",
           "LatestRestorableTime": "2020-04-08T22:08:59.317Z",
           "Port": 27017,
           "MasterUsername": "test",
           "PreferredBackupWindow": "02:00-02:30",
           "PreferredMaintenanceWindow": "tue:09:50-tue:10:20",
           "DBClusterMembers": [
               {
                   "DBInstanceIdentifier": "sample-instance-1",
                   "IsClusterWriter": true,
                   "DBClusterParameterGroupStatus": "in-sync",
                   "PromotionTier": 1
               },
               {
                   "DBInstanceIdentifier": "sample-instance-2",
                   "IsClusterWriter": true,
                   "DBClusterParameterGroupStatus": "in-sync",
                   "PromotionTier": 1
               }
           ],
           "VpcSecurityGroups": [
               {
                   "VpcSecurityGroupId": "sg-abcd0123",
                   "Status": "active"
               }
           ],
           "HostedZoneId": "ABCDEFGHIJKLM",
           "StorageEncrypted": true,
           "KmsKeyId": "arn:aws:kms:us-east-1:<accountID>:key/sample-key",
           "DbClusterResourceId": "cluster-ABCDEFGHIJKLMNOPQRSTUVWXYZ",
           "DBClusterArn": "arn:aws:rds:us-east-1:<accountID>:cluster:sample-cluster",
           "AssociatedRoles": [],
           "ClusterCreateTime": "2020-01-10T22:13:38.261Z",
           "EnabledCloudwatchLogsExports": [
               "profiler"
           ],
           "DeletionProtection": true
       }
   }
   ```

------

## 停用 Amazon DocumentDB 分析器
<a name="profiling.disable-profiling"></a>

若要停用分析器，請同時停用 `profiler` 參數和將`profiler`日誌匯出至 CloudWatch Logs。

### 停用 Profiler
<a name="profiling.disable-profiler"></a>

您可以使用 AWS 管理主控台 或 停用 `profiler` 參數 AWS CLI，如下所示。

------
#### [ Using the AWS 管理主控台 ]

下列程序使用 AWS 管理主控台 來停用 Amazon DocumentDB `profiler`。

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/docdb](https://console.aws.amazon.com/docdb) 開啟 Amazon DocumentDB 主控台。

1. 在導覽窗格中，選擇 **Parameter groups** (參數群組)。然後選擇您要停用 Profiler 的叢集參數群組的名稱。

1. 在產生的 **Cluster parameters (叢集參數)** 頁面中，選取 `profiler` 參數左邊的按鈕，然後選擇 **Edit (編輯)**。

1. 在 **Modify profiler (修改 Profiler)** 對話方塊中，選擇清單中的 `disabled`。

1. 選擇 **Modify cluster parameter (修改叢集參數)**。

------
#### [ Using the AWS CLI ]

若要使用 AWS CLI在叢集上停用 `profiler`，請修改叢集，如下所示。

```
aws docdb modify-db-cluster-parameter-group \
    --db-cluster-parameter-group-name sample-parameter-group \
    --parameters ParameterName=profiler,ParameterValue=disabled,ApplyMethod=immediate
```

------

## 停用分析器日誌匯出
<a name="profiling.disabling-logs-export"></a>

您可以使用 AWS 管理主控台 或 來停用將`profiler`日誌匯出至 CloudWatch Logs AWS CLI，如下所示。

------
#### [ Using the AWS 管理主控台 ]

下列程序使用 AWS 管理主控台 來停用 Amazon DocumentDB 匯出日誌至 CloudWatch。

1. 在 https：//[https://console.aws.amazon.com/docdb](https://console.aws.amazon.com/docdb) 開啟 Amazon DocumentDB 主控台。

1. 在導覽窗格中，選擇**叢集**。對於您要停止匯出日誌的叢集，選擇其名稱左側的按鈕。

1. 在 **Actions (動作)** 功能表上，選擇 **Modify (修改)**。

1. 向下捲動至 **Log exports (日誌匯出)** 區段，並取消選取 **Profiler logs (分析工具日記)**。

1. 選擇**繼續**。

1. 檢閱您的變更，然後選擇希望叢集套用此變更的時機：
   + **Apply during the next scheduled maintenance window (在下次排定的維護時段套用)**
   + **Apply immediately (立即套用)**

1. 選擇**修改叢集**。

------
#### [ Using the AWS CLI ]

下列程式碼會修改叢集`sample-cluster `，並停用 CloudWatch Profiler 日誌。

**Example**  
針對 Linux、macOS 或 Unix：  

```
aws docdb modify-db-cluster \
   --db-cluster-identifier sample-cluster \
   --cloudwatch-logs-export-configuration '{"DisableLogTypes":["profiler"]}'
```
針對 Windows：  

```
aws docdb modify-db-cluster ^
   --db-cluster-identifier sample-cluster ^
   --cloudwatch-logs-export-configuration '{"DisableLogTypes":["profiler"]}'
```
此操作的輸出將會如下所示。  

```
{
    "DBCluster": {
        "AvailabilityZones": [
            "us-east-1c",
            "us-east-1b",
            "us-east-1a"
        ],
        "BackupRetentionPeriod": 1,
        "DBClusterIdentifier": "sample-cluster",
        "DBClusterParameterGroup": "sample-parameter-group",
        "DBSubnetGroup": "default",
        "Status": "available",
        "EarliestRestorableTime": "2020-04-08T02:05:17.266Z",
        "Endpoint": "sample-cluster.node.us-east-1.docdb.amazonaws.com",
        "ReaderEndpoint": "sample-cluster.node.us-east-1.docdb.amazonaws.com",
        "MultiAZ": false,
        "Engine": "docdb",
        "EngineVersion": "3.6.0",
        "LatestRestorableTime": "2020-04-09T05:14:44.356Z",
        "Port": 27017,
        "MasterUsername": "test",
        "PreferredBackupWindow": "02:00-02:30",
        "PreferredMaintenanceWindow": "tue:09:50-tue:10:20",
        "DBClusterMembers": [
            {
                "DBInstanceIdentifier": "sample-instance-1",
                "IsClusterWriter": true,
                "DBClusterParameterGroupStatus": "in-sync",
                "PromotionTier": 1
            },
            {
                "DBInstanceIdentifier": "sample-instance-2",
                "IsClusterWriter": true,
                "DBClusterParameterGroupStatus": "in-sync",
                "PromotionTier": 1
            }
        ],
        "VpcSecurityGroups": [
            {
                "VpcSecurityGroupId": "sg-abcd0123",
                "Status": "active"
            }
        ],
        "HostedZoneId": "ABCDEFGHIJKLM",
        "StorageEncrypted": true,
        "KmsKeyId": "arn:aws:kms:us-east-1:<accountID>:key/sample-key",
        "DbClusterResourceId": "cluster-ABCDEFGHIJKLMNOPQRSTUVWXYZ",
        "DBClusterArn": "arn:aws:rds:us-east-1:<accountID>:cluster:sample-cluster",
        "AssociatedRoles": [],
        "ClusterCreateTime": "2020-01-10T22:13:38.261Z",
        "DeletionProtection": true
    }
}
```

------

## 存取 Amazon DocumentDB 分析器日誌
<a name="profiling.accessing"></a>

請依照下列步驟，在 Amazon CloudWatch 上存取您的設定檔日誌。

1. 透過 [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/) 開啟 CloudWatch 主控台。

1. 請確定您位於與 Amazon DocumentDB 叢集相同的區域。

1. 在導覽窗格中，選擇**日誌**。

1. 若要尋找叢集的分析工具日誌，請在清單中選擇 `/aws/docdb/yourClusterName/profiler`。

   您可在每個執行個體名稱下分別取得每個執行個體的描述檔日誌。

## 常見查詢
<a name="profiling.common-queries"></a>

以下是一些常用的查詢，可用來分析您已側錄的命令。如需 CloudWatch Logs Insights 的詳細資訊，請參閱[使用 CloudWatch Logs Insights 分析日誌資料](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html)和[範例查詢](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax-examples.html)。

### 取得指定集合上 10 個最慢的操作
<a name="profiling.common-queries.slow-queries-on-collection"></a>

```
filter ns="test.foo" | sort millis desc | limit 10
```

### 取得花費超過 60 毫秒集合的所有更新操作
<a name="profiling.common-queries.updates-gt-60-ms"></a>

```
filter millis > 60 and op = "update"
```

### 取得上個月 10 個最慢的操作
<a name="profiling.common-queries.slow-queries-last-month"></a>

```
sort millis desc | limit 10
```

### 使用 COLLSCAN 計劃摘要取得所有查詢
<a name="profiling.common-queries.collscan-plan-summary"></a>

```
filter planSummary="COLLSCAN"
```