I am using Firestore for my application and every day thousands of documents are out of date and will not be used. So my question is which one is better, keeping them so they don’t have an effect on query performance in the future, or deleting them by clients when their job is done.
Example of document:
Order for food
userID: "UOMXO1C17m3df2WO58Jf"
orderTime: July 13, 2022 at 11:22:21 PM UTC+3
restaurantId: "7m3df2WO58Jjkadfladd"
2
Answers
Keeping the data might affect your query performance in the near future. However, if you keep the data it can help you to gather information of the document easily. You may also check best practices for further help that you might need.
But if the document is not necessary to store the data, I would suggest that deleting them by clients when their job is done is fine so whenever you try to query time to time it will not affect/slows down.
If you keep them, there will be no performance issue for future queries. Why? Because the query performance in Firestore depends on the number of documents you request and not on the number of documents you search. It doesn’t really matter if you search 10 documents in a collection of 100 documents or in a collection that contains 100 MIL documents, the response time will always be the same. So keeping those documents will not affect the performance in any way.
However, getting rid of outdated documents that will never be used anymore, can be solved in two ways. The first one would be on the client, which means that you’ll have to create a delete operation each time a document becomes obsolete. Or, you can perform the exact same operation in a trusted environment you control. The latter is obviously more recommended. So you might consider using Cloud Functions for Firebase, to trigger a function that does exactly that.