开发者

Fastest way for updating: update() or save()?

I loop through the documents of my co开发者_开发知识库llection, do some stuff, and then update the database. But as I actually have all the data of the document I'm updating, would save() be faster than of update() if I do it that way?

foreach ($cursor as $doc) {
  $doc['new_field'] = 'value';
  $coll->save($doc);

  /* or (currently) */

  $coll->update(array('known_field' => $doc['known_field']), array('$set' => array('new_field' => 'value')));
}

Which way is faster?


::update should be faster because it update only some fields of a document. ::save save entire document and probably will slower.

In general better to use ::update where it possible, because if you using ::save possible concurrency problems. For example if two threads has loaded same document, update it and then trying to save. Lets say first thread has saved document. Then second thread rewrite changes of first thread and you lose updates of first thread. With atomic update you never get stuck on this problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜