Tord Larsen
Tord Larsen

Reputation: 2840

How to search in Firebase this very large list of users

I try to create a people of the world model in Firebase. This means that somewhere I need to put 500 miljon+ records like this Firebase json:

      "USER" : {
        "HnhLyXRxUINmlltKOfxx2QBYiQ53" : {  // FireBase user ids
          "FRIENDLYS" : {
            "-KWrFrMx1yQe0jhyA7HP" : { // Push ids
              "animal" : "dog"
            },
            "-KWrG7EsLkZvASjpcW3A" : {
              "animal" : "cat"
            },
            "-KWrG9hbQxrZwdfjqbuQ" : {
              "animal" : "horse"
            }
          },
          "email" : "[email protected]",
          "lastName" : "Doe",
          "firstName" : "John",
          "googleId" : "10086464927329323e2326"
        },
         "HnhLyXHKJH67677GJKMnbBYiQ53" : {  // FireBase user ids
           "FRIENDLYS" : {
            "-KWrFrMdjnjs982s7HP" : { // Push ids
              "animal" : "ant"
            ....

        }

I want to search for one USER like this:

firebase.child("USER/HnhLyXRxUINmlltKOfxx2QBYiQ53").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

The question is if this is a good approach to keep building on, especially Is it feasible to have a "USER" object this big when searching like i do?

What can I expect?

Upvotes: 1

Views: 518

Answers (1)

vzsg
vzsg

Reputation: 2896

Your code is not searching for a user. You're constructing a direct reference to a child node, which operation itself does not have any performance implications. Looking up a key directly is always fast, so the number of users will not negatively affect your application.

However, storing "FRIENDLYS" [sic] under the user does not scale. Loading a single user node will get slower with each extra child put there. You should store them separately under a different root node and load them only if you actually need them.

See also the Structure Your Data section of the Realtime Database guide.

Upvotes: 2

Related Questions