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

Query index bounds are incorrect when _id used in compound index

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.6.8
    • Component/s: Index Maintenance, Querying
    • Labels:
      None
    • ALL
    • Hide

      Import the data from the sample export

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      gunzip -c records.json.gz |mongoimport --db index-test --collection workspaceFile
      

      Create indexes

          
      db.workspaceFile.createIndex(
        {
                  "_id" : 1, // <---- this seems to cause a problem, see executionStats below
                  "workspaceId" : 1,
                  "parent._id" : 1
        },
        {name:'testIndexA'}
      );
      db.workspaceFile.createIndex(
        {
                  "workspaceId" : 1,
                  "parent._id" : 1
        },
        {name:'testIndexB'}
      );   
      

       

      Define the query parameters

       
      var workspaceId = "ws-3";
      var fileId = ObjectId("5bd33d003bff4aac38456157");
      var fileFamilyQuery = {
        "$and": [{
            "workspaceId": workspaceId,
            "$or": [{
                "_id": fileId,
            }, {
                "parent._id": fileId
            }]
        }]
      };
      

       

      Run query against testIndexA

      db.workspaceFile.find(fileFamilyQuery).sort({"_id":-1}).hint("testIndexA").explain('executionStats')
      

      Inspect the indexBounds and see that the parameters are not set and the number of keys examined is too high

        
      "indexBounds" : {
      	"_id" : [
      		"[MaxKey, MinKey]"
      	],
      	"workspaceId" : [
      		"[MaxKey, MinKey]"
      	],
      	"parent._id" : [
      		"[MaxKey, MinKey]"
      	]
      },
      "keysExamined" : 500005,
      

      Run against testIndexB

       db.workspaceFile.find(fileFamilyQuery).sort({"_id":-1}).hint("testIndexB").explain('executionStats')
      

      Inspect the indexBounds and see the parameters are partially at least partially set (but still missing parent._id)

      "indexBounds" : {
        "workspaceId" : [
          "[\"ws-3\", \"ws-3\"]"
        ],
        "parent._id" : [
          "[MinKey, MaxKey]"
        ]
      },
      "keysExamined" : 2, 
      

       

       

      Show
      Import the data from the sample export Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml gunzip -c records.json.gz |mongoimport --db index-test --collection workspaceFile Create indexes db.workspaceFile.createIndex( { "_id" : 1, // <---- this seems to cause a problem, see executionStats below "workspaceId" : 1, "parent._id" : 1 }, {name: 'testIndexA' } ); db.workspaceFile.createIndex( { "workspaceId" : 1, "parent._id" : 1 }, {name: 'testIndexB' } );   Define the query parameters var workspaceId = "ws-3" ; var fileId = ObjectId( "5bd33d003bff4aac38456157" ); var fileFamilyQuery = { "$and" : [{ "workspaceId" : workspaceId, "$or" : [{ "_id" : fileId, }, { "parent._id" : fileId }] }] };   Run query against testIndexA db.workspaceFile.find(fileFamilyQuery).sort({ "_id" :-1}).hint( "testIndexA" ).explain( 'executionStats' ) Inspect the indexBounds and see that the parameters are not set and the number of keys examined is too high "indexBounds" : { "_id" : [ "[MaxKey, MinKey]" ], "workspaceId" : [ "[MaxKey, MinKey]" ], "parent._id" : [ "[MaxKey, MinKey]" ] }, "keysExamined" : 500005, Run against testIndexB db.workspaceFile.find(fileFamilyQuery).sort({ "_id" :-1}).hint( "testIndexB" ).explain( 'executionStats' ) Inspect the indexBounds and see the parameters are partially at least partially set ( but still missing parent._id ) "indexBounds" : { "workspaceId" : [ "[\" ws-3\ ", \" ws-3\ "]" ], "parent._id" : [ "[MinKey, MaxKey]" ] }, "keysExamined" : 2,    

      When I include _id inside a compound index, the query doesn't appear to bind the proper values to the index bounds for the query plan. I'm unsure if this is a bug or something I'm missing. Related StackOverflow Question

      I've saved some sample data to help reproduce for this issue available on Google Drive since it's too large for a Jira attachment.

            Assignee:
            daniel.hatcher@mongodb.com Danny Hatcher (Inactive)
            Reporter:
            mike@devnull.org Mike Cantrell
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: