You can view the slow query logs of a database in an instance in the ApsaraDB for MongoDB console and analyze the logs to optimize the database.
Prerequisites
The instance is a replica set instance or a sharded cluster instance.
If the instance was purchased between June 6, 2021 and November 25, 2021, the audit log feature is enabled and slow is selected in the Audit Log Filter Setting panel. For more information about how to enable the audit log feature, see Enable the audit log feature.
Note If only slow is selected in the Audit Log Filter Setting panel, the audit log feature is provided free of charge.
Usage notes
The retention period of slow query logs is seven days.
Slow query logs cannot be deleted.
You cannot view the slow query logs of databases in a standalone instance.
The log management feature is unavailable in some regions. You can log on to the ApsaraDB for MongoDB console to check whether the feature is supported in a specific region.
Procedure
Go to the Replica Set Instances or Sharded Cluster Instances page. In the top navigation bar, select the region in which an instance resides. Then, find the instance and click the ID of the instance.
In the left-side navigation pane of the instance details page, choose .
View slow query logs based on the instance architecture.
Note Click Export to download the slow query logs to your on-premises computer.
FAQ
Why are the expected time-consuming operations not recorded in slow query logs?
If the expected time-consuming operations are not found when you query slow query logs, we recommend that you use one of the following methods to troubleshoot the issue:
View the specified slow operation threshold. Slow operations refer to those whose execution duration exceeds the specified time threshold. You can check whether the settings of the operationProfiling.slowOpThresholdMs parameter meet your requirements. The default value of the parameter is 100. The parameter is in the unit of milliseconds. For more information about how to view and configure parameters, see Configure database parameters for an instance.
View the instance version. If your instance version is MongoDB 4.0, we recommend that you perform the audit log analysis or upgrade your instance to MongoDB 4.2. For more information, see Query audit logs and Upgrade the major version of an instance.
By default, only admin and slow are selected for the audit operation types after the audit log feature is enabled. In this case, the operations of the corresponding types are recorded in the slow query logs generated by the feature. For instances that run MongoDB 4.0, if you change the specified audit operation types, an additional record is not generated for a update or delete operation when the operation is a slow one. We recommend that you analyze slow operations based on the execution duration shown in audit logs, or upgrade your instance to MongoDB 4.2. This version has optimized the preceding behavior to ensure that all slow operations are recorded in slow query logs.
Why am I not authorized to access slow query logs as a Resource Access Management (RAM) user?
The slow log analysis feature is provided by Database Autonomy Service (DAS). Before you access the slow query logs, you must be granted permissions on DAS. For more information, see How do I use DAS as a RAM user?
References
For more information about how to view detailed slow query logs or export slow query logs, see Slow query logs.