Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is MongoDB slower to insert Records only when collection does not exist?

I was doing the benchmarking of MongoDB 3.2.17 for fun and cannot understand the reason for this behavior.

  • When I create an empty collection prior to doing insertion

    MongoDB x 906 ops/sec ±2.78% (75 runs sampled)

  • When I don't create any empty collection and just simply run insertMany

    MongoDB x 87.81 ops/sec ±94.31% (71 runs sampled) // Error Rate is high, why?

My Code that uses Benchmark.js so that you can point if I made some mistake there

var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
const MongoClient = require('mongodb').MongoClient;
var collectionMongodb = 'testrecords2';
Promise.all([
    MongoClient.connect('mongodb://localhost:27017/testme') 
]).then((clients) => {
    var nativeCollection = clients[0].db('testmongodb').collection(collectionMongodb)
    var records = [];
    for (var i = 0; i < 100; i++) {
        records.push({
            name: 'bugwheels' + i,
            interest: 'Not Many',
            job: 'Useless'
        })
    }
    suite
    .add('MongoDB', {
        defer: true,
        fn: function(def) {
            nativeCollection.insertMany(records, (e, r) => {
                def.resolve();
                // console.log(r)
            })
        }
    })
    .on('cycle', function(event) {
        console.log(String(event.target));
    })
    .on('complete', function() {
        console.log('Fastest is ' + this.filter('fastest').map('name'));
//          nativeCollection.drop()
    })
    .run({ 'async': true });    
})

Please, let me know what has gone wrong?

My StorageEngine

{
    "name" : "wiredTiger",
    "supportsCommittedReads" : true,
    "persistent" : true
 }

I started mongoDB using:

mongod --dbpath ./db
like image 744
bugwheels94 Avatar asked Mar 05 '18 12:03

bugwheels94


1 Answers

This is quite simple. You are inserting the same 100 records in every run.

When you drop the collection between every run, you are measuring how long it takes to drop collection and then insert 100 documents into it.

When you comment out dropping the collection, you insert the 100 records in the first run, but then your subsequent runs are each attempting to insert the exact same 100 documents into the same collection and they are all getting the error:

exception: E11000 duplicate key error collection: testmongodb.testrecords2 index: _id_ dup key: { : ObjectId('5aa19388df671d3a065076f5') } code:DuplicateKey

I assume that the way you are creating empty collections actually causes the amount of work to vary significantly so one thing you should do is make sure you are benchmarking correctly by generating unique records every time.

like image 179
Asya Kamsky Avatar answered Sep 27 '22 17:09

Asya Kamsky