php-on-couch 快速获取所有记录

php-on-couch get all records fast

使用 PHP-ON-COUCH,我尝试使用 php-on-couch 获取 couchdb 中的所有记录,但速度不快。

 require_once "lib/couch.php";
 require_once "lib/couchClient.php";
 require_once "lib/couchDocument.php";    

 $couch_dsn = "http://localhost:5984/";
 $couch_db  = "couch";

  $client = new couchClient($couch_dsn,$couch_db);
  $all_singers = $client->getAllDocs();
  foreach ( $all_singers->rows as $row ) {
    $doc = $client->getDoc($id);
    echo $doc->singer;
    echo $doc->title;
    echo $doc->description;
   }

还有其他方法可以正确执行此操作吗?

提前致谢

您没有正确使用这些功能。您现在正在做的事情非常慢,因为您获取了所有文档,然后您使用 (getDoc) 函数一个一个地再次获取它们。当你查询所有文档时,你会得到这样的结果:

{
    "total_rows": 0,
    "count": 0,
    "rows": [{
        "key": 1,
        "value": "value",
        "doc": {
            "singer": "Foo",
            "title": "bar"
        }
    }]
}

这是您的代码的修订版:

<?php

require_once "lib/couch.php";
require_once "lib/couchClient.php";
require_once "lib/couchDocument.php";

$couch_dsn = "http://localhost:5984/";
$couch_db = "couch";

$client = new couchClient($couch_dsn, $couch_db);
$all_singers = null;

try {
    $all_singers = $client->include_docs(true)->getAllDocs();
} catch (Exception $e) {
    //Handle the exception here.
}
if (!isset($all_singers) || !isset($all_singers->rows))
    echo "No singers found";
else
    foreach ($all_singers->rows as $row) {
        if (isset($row->error))
            continue; //Log the error or something like this
        if (isset($row->doc)) {
            $doc = $row->doc;
            echo $doc->singer;
            echo $doc->title;
            echo $doc->description;
        }
    }