2013-04-29 80 views

回答

2

_BaseQuery的源代码,它是Model的直接超类:

def run(self, **kwargs): 
    """Iterator for this query. 

    If you know the number of results you need, use run(limit=...) instead, 
    or use a GQL query with a LIMIT clause. It's more efficient. If you want 
    all results use run(batch_size=<large number>). 

def fetch(self, limit, offset=0, **kwargs): 
    """Return a list of items selected using SQL-like limit and offset. 

    Always use run(limit=...) instead of fetch() when iterating over a query. 

如果你有一个好主意你会得到多少实体,并且你想要预取引用属性或者同时放置多个实体,那么提取可能更有用。

如果您只是在没有获取或运行的情况下进行迭代,那么默认的批量大小可能会小于最佳值,因此您将有更多的往返行程增加延迟。

请注意,您应该考虑使用ndb而不是db它为您提供了一些其他选项,例如更简单的异步操作,另外还有query.map()和query.map_async()。