title: MongoDB performance slow with C# driver, not in commandline
body: <p>I'm exploring MongoDB to check if we can/will use it as a noSQL store for our product.
  Everything appears to be working fine, but it's rather slow in our C# application. Queries take about 120 to 140ms to return the results, which is unacceptable for our application (more as 10 times slower compared to MS SQL).
  At first I thought it might have something to do with the <code>$in</code> operator and the <code>Guid</code> fields I was filtering on, but this isn't the case.</p>
  
  <p>At the moment I have created a small test to measure the performance of a specific query. The method looks like this:</p>
  
  <pre><code>public IQueryable&lt;T&gt; QueryTest&lt;T&gt;(string collection, IEnumerable&lt;Guid&gt; shouldBeIn, string fieldName)
  {
      var dataModelCollection = MongoDatabase.GetCollection&lt;T&gt;(collection);
      var findResult = dataModelCollection
                              .FindAll()
                              .ToList();  //Doing this on purpose, so the query is executed immediatly. Just for this test!
                                          //This action takes about 110~140ms.
      return findResult.AsQueryable();
  }
  </code></pre>
  
  <p>Obviously this is test code as I wouldn't to a <code>ToList()</code> in real code and return an <code>IQueryable</code> later on.</p>
  
  <p>When I run this query in the MongoDB console it returns the result in 0ms (according to the explain)</p>
  
  <pre><code>&gt; db.StoreData.find().explain()
  {
          "cursor" : "BasicCursor",
          "isMultiKey" : false,
          "n" : 21,
          "nscannedObjects" : 21,
          "nscanned" : 21,
          "nscannedObjectsAllPlans" : 21,
          "nscannedAllPlans" : 21,
          "scanAndOrder" : false,
          "indexOnly" : false,
          "nYields" : 0,
          "nChunkSkips" : 0,
          "millis" : 0,
          "server" : "mymongodbserver:27017",
          "filterSet" : false
  }
  </code></pre>
  
  <p>There's an index specified at the <code>_id</code> field and also one on the <code>StoreId</code> field, because I want to filter on the <code>StoreId</code> later on.</p>
  
  <p>Both queries, the C# and console, are executed on the same machine. I've also ran the test multiple times and have run the <code>.reIndex()</code> command also a couple of times.</p>
  
  <p>Any ideas on how to proceed? </p>
  
  <p><strong>edit</strong></p>
  
  <p>It's a very small database with only a little bit of test data/records. As requested, there are <strong>21 records</strong> returned, I think the result of the <code>explain()</code> method also tells this.
  I've ran the <code>toArray()</code> also. I don't see how this will help, but this is part of the result as copying 21 objects in here probably isn't very helpful.</p>
  
  <pre><code>{
          "_id" : ObjectId("542e847e048b8b0f704a7834"),
          "StoreId" : BinData(3,"/FQLn0k/hkSofM9WvEsNKQ=="),
          "Shelves" : [
                  {
                          "ShelveId" : BinData(3,"iCDhfhi6z0adh2haWjrzoQ=="),
                          "Name" : "Shelve 1",
                          "Type" : 0
                  }
          ],
          "Doors" : [ ]
  },
  {
          "_id" : ObjectId("542e847e048b8b0f704a7835"),
          "StoreId" : BinData(3,"p7TkqeFrGEOAWtv0RZ4YjQ=="),
          "Shelves" : [ ],
          "Doors" : [ ]
  },
  </code></pre>
  
  <p><strong>edit2</strong></p>
  
  <p>I've added some more data in my <code>StoreData</code> collection to see if returning a lot (100.000) documents would be faster or slower.
  In the MongoDB client (<code>mongodb.exe</code>) I get enormous fast results.</p>
  
  <blockquote>
  <pre><code>&gt; db.StoreData.find().explain()
  {
          "cursor" : "BasicCursor",
          "isMultiKey" : false,
          "n" : 100021,
          "nscannedObjects" : 100021,
          "nscanned" : 100021,
          "nscannedObjectsAllPlans" : 100021,
          "nscannedAllPlans" : 100021,
          "scanAndOrder" : false,
          "indexOnly" : false,
          "nYields" : 781,
          "nChunkSkips" : 0,
          "millis" : 53,
          "server" : "cp-crossbario:27017",
          "filterSet" : false
  }
  </code></pre>
  </blockquote>
  
  <p>As you can see, returning about 100.000 results is happening in <strong>53 milisecconds</strong>.</p>
  
  <p>Running the <code>FindAll().ToList()</code> method via the MongoDB driver results in receiving all the document in <strong>5075 miliseconds</strong>. Quite the difference!</p>
  
  <p>A bit more background information on the machines I'm running it on:
  I've set up 2 Azure Standard_A2 machines (2 cores, 3.5GB memory) in the region West Europe.</p>
  
  <p>One of the machines is running the MongoDB database. I've installed this on <code>C:\MongoDB</code> and the data directory on <code>C:\MongoDB\bin\data</code>. This is just for testing purposes. No real production system should install there date on this drive of course.</p>
  
  <p>The testing database is about 135MB in size.</p>
  
  <p>On the other machine I've got the testing clients installed, the console application using the MongoDB C# driver and the MongoDB.exe. Both connect to the database via the same HTTP endpoint.</p>
  
  <p><strong>edit3</strong></p>
  
  <p>I've just ran the <code>QueryTest</code> method without the <code>&lt;T&gt;</code>, so now I'm returning the 'raw' <code>BsonDocument</code>s. The weird thing is, it's even slower. The 100.000 documents now take ~8000ms to return, while returning the POCOs they return in ~5000ms.</p>