On Sun, Mar 14, 2010 at 9:16 PM, Max <thebb...@gmail.com> wrote:
>
> If I would like to find all users that have java level > 5 and c++
> level > 2, then how to write a query in your suggested data model?

Every time this kind of question comes up I usually think to myself
"how would an RDBMS do this?" and then roughly approximate the same
solution.  RDBMSes aren't magic... they have indexes and a query
planner that tries to figure out the best approach for utilizing those
indexes.  On appengine, *you're* the query planner.  For a hint, try
running ANALYZE commands on normal databases, then code up more or
less what it says.

If you use an @Embedded (or in Twig, @Embed) collection:

class UserSkill {
    String skill;
    int ability;
}
class Person {
    @Id Long id;
    @Embedded UserSkill[] skills;
}

 * Create a multi-field index on Person {skills.skill, skills.ability}
 * Filter by the first condition:
ofy.query(Person.class).filter("skills.skill",
"java").filter("skills.ability >", 5);
 * Iterate through the results, skipping anyone who has a c++ skill
less than or equal to 2.

If you are using an UserSkill entity:

class UserSkill {
    @Id Long id;
    @Parent Key<Person> person;
    String skill;
    int ability;
}
class Person {
    @Id Long id;
}

 * Create a multi-field index on UserSkill {skill, ability}
 * Filter by the first condition:
ofy.query(UserSkill.class).filter("skill", "java").filter("ability >",
5);
 * Get all the parent userids of the results, putting them in a hashset.
 * Create a new query, filtering by the second condition:
ofy.query(UserSkill.class).filter("skill", "c++").filter("ability >",
2);
 * Skip any results from the new query that aren't in the set produced
by the first query.

If this seems computationally expensive, realize that this is little
different than what an RDBMS would do.  Maybe - just maybe - an RDBMS
could efficiently guess whether it should filter by "java" or by "c++"
first based on statistical analysis of the data.  In practice, query
planners are usually crap and they only work because a coin toss isn't
all that bad of a solution.  You're more likely to know the statistics
yourself.

I don't myself see how Slatkin's Relation Index Entity pattern can
help in this situation.

Jeff

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to