Gabriel
Gabriel

Reputation: 488

Understanding Firestore queries

I've been picking up some Firestore. Had a couple of questions regarding the careful usage of read and writes.

For an example of 1000 records in a collection called places: db.collection("places")

I wanted to understand the time/query complexity of a code as such

let doc_ID = `key`
db.collection("places").doc(doc_ID).get();

compared to

let doc_ID = `key`
db.collection("places").where("key_to_find", "==", doc_ID).get();

Does it loop through the 1000 records to find the key? In the first case I'm not too sure. But surely there should be a better approach if my collections have 1,000,000 records? Won't the time complexity be significant when I start doing queries in large collections?

My apologies if this is a beginner question. Wondering if there are differences between these 2 examples and whether any is faster or more efficient. Will also appreciate any directions to articles and resources talking about best querying techniques in Firestore.

Much thanks in advance!

EDIT: Another question to be how the querying relates to the number of READs, because we're paying for the reads per day in Firestore.

Upvotes: 1

Views: 655

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317477

Firestore uses a distirubted index to find documents in a collects, based on the filters you provide in the query. There is nothing slow about this index, and the size of the collection doesn't matter at all in terms of performance. Firestore scales massively. Every query for 1000 documents performs the same as every other query for 1000 documents. In this respect, we say that Firestore queries scale with the size of the result set, not with the size of the collection.

This might be difficult to believe at first, but this is how it was designed, and how you can expect it to perform. Firestore will simply not perform a query that scales this way, powered by its indexes. It will either tell you what about the query is not supported, or it will give you a link to create an index that serves the query at scale.

Upvotes: 1

Related Questions