Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1442

C# driver does not honor limit when executing find

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.0.1
    • Component/s: Operations, Performance
    • Labels:
      None
    • Environment:
      .NET 4.5, Azure, Windows

      I have been porting our repository layer to use the new driver (2.0.1), and in doing so we have noticed some performance degradation with the new driver. Upon investigating where we potentially went wrong I went back to the old code to capture the queries logged in the profiler database to compare against what is now being generated by the new driver.

      NOTE: We are using database version 3.0.4.

      In the queries generated by the new driver the LIMIT stage of the query is no longer honored, and thus not reported as part of the query execution plan.

      (new driver code/log below...)

      Legacy driver code:

                      var query = Query<Tenant>.EQ(e => e.Name, name);
                      var found = this.retryHandler.ExecuteFunc(() => this.Collection.FindOne(query));
                      return found;
      

      Logged in profiler database for legacy code:

      {
          "op" : "query",
          "ns" : "dfl.tenants",
          "query" : {
              "Name" : "dev"
          },
          "ntoskip" : 0,
          "nscanned" : 1,
          "nscannedObjects" : 1,
          "keyUpdates" : 0,
          "writeConflicts" : 0,
          "numYield" : 0,
          "locks" : {
              "Global" : {
                  "acquireCount" : {
                      "r" : NumberLong(2)
                  }
              },
              "MMAPV1Journal" : {
                  "acquireCount" : {
                      "r" : NumberLong(1)
                  }
              },
              "Database" : {
                  "acquireCount" : {
                      "r" : NumberLong(1)
                  }
              },
              "Collection" : {
                  "acquireCount" : {
                      "R" : NumberLong(1)
                  }
              }
          },
          "nreturned" : 1,
          "responseLength" : 103,
          "millis" : 0,
          "execStats" : {
              "stage" : "LIMIT",
              "nReturned" : 1,
              "executionTimeMillisEstimate" : 0,
              "works" : 1,
              "advanced" : 1,
              "needTime" : 0,
              "needFetch" : 0,
              "saveState" : 0,
              "restoreState" : 0,
              "isEOF" : 1,
              "invalidates" : 0,
              "limitAmount" : 0,
              "inputStage" : {
                  "stage" : "FETCH",
                  "nReturned" : 1,
                  "executionTimeMillisEstimate" : 0,
                  "works" : 1,
                  "advanced" : 1,
                  "needTime" : 0,
                  "needFetch" : 0,
                  "saveState" : 0,
                  "restoreState" : 0,
                  "isEOF" : 1,
                  "invalidates" : 0,
                  "docsExamined" : 1,
                  "alreadyHasObj" : 0,
                  "inputStage" : {
                      "stage" : "IXSCAN",
                      "nReturned" : 1,
                      "executionTimeMillisEstimate" : 0,
                      "works" : 1,
                      "advanced" : 1,
                      "needTime" : 0,
                      "needFetch" : 0,
                      "saveState" : 0,
                      "restoreState" : 0,
                      "isEOF" : 1,
                      "invalidates" : 0,
                      "keyPattern" : {
                          "Name" : 1
                      },
                      "indexName" : "Name_1",
                      "isMultiKey" : false,
                      "direction" : "forward",
                      "indexBounds" : {
                          "Name" : [ 
                              "[\"dev\", \"dev\"]"
                          ]
                      },
                      "keysExamined" : 1,
                      "dupsTested" : 0,
                      "dupsDropped" : 0,
                      "seenInvalidated" : 0,
                      "matchTested" : 0
                  }
              }
          },
          "ts" : ISODate("2015-10-14T18:34:08.919Z"),
          "client" : "127.0.0.1",
          "allUsers" : [],
          "user" : ""
      }
      

      New driver code:

                      var filter = Builders<Tenant>.Filter.Eq(e => e.Name, name);
                      var find = this.Collection.Find(filter).Limit(1);
                      return await this.retryHandler.ExecuteFuncAsync(() => find.SingleOrDefaultAsync()).ConfigureAwait(false);
      

      Logged in profiler database for new code:

      {
          "op" : "query",
          "ns" : "dfl.tenants",
          "query" : {
              "$query" : {
                  "Name" : "dev"
              }
          },
          "ntoreturn" : 1,
          "ntoskip" : 0,
          "nscanned" : 1,
          "nscannedObjects" : 1,
          "keyUpdates" : 0,
          "writeConflicts" : 0,
          "numYield" : 0,
          "locks" : {
              "Global" : {
                  "acquireCount" : {
                      "r" : NumberLong(2)
                  }
              },
              "MMAPV1Journal" : {
                  "acquireCount" : {
                      "r" : NumberLong(1)
                  }
              },
              "Database" : {
                  "acquireCount" : {
                      "r" : NumberLong(1)
                  }
              },
              "Collection" : {
                  "acquireCount" : {
                      "R" : NumberLong(1)
                  }
              }
          },
          "nreturned" : 1,
          "responseLength" : 103,
          "millis" : 0,
          "execStats" : {
              "stage" : "FETCH",
              "nReturned" : 1,
              "executionTimeMillisEstimate" : 0,
              "works" : 1,
              "advanced" : 1,
              "needTime" : 0,
              "needFetch" : 0,
              "saveState" : 0,
              "restoreState" : 0,
              "isEOF" : 1,
              "invalidates" : 0,
              "docsExamined" : 1,
              "alreadyHasObj" : 0,
              "inputStage" : {
                  "stage" : "IXSCAN",
                  "nReturned" : 1,
                  "executionTimeMillisEstimate" : 0,
                  "works" : 1,
                  "advanced" : 1,
                  "needTime" : 0,
                  "needFetch" : 0,
                  "saveState" : 0,
                  "restoreState" : 0,
                  "isEOF" : 1,
                  "invalidates" : 0,
                  "keyPattern" : {
                      "Name" : 1
                  },
                  "indexName" : "Name_1",
                  "isMultiKey" : false,
                  "direction" : "forward",
                  "indexBounds" : {
                      "Name" : [ 
                          "[\"dev\", \"dev\"]"
                      ]
                  },
                  "keysExamined" : 1,
                  "dupsTested" : 0,
                  "dupsDropped" : 0,
                  "seenInvalidated" : 0,
                  "matchTested" : 0
              }
          },
          "ts" : ISODate("2015-10-14T20:18:23.168Z"),
          "client" : "127.0.0.1",
          "allUsers" : [],
          "user" : ""
      }
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            bothead bothead
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: