Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Firestore Query stream & get?

I'm planning to query large set of data from Firestore database using NodeJS api. Is there any benefit on using stream api (https://cloud.google.com/nodejs/docs/reference/firestore/0.13.x/Query?#stream) instead of reqular query get (https://cloud.google.com/nodejs/docs/reference/firestore/0.13.x/Query?#get)?

My impression is that streaming is more efficient in terms of memory consumption.

My environment for querying is Firebase functions.

like image 573
ovaris Avatar asked May 04 '18 12:05

ovaris


People also ask

What is the difference between firestore and realtime?

Cloud Firestore is Firebase's newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales further than the Realtime Database. Realtime Database is Firebase's original database.

How do I speed up firestore queries?

One simple option is to add limits to your queries. If you suspect that your user only needs the first handful of results from your employee list, add a limit(25) to the end of your query to download just the first batch of data, and then only download further records if your user requests them.

What is query in Firebase?

A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location. The Query class (and its subclass, DatabaseReference ) are used for reading data. Listeners are attached, and they will be triggered when the corresponding data changes.

What is onSnapshot in Firebase?

You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.


1 Answers

Yes, using stream over get will reduce the memory consumption of your Cloud Function! I have experienced it first-hand: my function's memory went from 1GB to 200MB when I replaced get with stream.

To give more details: with the Node.js Admin SDK, in order to access your data:

  • First you define a query. You can specify a collection and add where, offset, limit, startAfter to describe more precisely the data that you need. If you don't add any specifier, you are querying the entire collection.
  • Then you fetch the corresponding results, through one of these 3 functions:
    • get, which returns a snapshot containing all documents matching your query (or the whole collection if you did not limit your query). You absolutely need to add a limit clause to your query if you do not want to explode your memory.
    • onSnapshot, which creates a listener which is going to provide you with documents matching your query. I noticed that it sends an initial big batch of all documents, then sends documents as they are created. So it can explode your memory, and you don't know when to stop listening, so I don't use it in Cloud Functions (but I really like it on client-side, another story)
    • stream, which provides you with a Node.js readable stream. You can consume this stream which provides documents one by one, so it is nice to your memory.
like image 54
Louis Coulet Avatar answered Sep 21 '22 20:09

Louis Coulet