1
votes

In Firestore documentation said:

There are no additional costs for using cursors, page tokens, and limits. In fact, these features can help you save money by reading only the documents that you actually need.

But the following queries produce 80 reads:

    val query = collection.orderBy("name")

    query.limit(30).get(Source.SERVER).continueWithTask {
        query.limit(20).get(Source.SERVER)
    }.continueWith {
        val doc = it.result!!.documents[5]

        query.startAfter(doc).limit(30).get(Source.SERVER)
    }

I expect only 35 reads as second query start from the same point as first and reads only 20 documents which already read by first query. And third query start from the 5th document so it reads 5 new documents. Also if I run any of this queries multiple times reads count will increase as if I run it only one time. For example if i run first query 5 times reads count will increase only by 30 not 150 as expected by me.

This queries differ only by limits which as said in documentation do not have additional costs for using. Why they produse additional reads(on usage tab at firebase console)? Maybe this reads won't be charged?

Thanks!

1

1 Answers

3
votes

If I understand what you're describing correctly, this is intended behavior. I count 80 reads in your code, so I'd expect you to be billed for 80 reads. Note that .get triggers the reads immediately, regardless of whether you just retrieved the same documents a moment ago / a line above.

val query = collection.orderBy("name")

query.limit(30).get(Source.SERVER).continueWithTask {    <----- 30 reads triggered here
    query.limit(20).get(Source.SERVER)    <----- 20 reads triggered here
}.continueWith {
    val doc = it.result!!.documents[5]

    query.startAfter(doc).limit(30).get(Source.SERVER)    <----- 30 reads triggered here
}

and of course 30+20+30 = 80.

And yes, limit is saving you money here, because without limit, then each of those .get()s would be reading ALL of the documents in the collection.