App Engine: What is the fastest way to check if my datastore query returns any result?
I like to check if there is any result for my datastore query in the Google App Engine Datastore. This is my query:
users = User.all()
users.filter("hash =", current_user_hash)
What is the fastest and most elegant way to check if my query returns any result?
PS: I know a way to do so, but I'm very unsure if i开发者_如何学Pythont is very efficient...
If you also need to fetch the results, the most efficient way is to fetch the results with .fetch(), and then check if the list is nonempty. If you don't actually need the results, call .count(1).
What you shouldn't do is call .count(1) if you also need the results - this'll require executing the query twice.
users.count(1)
is probably the fastest way to do this; users.fetch(1)
would be the other way to accomplish this, but requires fetching an entire entity; depending on the size of that entity this could be fairly slow.
精彩评论