Error
Error Code:
291
MongoDB Error 291: Missing Query Execution Plans
Description
Error 291, 'No Query Execution Plans,' indicates that MongoDB was unable to generate a valid execution plan for the submitted query. This often occurs when the query optimizer cannot determine an efficient way to process the request due to complexity, unsupported operations, or indexing issues.
Error Message
No Query Execution Plans
Known Causes
4 known causesOverly Complex or Unsupported Query
The query contains operations or a level of complexity that the MongoDB query optimizer struggles to process into an executable plan.
Missing or Ineffective Indexes
The database lacks appropriate indexes to support the query predicates efficiently, preventing the optimizer from creating a viable execution plan.
Sharding Configuration or Data Distribution
Problems in a sharded cluster, such as an improper shard key or skewed data distribution, can hinder the optimizer's ability to create a global plan.
Optimizer Resource Limits
In rare cases, the query optimizer might hit internal resource limits or encounter an edge case that prevents it from generating a plan.
Solutions
3 solutions available1. Analyze and Optimize Slow Queries medium
Identify and improve queries that are not generating execution plans.
1
Enable the profiler to capture slow queries. Set the `profile` setting to 1 to profile all operations, or 2 to profile slow operations and their corresponding query plans. You can do this via the `mongosh` shell.
db.setProfilingLevel(1, 100); // Profile all operations, with a slow operation threshold of 100ms
2
Query the `system.profile` collection to find queries that are taking too long or are not generating plans. Look for entries where the `planSummary` field might be missing or indicate a lack of a plan.
db.system.profile.find({ 'op': 'query', 'millis': { '$gt': 100 } }).pretty();
3
For identified slow queries, analyze their performance. Consider adding appropriate indexes to the collection that the query is targeting. Use `explain()` to understand the current query execution plan (or lack thereof).
db.collection('your_collection').find({ 'field1': 'value1', 'field2': 'value2' }).explain('executionStats');
4
Create indexes based on the `explain()` output. For example, if the query filters on `field1` and sorts by `field2`, an index on `[{ field1: 1, field2: 1 }]` might be beneficial.
db.collection('your_collection').createIndex({ 'field1': 1, 'field2': 1 });
5
After adding indexes, re-run the query with `explain()` to confirm that an execution plan is now generated and that performance has improved.
db.collection('your_collection').find({ 'field1': 'value1', 'field2': 'value2' }).explain('executionStats');
6
Once performance is satisfactory, you can disable profiling or adjust its level to reduce overhead.
db.setProfilingLevel(0);
2. Restart the MongoDB Service easy
A simple restart can sometimes resolve transient issues preventing plan generation.
1
Gracefully shut down the MongoDB server. The command to do this depends on your operating system and how MongoDB was installed.
# For systems using systemd (e.g., Ubuntu 15.04+, CentOS 7+)
sudo systemctl stop mongod
2
Wait a few moments for the server to fully shut down.
text
3
Start the MongoDB server again.
# For systems using systemd
sudo systemctl start mongod
4
After the service has restarted, attempt to run the problematic query again and check if an execution plan is generated.
db.collection('your_collection').find({ 'some_field': 'some_value' }).explain('executionStats');
3. Check for Collection or Database Corruption advanced
Investigate if underlying data corruption is preventing query plan generation.
1
Run the `validate` command on the affected collection to check for data integrity issues. This can be done from the `mongosh` shell.
db.collection('your_collection').validate(true);
2
If the `validate` command reports errors, it indicates potential corruption. Consult MongoDB documentation or support for specific error codes and recovery procedures.
text
3
As a last resort for severe corruption, consider restoring from a known good backup. This is a disruptive process and requires careful planning.
text