Reputation: 751
Consider a simple query like firestore.collection('animals').where('name', isEqualaTo:'Zebra').get();
what is the data usage of this query, does it read all the documents till it find the (Zebra) or does it finds it in no time. Thanks
Upvotes: 1
Views: 173
Reputation: 40582
Firestore uses sorted indices and a binary search pattern. This makes reads extremely fast and basically makes it impossible to create a slow query.
An overview of Firestore queries is published here. And there's a detailed explanation of the indices in Todd's How do queries work in Cloud Firestore? video at time 5:30.
You pay for exactly how many documents are returned. So only the matches and only up to the limit you set. There's a detailed explanation and example billing scenario in the docs here.
Note that compound queries are also extremely fast since we use a ZigZag merge join (explained in this I/O talk and summarized in Todd's video at time 9:22).
What are the tradeoffs for all of this amazing read speed? Since the sorted index is created at write time, this limits the write speeds, which is a common NoSQL philosophy since reads are generally much more common. Of course, you can optimize write speeds by removing the automatic indices on every field and only indexing fields you need for querying.
Since we rely on a zig-zag index, it also limits the complexity of queries that can be performed, which is why OR and CONTAINS queries are difficult and convoluted in Firestore.
Upvotes: 1