Firebase Realtime Database Pricing with Query

流过昼夜 提交于 2021-02-18 08:07:50

问题


I have a question about the firebase database pricing. I have about 400,000 rows in the leaderboard of my database, but in my app I just want to load the last 500 rows, so my question will I get charged for the 500 rows loaded when I run the query or will I get charged for all 400,000 rows.

Realtime database charges 5$ per gb stored and 1$ per gb downloaded. I did the calculation with Firestore and found that Realtime Database would be way cheaper if i get charged for the 500 rows and not the 400,000 rows.

I searched all documentation and have not found anything about queries: https://firebase.google.com/pricing https://firebase.google.com/docs/database/usage/billing

Can someone tell me if I get charged for just the 500 rows in my collection or for all the data in the collection and if there is a way to only get charged for the 500 rows maybe with security rules?

Here is my query code:

let queryRef = ref.child("Leaderboard").queryOrdered(byChild: "totalStars").queryLimited(toLast: 500)

How the database looks like. (It will have about 500,000 childs same as these and be loaded 200,000 per day, But I just want to be priced on the top 500 that I load and not the whole 500,000 each time a user loads the leaderboard is it possible?)


回答1:


You will only be charged for the number of Firestore Documents corresponding to the result of your query (not to the number of docs in the collection).

So in your case a maximum of 500 reads, since you would limit the Query to 500 documents.


On the other hand, note that the Realtime Database queries are not shallow (while the Firestore ones are) and therefore if you query for a JSON node you'll get the entire tree under this node.




回答2:


Renaud's answer is the correct one but let me add some additional information and restate that:

With the Firebase Real Time Database for downloads you are charged for what is downloaded and not how many nodes you are querying.

So the key is to reduce the amount of data you're downloading. Your nodes are already pretty shallow however, there's a huge savings to be made because in your current structure, the node key (the users uid) is duplicated within the node as a child node, and that's not needed.

You can always get the node key with snapshot.key and remove that child node. So it would look like

uid
   fullName: "Logan Paul"
   stars: 40

Also, I think your calculations are off a bit. It looks like each node would be about 100 bytes of data, and Firebase strings are UTF-8 Encoded so if you download 500 nodes per user per day and you have 200,000 users, that about 38Gb per day (as binary).

Roughly 400 bytes * 500 nodes * 200,000 users * 0.000000000931322574615479 bytes per Gb = 38Gb

so about $38 a day if I did my math correctly.



来源:https://stackoverflow.com/questions/60693333/firebase-realtime-database-pricing-with-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!