Home > Net >  Android firestore using "whereTo()" many times causes cost a lot?
Android firestore using "whereTo()" many times causes cost a lot?

Time:02-14

I am making a function to classify by user's region and MBTI.

Here is a code.

private void EventChangeListener() {
    //firestore.collection("Users").orderBy("time",Query.Direction.DESCENDING).limit(50)
    firestore.collection("Users").orderBy("time",Query.Direction.DESCENDING).limit(50)
            .whereEqualTo("gender","여자")
            .whereEqualTo("mbti","ENTP")
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                    if(error != null){

                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }

                        Log.e("Firestore error",error.getMessage());
                        return;

                    }

                    for (DocumentChange dc : value.getDocumentChanges()){

                        if (dc.getType() == DocumentChange.Type.ADDED){
                            userArrayList.add(dc.getDocument().toObject(User.class));
                        }

                        myAdapter.notifyDataSetChanged();
                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }
                    }

                }
            });
}

Like this whereEqualTo().whereEqualTo().whereEqualTo() causes a lot cost??

If It is right, then is there any other way?

CodePudding user response:

You are charged for number of documents that match your query and are returned. Number of query clauses do not affect cost. Using more filters like whereEqualTo().whereEqualTo().whereEqualTo() will ideally reduce number of matches and cost less.

For example, if you have a collection of 50 restaurants and you use 1 whereEqualTo on 'city' field then it'll return and cost N only for restaurants that are present in that city. If you add another whereEqualTo on 'ratings' then number of matches might be even less than N.

  • Related