3

I have about 50k entities stored in appengine. I am able to look up an individual record via the GQL admin interface with a query like:

SELECT * FROM Pet where __key__ = KEY( 'Pet','Fido')

But I'm having trouble figuring out how to do a batch version of this via JDO. Right now I have this:

    PersistenceManager pm = ...;
    for(Pet pet : pets) {
        for(String k : getAllAliases(pet)) {
            keys.add(KeyFactory.createKeyString(Pet.class.getSimpleName(), k));
        }
    }
    Query q = pm.newQuery("select from " + Pet.class.getName() + " where id == :keys");
    List<Pet> petlist = (List<Pet>) q.execute(keys);

But though 'Fido' works in the GQL case, it returns nothing when I use that Java + JDO code. What am I doing wrong?

skaffman
  • 398,947
  • 96
  • 818
  • 769
TheDon
  • 388
  • 1
  • 2
  • 11

3 Answers3

2

Don't use queries to fetch entities by key - they're inefficient, and require a query for each object retrieved! Instead, get the objects by key. Bulk fetches by key don't appear to be supported by JDO, but fetching them by key one at a time is still going to be a lot more efficient than doing queries for them.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • I find it hard to believe that fetching entities - synchronously - individually by key is going to be faster than doing a JDO query. I will try the experiment at some point. – TheDon Mar 26 '10 at 01:25
  • 2
    'IN' is not natively supported by App Engine - the SDK emulates it by executing multiple, sequential, queries. Each query has to do an index scan followed by a lookup by key. In comparison, a lookup by key only has to do a small part of that, so it's substantially faster. – Nick Johnson Mar 26 '10 at 09:49
1

Likely doesn't work in the JDOQL case since it isn't valid JDOQL :-P

Collection keys = ... (create your keys collection)
Query q = pm.newQuery("SELECT FROM " + Pet.class.getName() + " WHERE :keys.contains(id)");

i.e the filter uses Java syntax

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
  • Good to know. I was expecting to get some sort of hard-to-ignore error notification if the query wasn't valid, so I'm disappointed there. – TheDon Mar 18 '10 at 12:20
0

http://gae-java-persistence.blogspot.com/2009/10/executing-batch-gets.html

I didn't try the JDO version, but the JPA version does work!

Kenyth
  • 118
  • 1
  • 8