Update vs Save - performance

2,439 views
Skip to first unread message

Ofer Cohen

unread,
Sep 18, 2013, 8:48:49 AM9/18/13
to mongod...@googlegroups.com
Hello

I'm building app based on PHP and MongoDB. The app have massive update of documents, and must be one at a time, because of processing of data for each line separately.
Which method has better performance in this case - Update or Save?

Thanks, Ofer

Keith Branton

unread,
Sep 18, 2013, 10:20:42 AM9/18/13
to mongod...@googlegroups.com
If your updates are small, update should be faster because it does less work, however there are many mutations that update cannot perform, so whether you can use it depends on what kind of updates you are trying to perform.

Assuming you can use update, and if performance is an issue, you should benchmark the performance of each approach for your specific workload, but my gut says that smaller updates will always be faster than saves.

Personally I always save records in my application because it is easier with the way I use my data (I always load before I change) and I haven't found performance of these saves to be causing an bottleneck yet.


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jeff Lee

unread,
Sep 18, 2013, 2:01:47 PM9/18/13
to mongod...@googlegroups.com
Keith's point about the size of the update is important.

If you're doing a lot of updates that involve changing the document sizes a move may be required which will cause performance to suffer.  If you know beforehand that the updates are likely to significantly increase the doc sizes, you may want to consider powerOf2Sizes or setting and unsetting a placeholder field on update.

You'll also want to be sure to use $set with update otherwise you're essentially doing the same thing as save.

Ofer Cohen

unread,
Sep 21, 2013, 2:33:13 PM9/21/13
to mongod...@googlegroups.com
Hey
Thanks for feedback.
Few more notes:
  • I'm loading the data before to make some manipulations on the data.
  • Save command make the update by query _id (not the shard key)
  • Update command make the the update by the shard key.
Is the update approach will be faster than save because of the shard key?

Jeff Lee

unread,
Sep 21, 2013, 2:50:08 PM9/21/13
to mongod...@googlegroups.com
Save is implemented in the client and just wraps the right command based on whether or not you include the _id.


If the collection is sharded, adding both the _id and the shard key should allow mongos to route the upsert to the correct shard.

Asya Kamsky

unread,
Sep 22, 2013, 3:25:21 AM9/22/13
to mongod...@googlegroups.com
You *must* set the shard key when doing an insert or an update. Otherwise you will get an error.

From: Ofer Cohen <ofer...@gmail.com>
Date: Sat, 21 Sep 2013 11:33:13 -0700 (PDT)
Subject: Re: [mongodb-user] Update vs Save - performance

Ofer Cohen

unread,
Sep 22, 2013, 3:32:19 AM9/22/13
to mongod...@googlegroups.com
Hey

Thanks for feedback.
What about save command? Is it use shard key?

Thanks, Ofer



You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/dZzMXY5v_Rk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.

Sam Millman

unread,
Sep 22, 2013, 3:52:10 AM9/22/13
to mongod...@googlegroups.com
save is just a wrapper so yes

Ofer Cohen

unread,
Sep 22, 2013, 5:11:12 AM9/22/13
to mongod...@googlegroups.com
So the update will be faster *just* because it's updating less data, is that correct?

Asya Kamsky

unread,
Sep 22, 2013, 10:03:36 PM9/22/13
to mongod...@googlegroups.com
First, for *your* data and your use case, you should try both and see what the performance is.

In theory, update will be faster. They may be equal.

Save is not thread safe though - two threads manipulating the same document could get in each other's way. So that's another strong argument against using save.

Asya


From: Ofer Cohen <ofer...@gmail.com>
Date: Sun, 22 Sep 2013 11:11:12 +0200
Reply all
Reply to author
Forward
0 new messages