Skip to content
LESSON 29 / 50Intermediateindexing

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.

Interactive Engine Simulator

Index Traversal & IXSCAN vs COLLSCAN Execution Plan

Topic 29: Hidden Indexes: Evaluating Index Dropping Safely

Execution Stage
IXSCAN + FETCH
B-Tree leaf seek
Documents Examined
2
O(log N) lookup
Keys Examined
2
Index keys inspected
Query Latency
0.3 ms
WiredTiger execution time
Explain Plan Output Summary:
{
  "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.

Sample BSON Documents:
Collection: db.orders
{
  "_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

Step 1: Hide an index from query optimization
db.orders.hideIndex("customer_code_1");
Index remains updated on writes, but query planner ignores it.
Step 2: Unhide an index instantly if performance degrades
db.orders.unhideIndex("customer_code_1");
Immediately available to query planner in O(1) time without rebuilding.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
Write MaintenanceHidden indexes still incur write performance overhead and disk storage footprint.
_id ExceptionThe 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));
});
Production Warning: Do not leave indexes hidden indefinitely; they continue consuming write IOPS and cache space.

7. Architectural Trade-offs

Advantages
  • •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
Disadvantages & Gotchas
  • •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

Q1: Why should you hide an index instead of immediately dropping it when performing index cleanup in production?

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?