0

I need some clarification about Firebase query costing, and I have user node like below JSON (it's not in the correct format)

"User": {
    "user1": {
        "filter1": "...",
        "filter2": "...",
        "filter3": "...",
        "filter4": "..."
    },
    "user2": {
        "filter1": "...",
        "filter2": "...",
        "filter3": "...",
        "filter4": "..."
    },
    "userN": {
        "filter1": "...",
        "filter2": "...",
        "filter3": "...",
        "filter4": "..."
    }
}

Now, the query is

APPROACH 1

Userref.orderByChild("filter1").equalsTo("string").addListenerForSingleValueEvent;

This query returns only the data which has filter1 value is "string" (if i am not wrong). For example in 100 users, if firebase query matches only 5 then query returns only 5 users.

But I need some more filter, but more than one filter is not possible according to official document.

for this, APPROACH 2 I used

Userref.addListenerForSingleValueEvent();

this will return all 100 users data. and all filter set on the client-side.

Now the question is in 1st approach query returns only 5 users and, in 2nd approach query returns 100 users,

So can its query-based costing change? and if no. of users goes to 100 000, then data return timing (loading time) will be the same or different in both approaches?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Vora
  • 347
  • 2
  • 15

1 Answers1

2

This query returns only the data which has filter1 value is "string" (if I am not wrong).

You're not wrong, you're actually correct.

For example in 100 users, if firebase query matches only 5 then the query returns only 5 users.

Yes, that's the use of a query, to filter results.

But I need some more filter, but more than one filter is not possible according to the official document.

That also correct because there is no way you can add multiple:

.orderByChild("filterX").equalsTo("string")`

Calls to a query. However, there is a workaround that can help you solve this issue and for that, please try my answer from the following post:

How to sort Firebase records by two fields (Android)

Now the question is in 1st approach query returns only 5 users and, in 2nd approach query returns 100 users

Yes, that correct because when you are not filtering the results using a query, you are getting all children under a node. Getting all the data and filter the result on the client it's not an option since downloading the entire node is considered a waste of bandwidth and resource.

of users goes to 100 000, then data return timing (loading time) will be the same or different in both approaches

Downloading such a huge amount of data, it's not an option to ahead with. If the above solution will not help you solve the problem, you can try to use Cloud Firestore, where chaining multiple whereEquals() work perfectly fine.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Fully satisfied but what about costing affect in between two approaches ??? – Vora Jul 18 '20 at 14:39
  • 1
    Downloading the entire node using the second solution will be more expressive. You should always get only the data that you need. – Alex Mamo Jul 18 '20 at 14:58