performance - Milti-insert in MongoDB. Which way is more efficient? -
// first approach (var = 0; < 1000000; i++) { db.testdata.insert({number: i}); } // second approach (var = 0, arr = []; < 1000000; i++) { arr.push({number: i}); } db.testdata.insert(arr);
which way more efficient? second approach make single request database?
definitely second 1 way faster big number of inserts. because make 1 db call.
but not hard test yourself.
var x = 100000; var start = new date(); (var = 0; < x; i++) { db.coll1.insert({number: i}); } var time1 = new date() - start;
and second one
var start = new date(); (var = 0, arr = []; < x; i++) { arr.push({number: i}); } db.coll2.insert(arr); var time2 = new date() - start;
and suggested in comments, can try bulk insert (which in opinion doing same 2, might wrong):
var start = new date(); var bulk = db.coll3.initializeunorderedbulkop(); (var = 0; < x; i++) { bulk.insert({number: i}); } bulk.execute(); var time3 = new date() - start;
and here results machine time1 = 33846
, time2 = 2191
andtime3 = 2203
.
as see difference between 2 , 3 negligible, 1 15 times longer (if increase x way bigger). if you, use documented bulk insert.
Comments
Post a Comment