1

我有一个要加载到前端的实体列表。如果我的 NDB 中还没有这些实体,我会从另一个数据源加载它们。如果我的 NDB 中确实有它们,我显然会从那里加载它们。

我不想单独查询每个键来测试它是否存在,而是查询整个列表(出于效率原因)并找出 NDB 中存在哪些 ID,哪些不存在。

它可以返回一个布尔值列表,但欢迎任何其他实用的解决方案。

已经感谢您的帮助!

4

1 回答 1

0

如何对ndb.get_multi()您的列表进行 a,然后将结果与您的原始列表进行比较以找到您需要从其他数据源检索的内容?大概是这样的……

list_of_ids = [1,2,3 ... ]

# You have to use the keys to query using get_multi() (this is assuming that
# your list of ids are also the key ids in NDB)
keys_list = [ndb.key('DB_Kind', x) for x in list_of_ids]
results = ndb.get_multi(keys_list)
results = [x for x in results if x is not None] # Get rid of any Nones
result_keys = [x.key.id() for x in results]
diff = list(set(list_of_ids) - set(result_keys)) # Get the difference in the lists
# Diff should now have a list of ids that weren't in NDB, and results should have
# a list of the entities that were in NDB.

我不能保证它的性能,但它应该比一次查询每个实体更有效。根据我的经验,使用ndb.get_multi()是一个巨大的性能提升器,因为它减少了大量的 RPC。您可能会调整我在上面发布的代码,但也许它至少会为您指明正确的方向。

于 2013-06-26T17:18:40.500 回答