Vinicius Folha
Vinicius Folha

Reputation: 109

Firestore onSnapshot: One query complex or multiple query simple

There are any cost or performance difference about creates an onSnapshot with a complex clause or creates 3 on Snapshots with a simple one. In addition, the posts collection has a rule which check in the posts_permission collection if logged user has permission to read the posts. It's a mobile project, so I'm concerned with the performance and cost because the posts can be updated constantly.

Complex clause:

const query = db.collection('posts')
            .where(firebase.firestore.FieldPath.documentId(), 'in', ['id1','id2', 'id3']);
query.onSnapshot(snap => {console.log(snap)});

Simple clauses:

const query1 = db.collection("posts").doc("id1")
const query2 = db.collection("posts").doc("id2")
const query3 = db.collection("posts").doc("id3")
query1.onSnapshot(snap => {console.log(snap)});
query2.onSnapshot(snap => {console.log(snap)});
query3.onSnapshot(snap => {console.log(snap)});

Upvotes: 0

Views: 894

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317750

In terms of cost, X documents read always just cost X document reads. It doesn't matter how many queries it took. You will also pay for each document read by the evaluation of a security rule. It's really easy to compute the costs if you just follow the information in the documentation. There are no hidden costs.

In terms of performance, there is really no big difference, between multiple queries and a single query. Unless you are dealing with very large amounts of documents, you will likely see comparable performance based on the size and number of documents, and the quality of the network connection. All read documents are pipelined over a single connection, so there is no substantial difference in receiving documents from multiple listeners as opposed to one listener. You should benchmark this yourself if you have concerns.

Upvotes: 1

Related Questions