Home > Blockchain >  MongoDB find: query not matching index according to queryPlanner of explain
MongoDB find: query not matching index according to queryPlanner of explain

Time:10-24

I have a mongoDB with a collection that is not working as fast as I would like. I've been reading about explain() and queryPlanner (didn't know about it until now). And I have observed that my indices do not macth.

I've tried the simplest index and it doesn't return match, but the winningPlan does seem to match. I will tell you how I did the steps:

  1. db.candidatures.createIndex( {"client_id": 1 } )
  2. db.candidatures.find({ client_id: "800a0d60-b5fe-11ea-ae04-42010a840222", }).explain() (UPDATE: find by client_id not dni)
  3. Check queryPlanner result
{
  "plannerVersion": newNumberInt("1"),
  "namespace": "hrbot-prod.candidatures",
  "indexFilterSet": false,
  "parsedQuery": {
    "client_id": {
      "$eq": "800a0d60-b5fe-11ea-ae04-42010a840222"
    }
  },
  "queryHash": "6C94666F",
  "planCacheKey": "65C00083",
  "winningPlan": {
    "stage": "FETCH",
    "inputStage": {
      "stage": "IXSCAN",
      "keyPattern": {
        "client_id": newNumberInt("1")
      },
      "indexName": "client_id",
      "isMultiKey": false,
      "multiKeyPaths": {
        "client_id": [
          
        ]
      },
      "isUnique": false,
      "isSparse": false,
      "isPartial": false,
      "indexVersion": newNumberInt("2"),
      "direction": "forward",
      "indexBounds": {
        "client_id": [
          "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
        ]
      }
    }
  },
  "rejectedPlans": [
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "dni": newNumberInt("1")
        },
        "indexName": "client_id_1_dni_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "dni": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "dni": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "candidature_id": newNumberInt("1")
        },
        "indexName": "client_id_1_candidature_id_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "candidature_id": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "candidature_id": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "update_at": newNumberInt("-1")
        },
        "indexName": "client_id_1_update_at_-1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "update_at": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "update_at": [
            "[MaxKey, MinKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "update_at": newNumberInt("1")
        },
        "indexName": "client_id_1_update_at_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "update_at": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "update_at": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "estado": newNumberInt("1")
        },
        "indexName": "client_id_1_estado_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "estado": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "matching": newNumberInt("1")
        },
        "indexName": "client_id_1_matching_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "matching": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "matching": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "matching": newNumberInt("-1")
        },
        "indexName": "client_id_1_matching_-1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "matching": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "matching": [
            "[MaxKey, MinKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "disponibilidad": newNumberInt("-1")
        },
        "indexName": "client_id_1_disponibilidad_-1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "disponibilidad": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "disponibilidad": [
            "[MaxKey, MinKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "email": newNumberInt("1")
        },
        "indexName": "client_id_1_email_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "email": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "email": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "candidature_id": newNumberInt("1"),
          "email": newNumberInt("1")
        },
        "indexName": "client_id_1_candidature_id_1_email_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "candidature_id": [
            
          ],
          "email": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "candidature_id": [
            "[MinKey, MaxKey]"
          ],
          "email": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "candidature_id": newNumberInt("1"),
          "update_at": newNumberInt("-1")
        },
        "indexName": "client_id_1_candidature_id_1_update_at_-1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "candidature_id": [
            
          ],
          "update_at": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "candidature_id": [
            "[MinKey, MaxKey]"
          ],
          "update_at": [
            "[MaxKey, MinKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "disponibilidad": newNumberInt("-1"),
          "estado": newNumberInt("1")
        },
        "indexName": "client_id_1_disponibilidad_-1_estado_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "disponibilidad": [
            
          ],
          "estado": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "disponibilidad": [
            "[MaxKey, MinKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "direccion": newNumberInt("1"),
          "estado": newNumberInt("1")
        },
        "indexName": "client_id_1_direccion_1_estado_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "direccion": [
            
          ],
          "estado": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "direccion": [
            "[MinKey, MaxKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "direccion": newNumberInt("1"),
          "disponibilidad": newNumberInt("-1"),
          "estado": newNumberInt("1")
        },
        "indexName": "client_id_1_direccion_1_disponibilidad_-1_estado_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "direccion": [
            
          ],
          "disponibilidad": [
            
          ],
          "estado": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "direccion": [
            "[MinKey, MaxKey]"
          ],
          "disponibilidad": [
            "[MaxKey, MinKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "cp": newNumberInt("1")
        },
        "indexName": "client_id_1_cp_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "cp": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "cp": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "disponibilidad": newNumberInt("-1"),
          "cp": newNumberInt("1")
        },
        "indexName": "client_id_1_disponibilidad_-1_cp_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "disponibilidad": [
            
          ],
          "cp": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "disponibilidad": [
            "[MaxKey, MinKey]"
          ],
          "cp": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "update_at": newNumberInt("-1"),
          "estado": newNumberInt("1")
        },
        "indexName": "client_id_1_update_at_-1_estado_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "update_at": [
            
          ],
          "estado": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "update_at": [
            "[MaxKey, MinKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "update_at": newNumberInt("-1"),
          "disponibilidad": newNumberInt("1"),
          "estado": newNumberInt("1")
        },
        "indexName": "client_id_1_update_at_-1_disponibilidad_1_estado_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "update_at": [
            
          ],
          "disponibilidad": [
            
          ],
          "estado": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "update_at": [
            "[MaxKey, MinKey]"
          ],
          "disponibilidad": [
            "[MinKey, MaxKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "update_at": newNumberInt("1"),
          "estado": newNumberInt("1")
        },
        "indexName": "client_id_1_update_at_1_estado_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "update_at": [
            
          ],
          "estado": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "update_at": [
            "[MinKey, MaxKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "update_at": newNumberInt("-1"),
          "candidature.estado": newNumberInt("1"),
          "estado": newNumberInt("1")
        },
        "indexName": "client_id_1_update_at_-1_candidature.estado_1_estado_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "update_at": [
            
          ],
          "candidature.estado": [
            
          ],
          "estado": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "update_at": [
            "[MaxKey, MinKey]"
          ],
          "candidature.estado": [
            "[MinKey, MaxKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "create_at": newNumberInt("-1")
        },
        "indexName": "client_id_1_create_at_-1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "create_at": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "create_at": [
            "[MaxKey, MinKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "especialidad": newNumberInt("1"),
          "estado": newNumberInt("1")
        },
        "indexName": "client_id_1_especialidad_1_estado_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "especialidad": [
            
          ],
          "estado": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "especialidad": [
            "[MinKey, MaxKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "direccion": newNumberInt("1")
        },
        "indexName": "client_id_1_direccion_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "direccion": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "direccion": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "candidatura_seleccionada": newNumberInt("1")
        },
        "indexName": "client_id_1_candidatura_seleccionada_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "candidatura_seleccionada": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "candidatura_seleccionada": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "provincia": newNumberInt("1")
        },
        "indexName": "client_id_1_provincia_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "provincia": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "provincia": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "candidatura_seleccionada": newNumberInt("1"),
          "estado": newNumberInt("1"),
          "direccion": newNumberInt("1")
        },
        "indexName": "client_id_1_candidatura_seleccionada_1_estado_1_direccion_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "candidatura_seleccionada": [
            
          ],
          "estado": [
            
          ],
          "direccion": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "candidatura_seleccionada": [
            "[MinKey, MaxKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ],
          "direccion": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "client_id": newNumberInt("1"),
          "update_at": newNumberInt("-1"),
          "estado": newNumberInt("1"),
          "matching": newNumberInt("1")
        },
        "indexName": "client_id_1_update_at_-1_estado_1_matching_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "client_id": [
            
          ],
          "update_at": [
            
          ],
          "estado": [
            
          ],
          "matching": [
            
          ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": newNumberInt("2"),
        "direction": "forward",
        "indexBounds": {
          "client_id": [
            "[\"800a0d60-b5fe-11ea-ae04-42010a840222\", \"800a0d60-b5fe-11ea-ae04-42010a840222\"]"
          ],
          "update_at": [
            "[MaxKey, MinKey]"
          ],
          "estado": [
            "[MinKey, MaxKey]"
          ],
          "matching": [
            "[MinKey, MaxKey]"
          ]
        }
      }
    }
  ]
}

It strikes me to see that it did not match the index: indexFilterSet: false,

But in a winning plan it has an index with the name of the index with which it should have matched: winningPlan.inputStage.indexName: "client_id"

Can someone give me an explanation and help me to use my indexes correctly?

Thanks in advance!

CodePudding user response:

The short answer is that the indexFilterSet field is not an indication of whether or not the database used an index. The winningPlan section of the explain plan confirms the database did use one as expected.

indexFilterSet

The indexFilterSet field is defined here in the documentation as follows:

A boolean that specifies whether MongoDB applied an index filter for the query shape.

Index filters, also known as 'plan cache filters' are a way for users to manually influence the query plan selection process. They are the server-side equivalent of client-side hints, though the two behave a bit differently.

Index filters effectively restrict the set of indexes that the database will consider for usage when planning a given query shape. The indexFilterSet field in explain output will only be true if an index filter has already been manually configured for the query shape:

test> db.foo.find({x:1}).explain().queryPlanner.indexFilterSet
false
test> db.runCommand({planCacheSetFilter:"foo", query:{x:1}, indexes:[{x:1}]})
{ ok: 1 }
test> db.foo.find({x:1}).explain().queryPlanner.indexFilterSet
true

Under normal operation, index filters and hints should not be used. The database itself is responsible for planning and executing queries and there is usually no need to help it do so. Indeed it is very uncommon to see index filters set in environments.

winningPlan and Index Usage

The winningPlan document is the one that contains details the plan selected by the query optimizer. This is what you want to look at to determine what index (if any) was selected by the query planner for the query shape being explained.

As noted in the question, in your case the database selected a query plan using a tightly bounded index scan on the "client_id" index. This seems to be correct and normal, so there should be no problems or concerns from that regard.

Observations & Recommendations

That said, there are some other concerns that come to mind when looking at this explain output.

Most notably, there are 28 indexes that were considered for use for this query shape. I would recommend reviewing "Create Compound Indexes to Support Several Different Queries". It seems that you may be able to greatly reduce the set of indexes required to support your workload by removing redundant ones.

As a simple example, the { "client_id"S 1 } index that was selected in the winningPlan is itself redundant. Any of the 27 other indexes that start with that field could have likely all have been used just as efficiently. Reducing the number of indexes will have benefits such as reducing the overhead when writing documents, reducing the number of items competing for space in memory, and simplifying the process for the query planner. The last of those things may be particularly relevant in your situation if you are currently examining explain output.

There is additional material (article and video) regarding Unnecessary Indexes here.

  • Related