删除数据存储中数千个实体的最有效方法

Most efficient way to delete thousands of entities in datastore

这就是我删除数据存储区中数千个实体的方式:首先,获取第一个实体。如果存在第一个实体,则获取 500 个要删除的实体。其次,一次又一次地推迟 deletealltarget 直到第一个实体不存在。

def deletealltarget(twaccount_db_key):
  target_db = model.Target.query().filter(ndb.GenericProperty('twaccount_key') == twaccount_db_key).get()
  if target_db:
    target_dbs = model.Target.query().filter(ndb.GenericProperty('twaccount_key') == twaccount_db_key).fetch(500,keys_only=True)
    ndb.delete_multi(target_dbs)
    deferred.defer(deletealltarget,twaccount_db_key)

有没有更好的办法?

您可以使用 delete_multi_async asynchronously, instead of using defer, but besides that, you are doing good with this way. For example, you are using other advices already told, like using keys_only

Google推荐使用this Dataflow template进行批量删除,不知道适不适合你的场景