Home > Software design >  will document ids like {year}-Q{n} cause performance problems?
will document ids like {year}-Q{n} cause performance problems?

Time:06-11

The Firestore docs "Best Practices" section says:

Document IDs

Do not use monotonically increasing document IDs such as:

Customer1, Customer2, Customer3, ...

Product 1, Product 2, Product 3, ...

I'd like to store historical data in a collection with one document per quarter, and use document ids like 2022-Q1, 2022-Q2, etc.

These aren't strictly monotonically increasing because the year number changes after 4 quarters. But they're still lexicographically close. Will this cause performance problems?

My data goes back about 50 years so there will be around 200 documents.

CodePudding user response:

200 documents with any IDs are not going to cause any performance problems. That is a very small amount of data from Firestore's point of view.

The recommendation that you're citing from the docs has to do with situations where documents are added very rapidly to a collection. If you read on, it says "Such sequential IDs can lead to hotspots that impact latency". If you read about hotspotting, that is not your situation, so you don't have to worry about it.

Aside, it's generally not a good idea to put data in your document IDs. That can cause problems for you later. Data belongs in document fields. You're generally better off using Firestore's randomly generated IDs unless you already have a source of unique IDs.

  • Related