Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying on Firebase Database with large data set is very very slow

I use Firebase database on my Android app. Normally, it works fine. But when the database is getting larger, the query performance is getting worse. I added about 5k record on database (under "elk" and "su" nodes), then I queried on database (on "cut" and "user" nodes) but all the queries are very very slow. I defined data index on database rules but it did not work. How can I solve that problem?

Here are my queries :

// query to get the zones followed by user
FirebaseDatabase.getInstance()
                .getReference()
                .child("user")
                .child(userID)
                .child("zones");

// query to get cuts on a zone
FirebaseDatabase.getInstance()
                .getReference()
                .child("cut")
                .child(cutType)
                .orderByChild("zoneID")
                .equalTo(zoneID);

my database structure

database rules

like image 592
cimenmus Avatar asked Oct 29 '22 19:10

cimenmus


1 Answers

If you want to continue expanding the best thing to do would be to duplicate your data in a zone reference where it knows which elk/su are a part of it. Something like this:

{
    zones: {
        elk: {
            "istan-besik": {                
                "-KSp)bL5....": true,
                ...: true
            }
        }
    }
}

That way when you want to search for all you would just do:

...child('zones').child(zoneId).child(cutType)

And then loop through those to go get each elk/su directly

like image 55
Mathew Berg Avatar answered Nov 15 '22 06:11

Mathew Berg