Home > Back-end >  Firebase query effectiveness with queryLimited
Firebase query effectiveness with queryLimited

Time:04-14

I am wondering about the current topic and I know that some fellow firebase experts are quite quick here to respond that's why I share it here.

If I query data from a child and limit it to the last24, does it consume the same amount as when I query only the last one and do it 2 times? (Pagination, generally I am wondering if I should query more posts at once to decrease data consumption or if its the same)

example is it the same if I call this once

query.queryLimited(toLast: 2).observeSingleEvent

or this twice?

query.queryLimited(toLast: 1).observeSingleEvent

just from the viewpoint of data consumption

CodePudding user response:

just from the viewpoint of data consumption

A quick answer is that it really depends on the size of the objects.

Let's take a quick example. Let's assume we have a node of users that looks like this:

Firebase-root
   |
   --- users
        |
        --- $uid
        |    |
        |    --- name: "Zash__"
        |
        --- $uid
             |
             --- name: "Alex Mamo"

As you can see, my object is larger in size than yours, since my name contains nine characters while yours contains only six. So for the sake of this example, we could say that your object has a size of 6 bytes, while mine has 9 bytes.

So if you're using the following query:

query.queryLimited(toLast: 2).observeSingleEvent

The amount of data that will be downloaded will be (6 9) 15 bytes. On the other side, if you're using the following query twice:

query.queryLimited(toLast: 1).observeSingleEvent

The amount of data that will be downloaded will be (9 9) 18 bytes, considering that the last user in the query it's me.

So the amount of data that you download will always depend on the size of the objects that you're downloading. If by chance, both objects will have the exact same size, then the amount of data that will be downloaded will be the same.

If you want not to care about the size of the objects, then you should consider using Cloud Firestore, where it doesn't matter the size of the documents. This means that if a document is 20 bytes in size and you read it or is 1 Mib (the maximum size), you'll always have to pay the same price, which is a single document read.


P.S. Most likely the calculation isn't done exactly that way, but it was just an example to understand the concept.

  • Related