Wrong index used: date-index is used instead of boolean/date-index

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Major - P3
    • None
    • Affects Version/s: 3.6.3, 4.0.3
    • Component/s: None
    • None
    • ALL
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None

      I have documents with the following structure:

      {{{_id:"asdf", consumed: true, timestamp: ISODate("2018-12-10T11:57:57.960+0000") }}}

      For querying I have two different (non partial) indices:

      1. "timestamp_-1" (timestamp ascending)
      2. "consumed_1_timesamp_-1 (compound index with consumed ascending and timestamp ascending)

       

      When I create a query like 

      find({consumed: false, timestamp: { "$lte" : ISODate("2018-12-10T11:57:57.960+0000")}}

      the first index is used, which leads to bad performance.

       

      In what case would be for this query the first index better than the second index? Since consumed is a Boolean, using the second index will always look at equal or lower elements - in my case 17.000 instead of 17.000.000 elements.

       

      Attached is a explain('allPlansExecution') query. I added here a limit(400) query, since the evaluation would take several minutes when the first index is used.

       

      I know that i can hint to the second index, but in this case it should be very trivial for mongodb-server to detect that the second index is better than the first index.

        1. explain.json
          13 kB
          Valentin S
        2. getIndexes.json
          1.0 kB
          Valentin S
        3. explain-index-1.json
          9 kB
          Valentin S
        4. explain-long.json
          10 kB
          Valentin S
        5. explain-with-hint.json
          3 kB
          Valentin S

            Assignee:
            Danny Hatcher (Inactive)
            Reporter:
            Valentin S
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: