以下のコードは、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);