Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between bulk insert and array insert in Mongo db operations

Tags:

mongodb

nosql

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 ?

like image 215
logan Avatar asked Oct 20 '25 22:10

logan


1 Answers

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.

like image 131
Sergio Tulentsev Avatar answered Oct 22 '25 20:10

Sergio Tulentsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!