views:

56

answers:

2

Lets say I want to retrieve 4 objects. What would be faster:

1. for (int i = 0; i < 4; i++) {

persistenceManager.getObjectById(object1)

}

or

2. Query = (select * from objects where id == 'id1' || id == 'id2' || id == 'id3' || id == 'id4')

Thanks

A: 

I would guess the first form is way faster, but it's only a guess.

Riduidel
+2  A: 

By giving an example with multiple entities and an OR statement, you are complicating the issue a little bit. (The main issue is key based lookup vs Query based lookup.) The datastore doesn't support the union operation ("OR") natively. So behind the scenes your 2nd example will be doing 4 queries, one for each id.

Queries are usually slower than key lookups, so the 4 queries would normally be slower than 4 gets by ID. Exactly how much slower or faster will depend on your entities, but it is almost always a very significant difference.

However, in this case your queries are only using id fields. The Datanucleus JDO layer in app engine is smart enough to recognize a query that uses only ids, and will try to do a keys only batch operation. (see here) This should be the fastest option possible.

UPDATE: Apparently the difference between queries and keys has been reduced in the recent releases of the sdk, as discussed here.

Peter Recore