分析 Amazon DocumentDB 操作 - Amazon DocumentDB

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

分析 Amazon DocumentDB 操作

您可以使用 Amazon DocumentDB 中的效能分析工具 (與 MongoDB 相容性) 來記錄叢集上執行的作業的執行時間和詳細資料。Profiler 適用於監控叢集上最慢的操作,以協助您改善個別查詢效能和整體叢集效能。

根據預設會停用 Profiler 功能。啟用時,效能分析工具會將花費超過客戶定義的臨界值 (例如 100 ms) 時的操作記錄到 Amazon Logs。 CloudWatch 記錄的詳細資訊包括已分析的命令、時間、計劃摘要和用戶端中繼資料。將操作記錄到 CloudWatch 日誌後,您可以使用 CloudWatch 日誌深入解析來分析、監控和存檔 Amazon DocumentDB 分析資料。常見查詢小節提供常用的查詢。

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

雖然 Amazon DocumentDB 不會收取額外費用來啟用效能分析工具,但是系統會向您收取使用日誌的標準費率。 CloudWatch 如需 CloudWatch 日誌定價的相關資訊,請參閱 Amazon CloudWatch 定價

受支援的操作

Amazon DocumentDB 分析工具支持以下操作:

  • aggregate

  • count

  • delete

  • distinct

  • find(OP_ QUERY 和指令)

  • findAndModify

  • insert

  • update

限制

只有在查詢的整個結果集能夠容納一個批次,且結果集小於 16MB (大小上限) 時,慢速查詢效能分析工具才能發出效能分析工具記錄。BSON大於 16MB 的結果集會自動分割成多個批次。

大多數驅動程式或 shell 可能會設定較小的預設批次大小。您可以將批次大小指定為查詢的一部分。為了捕獲緩慢的查詢日誌,我們建議使用超過預期結果集大小的批次大小。如果您不確定結果集大小,或者結果有所不同,也可以將批次大小設定為較大的數字 (例如,100k)。

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

啟用 Amazon DocumentDB 分析器

在叢集上啟用 Profiler 包含三個步驟。請確定所有步驟都已完成,否則效能分析記錄檔將不會傳送至 CloudWatch 記錄檔。Profiler 是在叢集層級設定,並在叢集的所有資料庫和執行個體上執行。

在叢集上啟用 Profiler
  1. 由於您無法修改預設叢集參數群組,請確保您有可用的自訂叢集參數群組。如需詳細資訊,請參閱建立 Amazon DocumentDB 叢集參數群組

  2. 使用可用的自訂叢集參數群組,修改下列參數:profilerprofiler_threshold_msprofiler_sampling_rate。如需詳細資訊,請參閱修改 Amazon DocumentDB 叢集參數群組

  3. 建立或修改叢集以使用自訂叢集參數群組,並啟用將profiler記錄匯出至 CloudWatch 記錄檔。

以下各節說明如何使用 AWS Management Console 和 AWS Command Line Interface (AWS CLI) 來實作這些步驟。

Using the AWS Management Console
  1. 在開始之前,請先建立 Amazon DocumentDB 叢集和自訂叢集參數群組 (如果您還沒有叢集參數群組)。如需詳細資訊,請參閱 建立 Amazon DocumentDB 叢集參數群組創建一個 Amazon DocumentDB 集群

  2. 使用可用的自訂叢集參數群組,修改下列參數。如需詳細資訊,請參閱修改 Amazon DocumentDB 叢集參數群組

    • 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

  3. 修改叢集以使用自訂叢集參數群組,並將效能分析工具日誌匯出設定為發佈到 Amazon。 CloudWatch

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

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

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

    4. 在「日誌匯出」下,選取要發佈到 Amazon 的效能評測工具日誌。 CloudWatch

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

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

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

Using the AWS CLI

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

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

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

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

    [ [ "default.docdb3.6", "docdb3.6" ], [ "sample-parameter-group", "docdb3.6" ] ]
  2. 使用您的自訂叢集參數群組,修改下列參數:

    • profiler— 啟用或停用查詢效能分析。允許值為 enabled disabled。預設值為 disabled。若要啟用分析,請將值設定為 enabled

    • profiler_threshold_ms— 設定profiler為時enabled,所有花費超過時間的指令profiler -threshold-ms都會記錄到 CloudWatch。允許值為 [0-INT_MAX]。將此值設定為 0 會分析所有支援的操作。預設值為 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
  3. 修改 Amazon DocumentDB 叢集,使其使用上一個步驟中的sample-parameter-group自訂叢集參數群組,並將參數--enable-cloudwatch-logs-exports設定為。profiler

    下列程式碼會修改叢集sample-cluster以使用上一個步驟sample-parameter-group中的,並新增profiler至已啟用的 CloudWatch 記錄匯出。

    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 效能分析工具

若要停用效能分析工具,請同時停用profiler參數和將記錄檔匯出至profiler CloudWatch 記錄檔。

停用 Profiler

您可以使用 AWS Management Console 或來停用profiler參數 AWS CLI,如下所示。

Using the AWS Management Console

下列程序會使用 AWS Management Console 來停用 Amazon DocumentDB profiler

  1. 登入 AWS Management Console,然後在 https://console.aws.amazon.com/docdb 開啟 Amazon DocumentDB 主控台。

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

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

  4. Modify profiler (修改 Profiler) 對話方塊中,選擇清單中的 disabled

  5. 選擇 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

停用效能分析工具記錄檔匯出

您可以使用 AWS Management Console 或停用將profiler CloudWatch 記錄匯出至記錄檔 AWS CLI,如下所示。

Using the AWS Management Console

下列程序會使用將日誌匯出 AWS Management Console 至停用 Amazon DocumentDB。 CloudWatch

  1. 打開 Amazon DocumentDB 控制台 https://console.aws.amazon.com/docdb。

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

  3. Actions (動作) 功能表上,選擇 Modify (修改)

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

  5. 選擇繼續

  6. 檢閱您的變更,然後選擇希望叢集套用此變更的時機:

    • Apply during the next scheduled maintenance window (在下次排定的維護時段套用)

    • Apply immediately (立即套用)

  7. 選擇修改叢集

Using the AWS CLI

下列程式碼會修改叢集sample-cluster 並停用 CloudWatch 效能分析工具記錄。

若為 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 上的個人資料日誌 CloudWatch。

  1. 在開啟 CloudWatch 主控台https://console.aws.amazon.com/cloudwatch/

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

  3. 在導覽窗格中,選擇日誌

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

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

常見查詢

以下是一些常用的查詢,可用來分析您已側錄的命令。如需有關 CloudWatch 記錄洞見的詳細資訊,請參閱使用 CloudWatch 日誌深入解析和範例查詢分析記錄資料。

獲取指定集合上 10 個最慢的操作

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

獲取花費超過 60 ms 的集合上的所有更新操作

filter millis > 60 and op = "update"

取得上個月最慢的 10 個作業

sort millis desc | limit 10

使用COLLSCAN計劃摘要獲取所有查詢

filter planSummary="COLLSCAN"