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

Range query with compound multikey index scans more objects than needed

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.0.5, 2.0.6
    • Component/s: Index Maintenance, Querying
    • Environment:
      Windows, Linux
    • ALL

      I noticed very strange behavior IMO when range queries used with compound index where one of indexed fields is array.
      Example, I have collection of documents with HierarchyLevel as integer and Parents as array of strings, and other fieds. I make simple query:

      {"HierarchyLevel" : {"$gte" : 1, "$lte" : 2}}

      When I use simple key index or compound with simple type fields query optimizer runs as expected:

      {
              "cursor" : "BtreeCursor HierarchyLevel_1",
              "nscanned" : 153,
              "nscannedObjects" : 153,
              "n" : 153,
              "millis" : 1,
              "nYields" : 0,
              "nChunkSkips" : 0,
              "isMultiKey" : false,
              "indexOnly" : false,
              "indexBounds" : {
                      "HierarchyLevel" : [
                              [
                                      1,
                                      2
                              ]
                      ]
              }
      }
      
      {
              "cursor" : "BtreeCursor HierarchyLevel_1_Path_1",
              "nscanned" : 153,
              "nscannedObjects" : 153,
              "n" : 153,
              "millis" : 1,
              "nYields" : 0,
              "nChunkSkips" : 0,
              "isMultiKey" : false,
              "indexOnly" : false,
              "indexBounds" : {
                      "HierarchyLevel" : [
                              [
                                      1,
                                      2
                              ]
                      ],
                      "Path" : [
                              [
                                      {
                                              "$minElement" : 1
                                      },
                                      {
                                              "$maxElement" : 1
                                      }
                              ]
                      ]
              }
      }
      

      But when I use compound index with array field, $lte condition is simply ignored, and all index entries from lower bound is scanned

      {
              "cursor" : "BtreeCursor HierarchyLevel_1_Parents_1",
              "nscanned" : 283630,
              "nscannedObjects" : 283630,
              "n" : 153,
              "millis" : 836,
              "nYields" : 0,
              "nChunkSkips" : 0,
              "isMultiKey" : true,
              "indexOnly" : false,
              "indexBounds" : {
                      "HierarchyLevel" : [
                              [
                                      1,
                                      1.7976931348623157e+308
                              ]
                      ],
                      "Parents" : [
                              [
                                      {
                                              "$minElement" : 1
                                      },
                                      {
                                              "$maxElement" : 1
                                      }
                              ]
                      ]
              }
      }
      

      In this case count of scanned objects is bigger that count of actual documents, and query took lot of time.

        1. example_documents.js
          3 kB
          Sergejs Degtjars

            Assignee:
            hari.khalsa@10gen.com hari.khalsa@10gen.com
            Reporter:
            sergejs.degtjars@abcsoftware.lv Sergejs Degtjars
            Votes:
            3 Vote for this issue
            Watchers:
            21 Start watching this issue

              Created:
              Updated:
              Resolved: