Hidden Indexes: Evaluating Index Dropping Safely
Hidden indexes allow database administrators to hide an index from the query planner to safely test the impact of index removal before permanently dropping it.
Index Traversal & IXSCAN vs COLLSCAN Execution Plan
Topic 29: Hidden Indexes: Evaluating Index Dropping Safely
{
"queryPlanner": {
"winningPlan": {
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"indexName": "role_1_country_1",
"isMultiKey": false,
"direction": "forward"
}
}
},
"executionStats": {
"nReturned": 2,
"executionTimeMillis": 1,
"totalKeysExamined": 2,
"totalDocsExamined": 2
}
}1. Formal Definition & Architecture Intent
A Hidden Index is an index that is invisible to the query optimizer during query plan evaluation. While hidden, the index is still actively updated during inserts, updates, and deletes, maintaining full B-Tree synchronization. If queries degrade after hiding, the index can be unhidden instantly without a costly rebuild.
2. Explain Like I'm 5 (Mental Model)
Dropping a large index in production is like cutting a bridge: if traffic gets ruined, rebuilding the bridge takes hours. Hiding an index is like putting up a temporary "Road Closed" sign: you test the traffic, and if a traffic jam happens, you just take the sign down in 1 second.
3. Real-World Architecture Example
Testing whether an unmaintained 50GB index on orders.customer_code can be safely dropped without impacting production.
{
"_id": "o1",
"customer_code": "CUST-881"
}Hiding the index hides it from the query planner. If query response times remain normal for 48 hours, it is safe to permanently drop.
4. MQL Implementation Steps
db.orders.hideIndex("customer_code_1");db.orders.unhideIndex("customer_code_1");5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| Write Maintenance | Hidden indexes still incur write performance overhead and disk storage footprint. |
| _id Exception | The primary key _id index cannot be hidden. |
6. Deep Dive: Zero-Downtime Index Deprecation Workflow
Dropping a 100GB index takes 1 second, but rebuilding it if queries collapse takes hours of disk I/O, CPU, and cache thrashing. The enterprise best practice is: 1. Hide index, 2. Monitor slow query logs and p99 latency for 72 hours, 3. If no regressions occur, run dropIndex(). If latency spikes, unhideIndex() instantly restores performance.
// Check index hidden status:
db.orders.getIndexes().forEach(idx => {
print(idx.name + " -> hidden: " + (idx.hidden || false));
});7. Architectural Trade-offs
- •Eliminates the catastrophic risk of dropping an index that was secretly used by rare cron jobs or reports
- •Unhiding is an instantaneous metadata operation taking under 1 millisecond
- •Allows safe performance auditing in live production clusters
- •While hidden, the index continues to consume write overhead and disk space
- •Cannot be used to test write throughput improvements until permanently dropped
8. Technical Interview Questions
Dropping an index is irreversible; if you discover that a critical production job relied on that index, rebuilding a large index on a multi-terabyte collection takes hours and severely impacts cluster performance. Hiding the index makes it invisible to the planner while keeping it synchronized on disk, allowing you to instantly unhide it in 1ms if query regressions occur.
9. Staff-Level Ready Answer
"Hidden indexes are invisible to the query planner but continue to be updated on writes. They provide a safe, zero-risk deprecation path: hiding an index tests whether queries regress, allowing instantaneous unhiding in 1ms without the hours of downtime required to rebuild a dropped index."
10. Quick Knowledge Check
Does a hidden index continue to receive updates when documents are inserted or modified?