Error
Error Code:
175
MongoDB Error 175: Query Plan Terminated
Description
MongoDB Error 175, 'Query Plan Killed', indicates that the database system has terminated the execution plan for a query before it could complete. This typically occurs when a query consumes excessive resources, exceeds a time limit, or is explicitly stopped, preventing the operation from returning results.
Error Message
Query Plan Killed
Known Causes
4 known causesExcessive Resource Consumption
A query plan might be killed if it consumes too much CPU, memory, or I/O, leading MongoDB to terminate it to preserve overall system stability.
Query Optimization Change
MongoDB's query optimizer may dynamically switch to a more efficient execution plan mid-query, killing the older, less optimal plan.
Max Time MS Exceeded
If a query has a `maxTimeMS` limit set and exceeds this duration, MongoDB will automatically terminate its execution plan.
Manual Operation Termination
An administrator or another process explicitly used `db.killOp()` to stop a running query or operation.
Solutions
4 solutions available1. Optimize Slow Queries with Indexing medium
Add appropriate indexes to speed up query execution and prevent plan termination.
1
Identify the slow query causing the error. You can use the `db.system.profile.find()` command (if profiling is enabled) or analyze MongoDB logs for slow operations. Look for queries that take a long time to execute.
db.system.profile.find({millis: {$gt: 1000}}).sort({ts: -1})
2
Examine the query's `explain()` output to understand its execution plan and identify missing indexes.
db.collection.find({field1: 'value1', field2: 'value2'}).explain('executionStats')
3
Create an index that covers the fields used in the query's filter, sort, and projection stages. For compound indexes, consider the order of fields based on query selectivity.
db.collection.createIndex({field1: 1, field2: 1})
4
Verify that the new index is being used by re-running the `explain()` command.
db.collection.find({field1: 'value1', field2: 'value2'}).explain('executionStats')
2. Increase Query Execution Time Limit easy
Adjust the maximum time a query can run before being terminated.
1
Connect to your MongoDB instance using the mongo shell.
2
Use the `db.adminCommand()` to set the `maxTimeMS` option for the current session. This will increase the timeout for subsequent queries in that session. A value of 0 means no limit.
db.adminCommand({configureFailPoint: 'maxTimeAlwaysOn', mode: {times: 1}, data: {maxTimeMS: 300000}})
3
Alternatively, you can specify `maxTimeMS` directly in your query. This is a more granular approach.
db.collection.find({field: 'value'}).maxTimeMS(300000)
3. Analyze and Refactor Complex Queries advanced
Simplify or break down overly complex queries that might be overwhelming the query planner.
1
Identify queries that are frequently associated with Error 175. These are often complex aggregations or queries with many stages.
2
Use the `explain()` output to understand the query's execution plan. Look for stages that are taking an excessive amount of time or resources.
db.collection.aggregate([...]).explain('executionStats')
3
Consider breaking down the complex query into smaller, more manageable queries. For example, instead of a single large aggregation, you might perform intermediate operations and then combine results.
4
If possible, pre-compute or cache results of frequently used complex operations. This can be done by storing intermediate results in separate collections or using an external caching layer.
4. Review Server Resources and Configuration medium
Ensure sufficient system resources and appropriate MongoDB configuration to handle query load.
1
Monitor system resources such as CPU, RAM, and disk I/O on your MongoDB servers. High resource utilization can lead to query slowdowns and timeouts.
2
Check MongoDB's configuration settings, particularly `wiredTigerCacheSizeGB` and `maxIncomingConnections`. Ensure they are appropriately sized for your workload.
3
If running on a replica set or sharded cluster, ensure all nodes are healthy and synchronized. Network latency or node failures can impact query performance.
4
Consider scaling your MongoDB deployment if persistent resource constraints are identified. This might involve adding more nodes to a replica set or shards to a sharded cluster.