Android Firebase实时数据库快速删除大查询结果

Android Firebase实时数据库快速删除大查询结果,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我的实时数据库已经存储了超过1GB的数据,所以为了每月对其进行一次修剪、保存存储和优化日常使用,我会运行一个例程来删除旧的和不相关的数据 事情是这样的: App.getDatabaseInstance().getReference("store/orders/historic/").orderByChild("creationTs").limitToLast(500).endAt(System.currentTimeMillis() - (90L * ON

我的实时数据库已经存储了超过1GB的数据,所以为了每月对其进行一次修剪、保存存储和优化日常使用,我会运行一个例程来删除旧的和不相关的数据

事情是这样的:

App.getDatabaseInstance().getReference("store/orders/historic/").orderByChild("creationTs").limitToLast(500).endAt(System.currentTimeMillis() - (90L * ONE_DAY_MILLIS)).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.hasChildren()) {
                    for (DataSnapshot historicDs : dataSnapshot.getChildren()) {
                          historicDs.getRef().removeValue();
                    }      
                    cleanHistoricBranch();
                } else
                    System.out.println("FINISHED!!!");
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
查询在数据库中运行数千个节点(不是数百万个),但需要数小时才能完成。我想问题是数据必须一个接一个地下载和删除

我尝试了不同的方法,但效果不好

App.getDatabaseInstance().getReference("store/orders/historic/").orderByChild("creationTs").limitToLast(500).endAt(System.currentTimeMillis() - (90L * ONE_DAY_MILLIS)).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.hasChildren()) {
                        dataSnapshot.getRef().removeValue(); //deletes the whole branch, even the nodes that doesnt match the query.     
                        cleanHistoricBranch();
                    } else
                        System.out.println("FINISHED!!!");
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
    
                }
            });

那么,有没有更好的方法来修剪数据库层次结构中的大量节点?每个节点只有很少的数据,但我有大约20到5万个候选节点要删除。

如果时间主要花在读取数据上,常用的方法是:

  • 更频繁地运行该进程,以便每次执行的数据更少
  • 设置数据库的集成备份,并使用该备份确定要脱机删除的密钥。然后将写操作发送到联机数据库