MongoDB PHP findAndModify Multiple Performance

时间:2012-11-30 16:08:01

标签: php mongodb

我在要处理的名为Reports的集合中有一个文档。我做了像

这样的查询
$collectionReports->find(array('processed' => 0)) 

(50到2000件之间的任何地方)。我处理它们我需要的方式并将结果插入到另一个集合中,但我需要更新原始报告以将处理设置为当前系统时间。现在它看起来像:

$reports = $collectionReports->find(array('processed' => 0));
$toUpdate = array();
foreach ($reports as $report) {
    //Perform the operations on them now
    $toUpdate = $report['_id'];
}
foreach ($toUpdate as $reportID) {
    $criteria = array('_id' => new MongoId($reportID));
    $data = array('$set' => array('processed' => round(microtime(true)*1000)));
    $collectionReports->findAndModify($criteria, $data);
}

我的问题在于它非常低效。对于2000个报告,处理报告并将其插入到集合中可能需要700毫秒,但只更新处理时间至少需要1500毫秒才能获得相同的2000报告。有什么提示可以加快速度吗?提前谢谢。

编辑:处理的时间不必是精确的,它可以是脚本运行的时间(+/- 10秒左右),如果可以获取对象($ report)和像这样直接更新时间,这比在第一个foreach之后搜索更好。

1 个答案:

答案 0 :(得分:0)

感谢Sammaye,从findAndModify()更改为update()似乎工作得更好更快。