分析亞馬遜文檔數據庫操作 - Amazon DocumentDB

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

分析亞馬遜文檔數據庫操作

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

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

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

雖然 Amazon DocumentDB 不會收取額外費用來啟用效能分析工具,但是我們會向您收取使用量的標準費率 CloudWatch 記錄檔。如需相關資訊 CloudWatch 記錄檔定價,請參閱亞馬遜 CloudWatch 價錢

受支援的 操作

亞馬遜文檔數據庫分析工具支持以下操作:

  • aggregate

  • count

  • delete

  • distinct

  • find (OP_QUERY 和命令)

  • findAndModify

  • insert

  • update

限制

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

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

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

啟用亞馬遜文件資料庫效能分析工具

在叢集上啟用 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. 記錄匯出,選取效能分析工具記錄檔發布到亞馬遜 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. 修改您的亞馬遜文檔數據庫叢集,使其使用sample-parameter-group自定義集群參數組從上一步,並設置參數--enable-cloudwatch-logs-exportsprofiler

    下面的代碼修改集群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 } }

停用亞馬遜文件資料庫效能分析工具

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

停用 Profiler

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

Using the AWS Management Console

下列程序會使用AWS Management Console禁用亞馬遜文檔數據庫profiler

  1. 登入AWS Management Console,然後在以下位置打開亞馬遜文檔數據庫控制台https://console.aws.amazon.com/docdb

  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

停用 Profiler 日誌匯出

您可以禁用導出profiler記錄到 CloudWatch 使用記錄AWS Management Console或者AWS CLI,如下所示。

Using the AWS Management Console

下列程序會使用AWS Management Console禁用亞馬遜文檔數據庫將日誌導出到 CloudWatch。

  1. 打開亞馬遜文檔數據庫控制台https://console.aws.amazon.com/docdb

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

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

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

  5. 選擇 Continue (繼續)。

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

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

    • Apply immediately (立即套用)

  7. 選擇 Modify cluster (修改叢集)。

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 文件資料庫叢集位於相同的區域。

  3. 在導覽窗格中,選擇 Logs (日誌)。

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

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

常用查詢

以下是一些常用的查詢,可用來分析您已側錄的命令。有關更多信息 CloudWatch 日誌洞察,請參閱分析記錄資料 CloudWatch 日誌洞察範例查詢

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

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

取得集合上耗費超過 60 毫秒的所有更新操作

filter millis > 60 and op = "update"

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

sort millis desc | limit 10

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

filter planSummary="COLLSCAN"