Todd
Todd

Reputation: 179

SearchView with Firebase RealTime Database is taking too long to bring results and it's expensive

I'm trying to get a specific user by it's mail with a searchview in my app.

I have more than 7000 users in my database and with this searchview it takes like from 5 to 10 or 15 seconds to filter and bring data

@Override
public boolean onQueryTextSubmit(String s) {

    searchQuery.startAt(s).endAt(s+"\uf8ff").limitToFirst(3).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                UserPojo user = snapshot.getValue(UserPojo.class);
                Log.i(TAG, "onDataChange: " + user.getEmail());

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    return false;
}

the problem is the time it takes to filter the data (from 5 to 15 seconds)

Is there anyway to filter faster with a better query ? or do I need to get all the data at first , store locally and then filter it there in client side ? any suggestions on how to approach this ?

Also I have noticed with the profiler that each search is taking from 4 to 8mb of network in order to get done, that's a lot of network use for just a simple text query.

See network usage here

thanks

Upvotes: 0

Views: 73

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599776

It sounds like you may not have defined an index on the field you're searching on. If there is no index, the filtering cannot be done on the server. So in that case the server sends all data to the client, which does the filtering. This would explain why it takes longer than you expect: it has to send all 7000 user profiles to the client.

If this is indeed the cause of the problem, you can fix it by adding an index in your rules file. Say that you're ordering/filtering a list called users on a property called email, you'd do this by:

{
  "rules": {
    "users": {
      ".indexOn": "email"
    }
  }
}

For more on this topic see the Firebase documentation on indexing data.

Upvotes: 1

Related Questions