当按发布目标、类型和状态对 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);