niteshb
niteshb

Reputation: 2829

Implementing join query using GQL in google-app-engine

I have an entity kind like:

prop1 prop2 docid other-properties

str1 p1 1001 .........

str2 p1 1002 .........

str2 p2 1001 .........

str1 p2 1003 .........

I want to have all those docid's that have "prop1 as str1 and prop2 as p1" and "prop1 as str2 and prop2 as p2" in common and want to use their other-properties. Like here the answer would be docid 1001. Can anyone suggest me a method to do so? I am even ready to change my database structure if required but I want these thing to be done by a single query only...

Ok its fine about other properties I may not need to use them, I just want all docid's that are common.

Right now I am using something like this:

for b in db.GqlQuery("SELECT * FROM b"):
for a in db.GqlQuery("SELECT * FROM a WHERE y=:1", b.y):
print a.x

But this is taking of lot of time since I have a huge number of entries.

Upvotes: 1

Views: 2272

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101149

The easiest way to model this would be to have the docs themselves have a list of strings:

class Doc(db.Model):
  strings = db.StringListProperty()

Then, you can do a query for multiple strings in that list like so:

q = Doc.all().filter('strings =', 'str1').filter('strings =', 'str2').get()

This will use the merge-join strategy by default, and thus won't require you to build any custom indexes.

Upvotes: 1

Related Questions