Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine 检查NDB(python)中已经存在id列表中的哪些id_Google App Engine_Python 2.7_App Engine Ndb - Fatal编程技术网

Google app engine 检查NDB(python)中已经存在id列表中的哪些id

Google app engine 检查NDB(python)中已经存在id列表中的哪些id,google-app-engine,python-2.7,app-engine-ndb,Google App Engine,Python 2.7,App Engine Ndb,我有一个正在加载到前端的实体列表。如果我还没有在NDB中加载这些实体,我将从另一个数据源加载它们。如果我的NDB中有它们,我显然会从那里加载它们 为了提高效率,我想查询整个列表,找出NDB中存在哪些ID,哪些ID不存在,而不是单独查询每个键来测试它是否存在 它可以返回一个布尔值列表,但任何其他实用的解决方案都是受欢迎的 谢谢你的帮助 对列表执行ndb.get_multi,然后将结果与原始列表进行比较,以找到需要从其他数据源检索的内容,怎么样?像这样的事情也许 list_of_ids = [1,2

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

为了提高效率,我想查询整个列表,找出NDB中存在哪些ID,哪些ID不存在,而不是单独查询每个键来测试它是否存在

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

谢谢你的帮助

对列表执行ndb.get_multi,然后将结果与原始列表进行比较,以找到需要从其他数据源检索的内容,怎么样?像这样的事情也许

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。您可能会对我上面发布的代码进行调整,但也许它至少会为您指明正确的方向