Better to use Key or User in referencing Users?

21 views
Skip to first unread message

Carter

unread,
Jun 16, 2011, 4:16:17 PM6/16/11
to Google App Engine
Is a query on type User more expensive than querying on type Key?

In storing references to users of your application across multiple
Kinds, which is better the better approach assuming I don't need
locking for transactions?

a) store the "User" type (the actual User object) or
b) store a "Key" type (that was used as the Primary Key for a Kind
that stores user information)

For example, if the Kind AppUser is defined as...

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class AppUser {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
private User user;
...
}

and we're storing an image uploaded by that AppUser, is it better to
use:

Option (a)

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class UploadedImage {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
private User user;
...
}

or, Option (b)

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class UploadedImage {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
private Key appUserKey;
...
}

When retrieving 100s of images owned by the currentUser(), is option
(b) preferred even though it would require the initial lookup of the
AppUser before querying UploadedImage using appUserKey?

Nick Johnson (Google)

unread,
Jun 17, 2011, 2:14:37 AM6/17/11
to google-appengine
Hi Carter,

Querying on one property or another has effectively no difference in performance. There are other considerations, however:
- Using a key will allow you to potentially add support for users authenticated in other ways (Eg, an external authentication framework) without changing the rest of your models.
- If you set the key_name of the userinfo model to the user_id of the user, you don't need to look the record up - you can create its key based on the logged in user with no datastore operations
- Querying on user objects can be tricky, particularly if the user has changed their email address. It's best to avoid doing this if you can.

-Nick Johnson


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




--
Nick Johnson, Developer Programs Engineer, App Engine


Reply all
Reply to author
Forward
0 new messages