I am wondering how following queries varies in performance. I understand that bulk insert executes in sequence and normal/array inserts also executes in sequence.
BULK INSERT
var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { _id: 1, item: "abc123", status: "A", soldQty: 5000 } );
bulk.insert( { _id: 2, item: "abc456", status: "A", soldQty: 150 } );
bulk.insert( { _id: 3, item: "abc789", status: "P", soldQty: 0 } );
bulk.execute( { w: "majority", wtimeout: 5000 } );
Normal INSERT
db.items.insert( { _id: 1, item: "abc123", status: "A", soldQty: 5000 } );
db.items.insert( { _id: 2, item: "abc456", status: "A", soldQty: 150 } );
db.items.insert( { _id: 3, item: "abc789", status: "P", soldQty: 0 } );
Array INSERT
var array_insert = [
{ _id: 1, item: "abc123", status: "A", soldQty: 5000 } ,
{ _id: 2, item: "abc456", status: "A", soldQty: 150 } ,
{ _id: 3, item: "abc789", status: "P", soldQty: 0 }
] ;
db.items.insert( array_insert );
Can any one explain what is the difference interms of performance or benefits for the above queries ?
Bulk insert is faster because inserts are sent all at once. Compare it to the regular flow where you send one insert, wait for server reply and only then send next insert.
It takes server the same amount of work to insert all the things, regardless of the mode (bulk vs. regular). In regular mode time is lost on client while it waits for server to respond. Not necessarily because server is slow. Network may be slow too.
Batching requests is a common way of speeding up application, regardless of the database used. See, for example, pipelining in Redis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With