title: How to improve MongoDB insert performance
body: <p><strong>The result:</strong></p>
  
  <p><strong>If you are operating on a dataset that is fault tolerant, or doing a one time process you can verify, changing WriteAcknowledge to Unacknowledged can help.</strong></p>
  
  <p><strong>Also, bulk operations are IsOrdered by default, which I was not aware off. Setting this to False actually makes the operation perform in bulk, otherwise it operates as one thread of updates.</strong></p>
  
  <hr>
  
  <p>MongoDB 3.0 / WiredTiger / C# Driver</p>
  
  <p>I have a collection with 147,000,000 documents, of which I am performing updates each second (hopefully) of approx. 3000 documents.</p>
  
  <p>Here is an example update:</p>
  
  <pre><code>"query" : {
      "_id" : BinData(0,"UKnZwG54kOpT4q9CVWbf4zvdU223lrE5w/uIzXZcObQiAAAA")
  },
  "updateobj" : {
      "$set" : {
          "b" : BinData(0,"D8u1Sk/fDES4IkipZzme7j2qJ4oWjlT3hvLiAilcIhU="),
          "s" : true
      }
  }
  </code></pre>
  
  <p>This is a typical update of which I my requirements are to be inserted at a rate of 3000 per second.</p>
  
  <p>Unfortunately these are taking twice as long, for instance the last update was for 1723 documents, and took 1061ms.</p>
  
  <p>The collection only has an index on the _id, no other indexes, and the average document size for the collection is 244 bytes, uncapped.</p>
  
  <p>The server has 64GB of memory, 12 threads. Insert performance is excellent with lower collection sizes, say around 50 million, but after about 80 million really starts to drop off.</p>
  
  <p>Could it be because the entire set does not sit in memory? Database is backed by RAID0 SSDs so IO performance should not become a bottleneck and if it was it should have shown this at the beginning?</p>
  
  <p>Would appreciate some guidance as I'm confident MongoDB can fulfill my rather meager requirements compared to some applications it is used in. There is not a substantial read rate on the database so Sharding would not improve matters, although perhaps I am wrong.</p>
  
  <p>Either way, the current insert rate is not good enough.</p>
  
  <p>Update: Here is the explain() of just the query...</p>
  
  <pre><code>"queryPlanner" : {
      "plannerVersion" : 1,
      "namespace" : "Collection",
      "indexFilterSet" : false,
      "parsedQuery" : {
          "_id" : {
              "$eq" : { "$binary" : "SxHHwTMEaOmSc9dD4ng/7ILty0Zu0qX38V81osVqWkAAAAAA", "$type" : "00" }
          }
      },
      "winningPlan" : {
          "stage" : "IDHACK"
      },
      "rejectedPlans" : []
  },
  "executionStats" : {
      "executionSuccess" : true,
      "nReturned" : 1,
      "executionTimeMillis" : 1,
      "totalKeysExamined" : 1,
      "totalDocsExamined" : 1,
      "executionStages" : {
          "stage" : "IDHACK",
          "nReturned" : 1,
          "executionTimeMillisEstimate" : 0,
          "works" : 2,
          "advanced" : 1,
          "needTime" : 0,
          "needFetch" : 0,
          "saveState" : 0,
          "restoreState" : 0,
          "isEOF" : 1,
          "invalidates" : 0,
          "keysExamined" : 1,
          "docsExamined" : 1
      },
      "allPlansExecution" : []
  },
  </code></pre>
  
  <p>The query it self is very fast, and the update operation takes about 25ish milliseconds, they are being pushed to Mongo by use of the BulkWriter: <code>await m_Collection.BulkWriteAsync(updates);</code></p>