A Ba
A Ba

Reputation: 275

Looping through Datastore query results takes too long. Is there a way to speed this up?

I am querying the datastore that looks something like this:

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

The results are being stored in a QueryResults<Entity> results;

Then I loop through the results and extract the data I need.

The kind has around 10000 Entities in it and I am extracting them all.

while (results.hasNext()) {

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

Is takes 10-ish seconds for this to happen. Is there a way to reduce this time? I know that looping through results is causing the slowdown.

Upvotes: 5

Views: 1004

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

(a) Make sure you set the batch size to 500 when you run your query. By default, it's 10.

(b) Optimize your own code inside the loop. For example:

Entity result;

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

Upvotes: 2

Related Questions