dsizmmwnm56437180 2016-03-14 10:37
浏览 61
已采纳

Yii2:MongoDB选择跳过大值

I'm writing yii2 command controller, that exports mongodb data to sphinx (as csv). MongoDB controller contains about 9M lines, and I've added a while cycle.

public function actionExportSphinx()
{
    $query = new MongoQuery;
    $count = (int) $query->select(['_id'])
                   ->from('test')
                   ->count();
    $i = 0;

    while($i < $count) {
        $rows = self::getExportData($i);
        self::printMongoRow($rows);
        unset($rows);
        $i += 100000;
    }
}

Each next cycle loop is more slower than previous one. And on $i = 500000 mongo excepts timeout... I know, that it is a mongo skip problem, but I don't know any solutions of this.

UPD: Added self::getExportData() and self::printMongoRow() methods.

public function printMongoRow($rows)
{
    foreach ($rows as $row) {
        printf("%d,\"%s\",\"%s\",\"%s\",\"%s\",%d,%d,%d
",
            $row['_id'],
            (isset($row['lastname']) ? str_replace('"', "", $row['lastname']) : ""),
            (isset($row['firstname']) ? str_replace('"', "", $row['firstname']) : ""),
            (isset($row['middlename']) ? str_replace('"', "", $row['middlename']) : ""),
            (isset($row['town']) ? str_replace('"', "", $row['town']['title']) : ""),
            (isset($row['birthday']) ? $row['birthday'] : 0),
            (isset($row['birthmonth']) ? $row['birthmonth'] : 0),
            (isset($row['birthyear']) ? $row['birthyear'] : 0)
            );
    }
}

public function getExportData($i)
{
    $query = new MongoQuery;
        $query->select(['lastname', 'firstname', 'middlename', 'town', 'birthday', 'birthmonth', 'birthyear'])
                ->from('test')
                ->limit(100000)
                ->offset($i);
        return $query->all();
}
  • 写回答

1条回答 默认 最新

  • dtpf76658 2016-03-14 15:37
    关注

    The answer is given in comments

    In case when you iterate over the whole MongoDb collection it is preferrable to use mongo query cursor directly instead of the skip-limit approach.

    When cursor is used you perform a query search only once and the results will be fetched into memory iteratively.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改