Question

https://developers.google.com/appengine/docs/python/ndb/

https://developers.google.com/appengine/docs/python/ndb/queries

Is there a difference between ndb.gql and ndb.query ? (beside the syntax)

for example

cursor = ndb.gql("select * from Account")

vs

cursor = Account.query()

Because I like the ndb.query because I think it's more readable

example: acc=Account.query(Account.username==form.username.data,Account.password==form.password.data)

I could not find any information if these two methods are different/equal under the hood.

=> Maybe there is a perfomance tradeoff?

If you are accustomed to SQL, beware of false assumptions when using GQL. GQL is translated to NDB's native query API. This is different from a typical Object-Relational mapper (like SQLAlchemy or Django's database support), where the API calls are translated into SQL before they are transmitted to the database server. GQL does not support Datastore modifications (inserts, deletes or updates); it only supports queries.

I guess that ndb.query should be "faster" because it does not need to be translated right ?

Was it helpful?

Solution

These are simply two different ways to do the same thing. Choose whichever is easier for you in a particular situation. You're right that gql() is theoretically slower because it builds up a Query object after parsing, but I don't think you'll be able to notice any difference in speed (assuming you actually run the query :-).

Note that both return an object of the same type:

>>> q1 = Employee.query()
>>> q1
Query(kind='Employee')
>>> q2 = gql('SELECT * FROM Employee')
>>> q2
Query(kind='Employee', default_options=QueryOptions(offset=0))
>>> 

(The default_options field is only relevant when you use the OFFSET or LIMIT syntax in GQL.)

So what you can do with the result is exactly the same. You can even apply additional filters and orders to q2 using the .filter() and .order() methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top