下面的代码按publish_target、type和status对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);
首先,您正在运行的不是单个聚合,而是基于前一个聚合的输出运行三个聚合,并且不清楚为什么要这样做,而不是在单个聚合中按publish_target、type和status分组。
其次,你使用的是通过Javascript实现的group()函数,而不是在服务器上本地运行的Aggregation Framework。您唯一需要的查询/命令是:
db.post.aggregate({$group:{
_id: { publish_target : "$publish_target" },
count: {$sum : 1}
} );
现在您将在服务器上获得这些计数。