Firestore: Understanding the read count and pricing applied when searching - firebase

I am developing a flutter app and have the following firestore code. There I am searching for the rooms where it contains a given memberid.
return _firestore
.collection('rooms')
.where('memberIds', arrayContains: currentUserUid)
.snapshots()
.asBroadcastStream();
Please consider the following.
Assume I have 100 rooms.
In 20 rooms you can find my memberId
Now, if I run the above code once, how many reads will it create and bill me for?
Is it for 1 read, 20 reads or for 100 reads?

Firestore only allows you to execute queries where it can find the documents to return based on the indexes that it keeps. So it doesn't ever have to read each document to determine whether it matches the conditions (something known as a table scan).
So if you return 20 rooms, you will be charged for 20 document reads, no matter how many documents there are in your rooms collection.

Related

Does get() call in rule evaluation charge an extra read for every single document pulled down in a query through Firestore?

I went through the documentation for Firestore pricing but was unable to find a clear answer to my question. Let's say I have a collection called posts and users pull down every document in the collection 'posts'. Now if the rule evaluation is such that
match /posts/{post}
allow read: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.hasSubscribed == true;
and the user pulls down 50 posts, will I be charged 50 additional reads (corresponding to the rule evaluation for every single document being requested in the query) because of the get call, making the total reads equal to 100 or will I be charged only 1 read for the entire query thus making the total number of reads equal to 51?
If you scroll down to Cloud Firestore Security Rules section, it says:
You are only charged one read per dependent document even if your rules refer to that document more than once.
In your security you are accessing only 1 document get(/databases/$(database)/documents/users/$(request.auth.uid));. Even if you are fetching 50 documents, you are only charged once for that single document (for that request) i.e. costing you 51 reads.

How firestore count read on flutter

I have several question about how does firebase count read in my flutter app,
i have an app which use multiple firestore snapshot as stream like this:
_firestore
.collection(salesOrderPath)
.where("unit", isEqualTo: 1)
.snapshots()
_firestore
.collection(salesOrderPath)
.where("status", isEqualTo: 2)
.snapshots()
This two stream contain some same document, does that same document counted twice or once?
2.If i have multiple where filter on my firestore snapshot like this:
_firestore
.collection(salesOrderPath)
.where("unit", isEqualTo: 1)//10 Document
.where("status", isEqualTo: 2)//4 Document
.orderBy('creationDate',descending: true)
.snapshots()
Would i be charged by 10 read or just 4?
Maybe not related, but i saw something called Network egress as limit in the firebase pricing, what is this network egress meaning actually is it for storage or for firestore?
How long does cache from firestore in our app last before we need to reread it again from firestore?
I am new at this and dont quite understand a lot of thing, thankyou so much for answering
I think you should definitely watch video on Firebase Guide regarding pricing (actually I suggest to watch all of them).
I don't think you will find better description of pricing.
I guess question#1 is 2 reads as there are 2 queries. Question#2 anwser is 4 reads. Question#3 - if you watch the video you will know that this is rather negligible cost.
Now point 4#. Unfortunately I don't know, but I found something that might be interesting for you here.
Firestore bills you for your result set. So if your whole query returns 10 documents from a million documents. You only get billed for those 10 documents.
Network Egress is:
Network traffic that begins inside of a network and
proceeds through its routers to a destination somewhere outside of the
network
So if you ask for one document, you get billed for that 1 read plus the 'work done' for firestore to get that document for you.
The cache does not have an explicit expiry date. It will expire if it needs to make more space for new data or there was a deletion server-side and your cache now needs to resync. The size of the cache is 40MB.
Like others have mentioned I highly recommend their series on youtube

Am I using Firestore Indexes correctly?

I've been recently making use of Flutter and Firebase to build some Networking Apps similar to the likes of LinkedIn. One of the App's features is its ability to match your profile with other users registered in the database based on factors such as location and the type of work you do. To do that I've been using Firestore snapshots with some conditions applied. Two of these conditions ask Firebase to sort the users by 'Last Active' and to only read documents of users that have signed in within the past month. In order to declutter the results and decrease the number of documents reads that are requested at a time. This is one example of a firebase snapshot I'm using:
Firestore.instance
.collection('usersIsb')
.where('Set', isEqualTo: true)
.where('Account Type', isEqualTo: _tempType)
.where('Services List', arrayContainsAny: _tempServices)
.where('Location', isEqualTo: widget.userData.location)
.where('Last Active', isGreaterThanOrEqualTo: widget.dateLimit)
.orderBy('Last Active', descending: true)
.limit(10)
.snapshots()
Naturally, because of the complexity of the request, Firebase asks me to create an Index for it, I've done that, and everything seems to be working correctly without any noticeable slowdowns or issues. However, I have a couple of questions that I'd like answered:
Are these indexes real-time? As in updated every time a new user document is created?
How many Indexes can I have in one Firestore database? The indexing process sounds intensive so I'm assuming there are drawbacks.
Is this how it's supposed to be done in the first place? It feels like I'm doing something wrong...
Extra: These are the Indexes I currently have enabled in my Firestore database.
This is my first post on the platform so feel free to ask for more information if needed, any advice on how to achieve the objective more efficiently is also appreciated.
Are these indexes real-time? As in updated every time a new user document is created?
Yes
How many Indexes can I have in one Firestore database?
The documentation on index limits says:
Maximum number of composite indexes for a database: 200
Maximum number of single-field index exemptions for a database: 200
You should read through the entire documentation there to understand all the limits.
Is this how it's supposed to be done in the first place?
That's a matter of opinion. You can do whatever you want within the documented limits. If your indexes and queries work for your use cases, then that's fine.

Do queries count as readings in Firestore?

If I have 1000 documents and I run a query to find 3 documents that meet a certain condition, will that count as reading 1000 or 3 documents?
Document reads are the fundamental unit of billing in Firestore as they relate to queries. Individual queries are not a primary unit of billing, but each query will be billed.
You will be billed for all the documents matched by a query, as those documents will be read and sent to the client. If your query matches no documents, then there is no billing. If your collection has 1000 documents, but your query returns 3 documents, you are charged 3 reads, not 1000.
The exception is that queries that return no documents are billed for a single document read. This means every query incurs a cost of at least one document read, no matter the results. If your query spans multiple requests (because of paging), you are billed at least one document read per request.
Please consult the documentation for Firestore billing to get more detailed information.
As per your query If you are fetching all documents at once on the client end and running your criteria search it will be counted as a single query.
However, if you use the query method of Firestore it will be also counted as a single query.
Note:
Don't have references yet, I am telling through the personal experience.

Firestore Pricing - Does The Amount of Documents In a Collection Matters?

I have read in the documentation that I'm being charged for the amount of the requests I'm making to read, write or update documents. I have also read that reading a collection is priced the same as a reading a document ("For queries other than document reads, such as a request for a list of collection IDs, you are billed for one document read."), correct me if I'm wrong.
My question is: Does reading a collection with a big amount of documents in it (let's say - 10,000 documents) is priced the same as reading one with 10?
I'd like to get some explaination about it...
It depends on what you mean by "reading a collection", but for most people this means "querying a bunch of documents from a collection". And the answer is that the pricing generally depends on the number of documents retrieved.
To oversimplify things just a bit:
If you have a collection of 10 employees and you run a collection("employees").get() call, you will get back 10 employee documents, and be charged for 10 reads.
If you have a collection of 10,000 employees and you run a collection("employees").get() call, you will get back 10,000 employees, and be charged for 10,000 reads.
If you have a collection of 10,000 employees and you run a collection("employees").get().limit(10) call, you will get back 10 employees, and be charged for 10 reads.
If you have a collection of 10,000 employees, 4 of which are named "Courtney" and you run a collection("employees").where("first_name", "==", "Courtney") call, you will get back 4 employees and be charged for 4 reads.

Resources