If the query statements are not optimal enough (for example, indexes are not properly set), the number of concurrent requests is large, or the computing workload is heavy during the use of the MongoDB database, the CPU usage of the MongoDB database may become high. This will lead to problems such as slow data read/write and more timeouts, severely affecting normal service operations. This topic describes how to troubleshoot and locate the high CPU usage issue of the MongoDB instance in terms of query statements, slow log, and execution plans.
Viewing running statements
Perform the following steps to view the statements running in the database:
- Run the db.currentOp() command on the target instance to view the statements running in the database.
tip
For more information about the db.currentOp() command, refer to db.currentOp(). - Example of a partial return result is as follows.
"client" : "192.168.***.***:****",
"appName" : "MongoDB Shell",
"currentOpTime" : "2023-01-28T16:45:51.243+0800",
"secs_running" : NumberLong(0),
"microsecs_running" : NumberLong(137),
"ns" : "admin.$cmd.aggregate",
"waitingForLock" : false,
- You need to focus on the following fields in the response.
tip
You can view and analyze whether time-consuming requests exist in the response. For example, the CPU usage is normal during routine service operations, but increases after you perform some operations that require full table scan, which makes service response slow. You need to focus on these requests. For more information about the fields in the response, refer to currentOp. Querying slow logs
If the CPU usage of your instance increases out of expectation but no time-consuming requests are found in the output of the db.currentOp() command, you can analyze slow requests to troubleshoot and locate the fault.
By default, the MongoDB database records requests with an execution time of over 100 ms into slow logs. You can refer to the following steps to analyze slow logs and identify the cause of abnormal CPU usage increases.
- Query slow logs on the MongoDB console. For more information, refer to Querying slow logs.
- Analyze slow logs to find the cause of the abnormal CPU usage increase of the MongoDB instance.
- The following table lists some keywords in slow logs, causes for slow logs, and some handling suggestions.
Analyzing execution plans
MongoDB provides the explain() command for checking statistical data of the specified query plan, including the indexes used, whether the query statement is covered by the indexes, the number of scanned indexes, the number of read documents, the number of returned documents, and the time taken to execute the query.
You can create proper indexes based on the preceding information in a query plan to optimize querying and reduce CPU resource consumption. For more information about the output of the explain() command, refer to Explain results. Using an instance matching your business load
If the CPU usage is still high after you set proper indexes for a database request based on the statements running in the database, slow request, and analysis result of an execution plan, you must evaluate whether the current configuration of the MongoDB instance meets your business needs.
You can Viewing monitoring data to analyze the use of instance resources and carry out performance testing on the MongoDB database to check whether the current instance specification can provide the device performance and service capability needed by your business. Data analytics For more information about how to upgrade specification of MongoDB instance, refer to Modifying instance configuration.