[SOLVED] Is it okay to filter firebase data using conditions? Will it affect the speed and accuracy of Firebase realtime database?

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.



final String campus = "BISU Calape";

private ValueEventListener valueEventListener = new ValueEventListener() {       
    public void onDataChange(@NonNull DataSnapshot snapshot) {

        for(DataSnapshot dataSnapshot : snapshot.getChildren()){
            Faculty faculty = dataSnapshot.getValue(Faculty.class);
            if(faculty.getCampus().equals(campus) && faculty.getMemStatus.equals("Approved")){

        if(facultyList.size() > 0){


    public void onCancelled(@NonNull DatabaseError error) {
        Toast.makeText(FacultyActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();

This code is working so far, but what if this code will have to fetch/filter millions of records?


The Firebase Realtime Database doesn’t support queries on multiple properties. It supports only queries on a single child property. There is however a workaround in which you can create a new field in the database that can hold both values:

   ---- faculties
         --- $campusId
                --- campus: "BISU Main Campus"
                --- status: "Approved"
                --- campus_status: "BISU Main Campus_Approved"

So as you see, the campus_status field combines the values that you want to filter on.

So now, the following query will work:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference facultiesRef = db.child("faculties");
Query queryByCampusAndStatus = facultiesRef.orderByChild("campus_status").equalTo("BISU Main Campus_Approved");

Unlike the Realtime database, Cloud Firestore allows compound queries. This means that you can use multiple whereEqualTo() calls. So a query like the one below it will work perfectly fine:

facultiesRef.whereEqualTo("campus", "BISU Main Campus").whereEqualTo("status", "Approved");

Answered By – Alex Mamo

people found this article helpful. What about you?