【问题标题】:Looping through Datastore query results takes too long. Is there a way to speed this up?循环遍历 Datastore 查询结果需要很长时间。有没有办法加快这个速度?
【发布时间】:2016-07-15 15:20:36
【问题描述】:

我正在查询如下所示的数据存储:

Query<Entity> query = Query.gqlQueryBuilder(Query.ResultType.ENTITY,
                                    "SELECT * FROM " + kind
                                            + " WHERE Location = place").build();
results = datastore.run(query);

结果存储在QueryResults&lt;Entity&gt; results;

然后我遍历结果并提取我需要的数据。

这种类型有大约 10000 个实体,我正在提取它们。

while (results.hasNext()) {

   Entity result = results.next(); 
   ....
}

这需要 10 秒左右的时间。有没有办法减少这个时间?我知道循环遍历结果会导致速度变慢。

【问题讨论】:

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


    【解决方案1】:

    (a) 确保在运行查询时将批量大小设置为 500。默认为 10。

    (b) 在循环内优化您自己的代码。例如:

    Entity result;
    
    while (results.hasNext()) {
       result = results.next(); 
       ....
    }
    

    【讨论】:

    • 感谢您的回复!我在哪里设置批量大小?
    • 在低级 Datastore API 中,您可以在准备查询时传递 FetchOptions 对象。我在 Gcloud API 中没有看到类似的选项 - 问 Gcloud 团队可能是个好问题。
    • 检查是否可以切换到 Datastore API (cloud.google.com/appengine/docs/java/datastore/api-overview) - 它针对 App Engine 进行了优化,我发现它非常快。
    猜你喜欢
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多