DogCoffee
DogCoffee

Reputation: 19966

Firebase: Data Structure

When does the number of members within this node begin to affect performance ?

Example Node

Groups
    id_0
      groupName: Bills Cooking
      members
        uid_0: true
        uid_1: true
        uid_2: true

I am wondering when the number of keys within the members group becomes too many, and begins affect performance.

Each group contains a reference id to all the members associated with it, there may be 0 or they could be 1000 or more.

The members node (not show) would have their own reference indicating what groups they belong to. From my understanding this is a good way to structure your data in Firebase, having reference keys in both areas.

If want to list all the group names in a tableview, I’d fetch all the group nodes to get their names. The problem is doing so I’m fetching all the members keys at the same time - I’m sure its fine for 10 or so I just wonder whats the point where you say - time to refactor. If there are 100 groups and 1000 users in each group - you are fetching a lot of extra data.

I understand I could create another node and avoid this problem, but this is how Firebase recommends we structure the data. As detailed here

Thanks

Upvotes: 0

Views: 238

Answers (1)

Jay
Jay

Reputation: 35677

Here's the key to the answer:

If want to list all the group names in a tableview

If there are 100 groups and 1000 users in each group

100 groups is fairly small, even if there's a number of users in each group. So thinking through this...

A user logs in and a query is done which returns the groups they belong to.

Assuming that each user is not a member of every group (if they were, the structure would be un-needed) it will return some number of groups less than 100.

More importantly, for a good user experience, you probably aren't going to display 100 groups at a time - it will be some subset of that; perhaps 15 or so.

If you are worried about application memory space, instead of query'ing by .Value which returns all of the data at once, query by .ChildAdded which will return each node one at a time, grab the group name and toss the rest of the data.

Another thought is to break the group names out and look them in code as needed.

group_names
      gid_0: Bills Cooking
      gid_1: Jay's Big Band Extravagnza
      gid_2: Kato's Karate Korner

There's only 100 of them so upon app start load them all into a dictionary.

Then in the users node

users
  uid_0
    name: Jesse
    groups:
      gid_1: true
      gid_2: true

The user logs in and you know he belongs to group 1 and 2 and you already have those names from the groups dictionary

let groupName = groups_dict.objectForKey["gid_2"] //Kato's Karate Korner

Upvotes: 3

Related Questions