Currently, I am working on app with rank list functionalities, so I want to confirm some concern before starting it.
I want to know how many reads count for each query, what are the worst case scenarios.
Case 1: To get global top 10 users
await _firestore.collection('ranklist')
.orderBy('maxStreak', descending: true)
.limit(10)
.get();
Case 2 :To get top 10 users based on level
await _firestore.collection('ranklist')
.where('score', isGreaterThanOrEqualTo: minScore)
.where('score', isLessThan: maxScore)
.orderBy('score', descending: true)
.limit(10)
.get();
Case 3: To get global user rank
await _firestore.collection('ranklist')
.where('score', isGreaterThan: userScore)
.count()
.get();
why I am asking is this only because of reading various community post and got confused
as per Gemini answer
Gemini answer:
- Imagine there are 100 documents in the ranklist collection. 50 of those documents have a score within the minScore to maxScore range. Firestore has to read those 50 documents to determine which 10 have the highest scores. Even though you only get 10 documents back, you will be billed for 50 document reads.
I want to clarify these scenario.
Currently, I am working on app with rank list functionalities, so I want to confirm some concern before starting it.
I want to know how many reads count for each query, what are the worst case scenarios.
Case 1: To get global top 10 users
await _firestore.collection('ranklist')
.orderBy('maxStreak', descending: true)
.limit(10)
.get();
Case 2 :To get top 10 users based on level
await _firestore.collection('ranklist')
.where('score', isGreaterThanOrEqualTo: minScore)
.where('score', isLessThan: maxScore)
.orderBy('score', descending: true)
.limit(10)
.get();
Case 3: To get global user rank
await _firestore.collection('ranklist')
.where('score', isGreaterThan: userScore)
.count()
.get();
why I am asking is this only because of reading various community post and got confused
as per Gemini answer
Gemini answer:
- Imagine there are 100 documents in the ranklist collection. 50 of those documents have a score within the minScore to maxScore range. Firestore has to read those 50 documents to determine which 10 have the highest scores. Even though you only get 10 documents back, you will be billed for 50 document reads.
I want to clarify these scenario.
Share Improve this question edited Mar 20 at 5:52 Munsif Ali 6,5205 gold badges25 silver badges54 bronze badges asked Mar 19 at 19:03 dev.skasdev.skas 6754 silver badges19 bronze badges 1- 1 That Gemini answer is nonsense, likely because it's trained on a large corpus of outdated materials (your #2 and #3 queries have only been possible since 1-2 years ago). My answer based on the documentation and working on this product for a decade is below. I recommend sticking closer to the documentation, rather than letting an LLM hallucinate to its vibe's content. If you have communicate content that is confusing, please always include the links to that. – Frank van Puffelen Commented Mar 19 at 20:38
1 Answer
Reset to default 1Based on the documentation:
This will read at most 10 documents, so will cause between 1 and 10 charged document reads. Even if there are 0 results, the minimum charge for any query is 1 document read.
This is similar, but not exactly the same. There may be documents in the index that Firestore uses that are not part of the results, but for which it scans the index entries. For those index entries you pay 1 document read for each of up-to-1000 index entries skipped.
If you want to know the exact number of documents and index entries, run a query explain.
The pricing for this use-case is explained in the Firebase documentation under Query with range and inequality filters on multiple fields > Pricing
I wrote a blog post a while ago that uses this type of query, and did a comparison there of field ordering and its impact on these reads: How to perform geoqueries on Firestore (somewhat) efficiently.
This is simpler again: it charges 1 document read for each of up-to-1000 documents counted.