通过publish_target,类型和状态对80'000个帖子进行分组时,以下代码需要30秒。
有没有明显的方法来改善加载时间?
//by publish target
$collection = $this->mongoDB->Post;
$keys = array('publish_target' => true);
$initial = array("count" => 0);
$reduce = "function (obj, prev) { prev.count++; }";
$result = $collection->group($keys, $initial, $reduce);
foreach ($result['retval'] as $value) {
$this->results['Post']['publish_target'][] = array('key' => $value['publish_target'], 'value' => $value['count']);
}
// by type
$collection = $this->mongoDB->Post;
$keys = array('type' => true);
$initial = array("count" => 0);
$reduce = "function (obj, prev) { prev.count++; }";
$result = $collection->group($keys, $initial, $reduce);
foreach ($result['retval'] as $value) {
$this->results['Post']['type'][] = array('key' => $value['type'], 'value' => $value['count']);
}
// by status
$collection = $this->mongoDB->Post;
$keys = array('status' => true);
$initial = array("count" => 0);
$reduce = "function (obj, prev) { prev.count++; }";
$result = $collection->group($keys, $initial, $reduce);
foreach ($result['retval'] as $value) {
$this->results['Post']['status'][] = array('key' => $value['status'], 'value' => $value['count']);
}
固定
$ops = array(
array(
'$group' => array(
'_id' => array($arrayKey => '$'.$arrayKey),
'count' => array('$sum' => 1)
)
)
);
$retrieved = $collection->aggregate($ops);
答案 0 :(得分:0)
首先,这不是您正在运行的单个聚合,您根据前一个的输出运行三个,并且不清楚为什么要这样做而不是仅仅通过发布_目标,类型和状态进行分组。单一聚合。
其次,您正在使用通过Javascript实现的group()函数,而不是在服务器上本机运行的聚合框架。您需要的唯一查询/命令是:
db.post.aggregate({$group:{
_id: { publish_target : "$publish_target" },
count: {$sum : 1}
} );
现在,您将在服务器上一次性获取这些计数。