Home > Net >  Firestore: How is read calculated?
Firestore: How is read calculated?

Time:10-02

Data Structure:

Posts ( collection )

⤷ Post_1 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_2 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_3 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_4 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_5 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_100 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_1000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_5000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_10000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_15000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_20000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_25000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_50000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

Question 1: Let's suppose the user published a new post (The post is post_45813) and after that decided to update the post description, how is the read calculated in this situation, and how much will be?

Question 2: If I want to get documents from post_20000 to post_20100 regardless of category name, how is the read calculated in this situation, and how much will be?

Question 3: If I want to get all documents inside category 1 and let's suppose there are 13833 posts inside category 1, how is read calculated in this situation, and how much will be?

CodePudding user response:

The number of reads that you have to pay is always equal to the number of documents that are returned by your query.

Question 1: Let's suppose the user published a new post ( The post is post_45813 ) and after that decided to update the post description, How is the read calculated in this situation, and how much will be?

For publishing a new post, the cost is one write operation, and for updating the post, there is another write operation involved. A total of 2 writes.

Question 2: If I want to get documents from post_20000 to post_20100 regardless of category name, How is the read calculated in this situation, and how much will be?

If the number of documents between post_20000 and post_20100 is equal to 100, then you have to pay exactly 100 reads.

Question 3: If I want to get all documents inside category 1 and let's suppose there are 13833 posts inside category 1, How is read calculated in this situation, and how much will be?

If your collection contains 13833 posts and you want to get them all at once, you have to pay 13833 reads. But please note that no one in this world will ever be interested in reading so many posts. In that case, you should always consider getting the data in smaller chunks. This technique is called pagination.

You can also consider filtering the results according to some fields.

Please also note, that if your query yields no result, according to the official documentation regarding Firestore pricing, it said that:

Minimum charge for queries

There is a minimum charge of one document read for each query that you perform, even if the query returns no results.

So if, for example, you try to filter 13833 posts and you get no results, you're still charged with 1 read.

Here is also an article that you might be interested in:

  • Related