4
votes

Do I pay a penalty on query performance if I choose to query repeated property? For example:

class User(ndb.Model):
    user_name = ndb.StringProperty()
    login_providers = ndb.KeyProperty(repeated=true)

fbkey = ndb.Key("ProviderId", 1, "ProviderName", "FB")
for entry in User.query(User.login_providers == fbkey):
    # Do something with entry.key

vs

class User(ndb.Model)
    user_name = ndb.StringProperty()

class UserProvider(ndb.Model):
    user_key = ndb.KeyProperty(kind=User)
    login_provider = ndb.KeyProperty()

for entry in UserProvider.query(
    UserProvider.user_key == auserkey,
    UserProvider.login_provider == fbkey
):
    # Do something with entry.user_key

Based on the documentation from GAE, it seems that Datastore take care of indexing and the first less verbose option would be using the index. However, I failed to find any documentation to confirm this.

Edit

The sole purpose of UserProvider in the second example is to create a one-to-many relationship between a user and it's login_provider. I wanted to understand if it worth the trouble of creating a second entity instead of querying on repeated property. Also, assume that all I need is the key from the User.

1
It's unclear whether you also have a UserProvider class in the first version, or whether you just create Keys pointing nowhere (which is permissible, BTW :-). The two queries are not equivalent since one returns User entities and the other UserProvider entities. - Guido van Rossum
@GuidovanRossum I updated the question and hopefully is clearer now. It did take me a while to understand what is a ndb.Key, given that you can create a Key that points to nothing. - marcoseu

1 Answers

6
votes

No. But you'll raise your write costs because each entry needs to be indexed, and write costs are based on the number of indexes updated.