Firestore write batch with 400 docs takes multiple minutes to complete
Firestore write batch with 400 docs takes multiple minutes to complete
I am using the Firestore database on Android and found that when I do a batched write over 300-400 items (but below the limit of 500), it can take very long to complete and the success listener to kick in (i.e. 3-5 minutes). Sometimes the listener doesn't react at all.
In the latter case (when the listener doesn't react), neither the OnSuccess or OnFailure listener is being called. I'm checking with logs.
So it feels like the commit is stuck somewhere?
The document writes I make are with POJOs. Each POJO/Document has approximately 60 indices. Could the amount of indices be the problem?
The documents are nested in a subcollection.
Here is my code:
for (int i = 0; i < listOfPojos.size(); i++)
Pojo pojo= listOfPojos.get(i);
//set/create the document in subcollection. Use its date as name.
DocumentReference document = docRef.collection(pojo.getDevice_id()).document(pojo.getDate().toString());
batch.set(document, pojo);
batch.commit().addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
//do stuff
//log here doesn't kick in
).addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
//do stuff
//log here doesn't kick in
);
I have searched the forums quite a bit but haven't found a question with this problem. Hope that someone could point in the direction of what I'm doing wrong?
1 Answer
1
Sorry to post an answer myself, but I just figured something out.
Instead of having one batch of 400 writes, it's much faster to have 20 batches of 20 documents.
I'm not sure if that is only the case for me, but having smaller write batches seems to be almost exponentially faster than one large batch, even if that batch is clearly under the limit.
So my solution is just to have a hard cap of 20 documents per batch write, and now it's working much faster (I still need 20 seconds for 400 documents writes though).
I'll leave this question up, it might help someone. Or maybe someone who knows more about Firestore can chime in and confirm/refute my observation.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.