Google数据存储从select中获取不一致的结果

时间:2016-12-14 11:26:00

标签: php google-app-engine google-cloud-storage google-cloud-datastore

我非常需要GDS和PHP-GDS库的帮助。

我正在使用精彩的php-gds库从外部谷歌appengine进行提取。到目前为止,图书馆工作正常。

我的问题是我从GDS获取数据会返回不一致的结果,我不知道问题可能是什么。

请参阅以下代码:

    <?php
    //...
    //...
    $offset = 0;
    do{
       $query = "SELECT * FROM `KIND` order by _URI ASC limit 300 offset ".$offset;
       $tableList=[];
       $tableList = $this->obj_store->fetchAll($query);
       $offset += count($tableList);
       $allTables[] = $tableList;
       $totalRecords = $offset;
    }while(!empty($tableList));

    echo $totalRecords; // i expect total records to be equal to the number of entities in the KIND.
                        // but the results are inconsistent. In some cases, it is correct 
                        // but in most cases it is far less than the total records.
// I could have a KIND with 750 entities and only 721 will be returned in total.
// I could have a KIND with 900 entities and all entities will be returned.
// I could have a KIND with 4000 entities and only 1200 will be returned.
    ?>

请帮忙。此外,当我在云控制台中运行完全相同的查询时,我得到正确的实体计数。 (希望这有助于某人)

  

更新

我最终使用了游标。下面的新代码:

<?php
  $query = "SELECT * FROM `KIND`";
  $tableList=[];
  $queryInit = $this->obj_store->query($query);
  do{
      $page = $this->obj_store->fetchPage(300);// fetch page.
      $tableList = am($tableList,$page); //merge with existing records.
      $this->obj_store->setCursor($this->obj_store->getCursor());//set next cursor to previous last cursor
  }while(!empty($page)); //as long as page result is not empty.
?>

1 个答案:

答案 0 :(得分:1)

尝试使用光标而不是偏移量。请在此处查看游标(包括PHP中的示例)的讨论: https://cloud.google.com/datastore/docs/concepts/queries#datastore-cursor-paging-php