Andrew
Andrew

Reputation: 6900

In Firestore does an ordered query induce an additional read cost?

I have a collection foo of 1000 documents where each document has a field bar, a number. If I do a query db.collection('foo').orderBy("bar", "desc").limit(5) how many reads will I be charged for?

Upvotes: 1

Views: 508

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 600130

There is no additional charge for using an orderBy statement in your query, as the ordering of the results can be determined from the indexes. So Firestore doesn't have to read all documents in the collection to determine the value of bar in your scenario, it just needs to read the index for bar.

In fact, Firestore never needs to read all documents to satisfy a query. It's whole performance guarantee is based on being able to execute any query it allows by reading slices from indexes only.


You'll be charged for at least 1 and at most 5 reads by that query. The exact charge depends on how you implement the read operation, and the state of the local cache.

Things to keep in mind:

  • If the client is not connected to the server, you will not be charged for any document reads.
  • If a document is already in the cache of the client making the call, you may not be charged for reading it from the server.
  • If there are no documents returned from the server for the query, you will still be charged 1 document read for the query itself. If any documents are returned from the server, there is no additional charge for the query.

Upvotes: 3

Dharmaraj
Dharmaraj

Reputation: 50930

5 reads. You get charged for what you request which in this case is 5 due to limit(5) irrespective of amount of documents you have.

Read more at: Understand Cloud Firestore Billing

Upvotes: 0

Related Questions