Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-68672

Query using shard key and document ID does not use available index

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.2.21
    • Component/s: None
    • None
    • ALL

      I have a sharded collection with around ~ 200M documents, and an index on this collection: {datasetId: 1, _id: 1}, where datasetId is the shard key on the collection.

      When I query on the collection with the following query:

       

      find({datasetId: 'YYY', _id: ${gt: ObjectId('XXX')}}.sort({_id: 1})
      

      The query does not use the aforementioned index, it uses the {_id: 1} index, and takes a very long time to run, while greatly increasing the mongo servers cpu usage.

       

      Here are the query execution stats for a datasetId where ~ 40K documents satisfy the query, and an _id which is the oldest available that has specifed datasetId - which took 28.6 seconds:

      If I hint the index on the same query it uses it and runs very quickly scanning only the needed documents:

      Using any other operator than $gt /$gte works ($lt / $lte on the newest available ObjectId, $ne - all use the index with no need for a hint), the greater than operators are the only ones that do not use the index (example of a $lt query):

      I cannot hint at the index each in query of this nature, Why do only the $gt and $gte operators not use the index? seems they should act like any other operator and use the index if it is available?

        1. image-2022-08-09-13-42-22-395.png
          image-2022-08-09-13-42-22-395.png
          514 kB
        2. image-2022-08-09-16-23-11-039.png
          image-2022-08-09-16-23-11-039.png
          43 kB
        3. image-2022-08-09-16-23-38-511.png
          image-2022-08-09-16-23-38-511.png
          28 kB
        4. image-2022-08-09-16-24-30-226.png
          image-2022-08-09-16-24-30-226.png
          43 kB
        5. image-2022-08-09-16-32-04-424.png
          image-2022-08-09-16-32-04-424.png
          18 kB
        6. image-2022-08-09-16-32-24-886.png
          image-2022-08-09-16-32-24-886.png
          18 kB
        7. image-2022-08-09-16-32-59-723.png
          image-2022-08-09-16-32-59-723.png
          15 kB
        8. image-2022-08-09-16-34-22-817.png
          image-2022-08-09-16-34-22-817.png
          15 kB
        9. image-2022-08-09-16-36-06-516.png
          image-2022-08-09-16-36-06-516.png
          17 kB

            Assignee:
            eric.sedor@mongodb.com Eric Sedor
            Reporter:
            shachar.h@dataloop.ai Shachar Hochberg
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: