Follow the following steps to troubleshoot high CPU usage:
1. Check when the CPU usage is too high in the CPU label on the Dashboard in the console.
2. Log in to the corresponding instance machine to view the system resource usage: Firstly, use the monitoring tool (such as top or htop) in a Linux system or the task Manager in a Windows system to view the overall system CPU usage and other resource usage. Determine whether only the MongoDB process uses a large amount of CPU resources, or whether other processes are also competing for CPU resources.
3. Tool and method for analyzing the DDS query performance:
Tools such as MongoDB Compass are used to check what queries are currently being executed. For more information on downloading and using MongoDB Compass, go to the Mongodb official website.
Run the db.currentOp() command in MongoDB shell to check whether a query or operation uses a large amount of CPU resources.
Explanation of the db.currentOp() command:
For example, connect to Mongodb shell and execute the following operations:
> db.users.find({ age: { $gte: 18 } }).limit(10)
> db.currentOp()
{
"inprog" : [
{
"opid" : 1290, // Unique identifier for the operation
"op" : "query", // Operation type: query
"ns" : "exampledb.users", // Namespace involved in the operation
"query" : { "age" : { "$gte" : 18 } }, // Query condition
"client" : "127.0.0.1:57852", // IP address and port number of the client
"desc" : "conn211", // Description of the operation
"active" : true, // Whether the operation is active
"secs_running" : 5, // The time, in seconds, that the operation has been running
"oplog" : false,
"numYields" : 0,
"locks" : {
"Global" : "r", // Global read lock
"MMAPV1Journal" : "r" // Journal read lock of MMAPv1 engine
},
"waitingForLock" : false,
"msg" : "",
"numYieldOps" : 0,
"database" : "exampledb",
"command" : {
"find" : "users", // Command executed: Search operation
"filter" : { "age" : { "$gte" : 18 } }, // Filter condition
"limit" : 10 // Limited number of results
},
"planSummary" : "COLLSCAN", // Summary of query execution plan
"lsid" : {
"id" : { "UUID" : "e45ea917-6c61-4a5e-8681-2da4af89aa29" }, // Session ID
"uid" : ObjectId("ea6a1a2570a062f88eac10f5") // User ID
},
"$clusterTime" : { "clusterTime" : Timestamp(1657796621, 1), "signature" : { "hash" : { "BsonData" : "wDUTByvnZXjN+MJB/M9Rlg==", "Length" : 28 }, "keyId" : 6963168194386045953 } },
"stale" : false,
"txnNumber" : 11,
"startAt" : ISODate("2023-07-21T15:57:01.246Z"),
"autocommit" : false,
"lastWrite" : { "opTime" : { "ts" : Timestamp(1657796621, 1), "t" : 1 }, "lastWriteDate" : ISODate("2023-07-21T15:57:01Z"), "majorityOpTime" : { "ts" : Timestamp(1657796621, 1), "t" : 1 }, "majorityWriteDate" : ISODate("2023-07-21T15:57:01Z") },
"activeShards" : {},
"numShards" : 0,
"shardName" : "",
"hashed" : false,
"batchSize" : 0,
"mode" : "scanned"
}
]
}4. Verifying index usage: Check whether indexes in the database are being used effectively. Missing indexes or improper index usage can result in degraded query performance, resulting in high CPU usage.
5. Long-running queries: Check for long-running queries that may consume a lot of CPU resources. Optimizing the query performance or fine-tuning long-running queries is helpful to reduce CPU usage.
6. Adjusting Write Concern: The Write Concern setting of Write operations affects the confirmation mode of data writes. A higher Write Concern setting may increase CPU overhead. Select an appropriate Write Concern according to your application requirements.
7. Hardware performance: Confirm that the document database is running on well-performing hardware, including CPU, memory, and disk. Insufficient hardware performance may result in high CPU usage.
8. Logging level: Reduce the logging level of document data to reduce the impact of log output on the CPU.
9. Stress testing: Perform stress testing to simulate the load in a production environment and observe changes in CPU usage. Stress testing provides a better understanding of system bottlenecks and performance issues.
10. Sharding: If the data is large in size, you can use the sharded cluster of the DDS to distribute the data across multiple nodes to achieve scale out and load balancing.
11. Database version and configuration: Ensure that the document database service is a new and stable version, and configure the parameters of the document database service according to the hardware resources and load.
Note:
High CPU usage can be a complex problem that is caused by multiple factors. Before troubleshooting, we recommend that you conduct an experiment and perform tests in a test environment to identify the causes. If the problem persists, seek professional technical support for document databases.