Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 550
A Ba Avatar asked Oct 31 '22 00:10

A Ba


1 Answers

(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(); 
   ....
}
like image 155
Andrei Volgin Avatar answered Nov 15 '22 11:11

Andrei Volgin