2

It is a pretty general question but I couldn't find any document that explains this.

I use firestore as my main database for my app.

I recently implemented an autocomplete system by querying my firestore in the following 2 ways (first way to catch parts of the word and second to catch whole word if it is not at the beginning of the sentence):

db.collection( "IDS" )
        .whereGreaterThanOrEqualTo("ID", s.toString() ).whereLessThanOrEqualTo( "ID", s.toString() + "\uF7FF" ).limit(10).get()
        .addOnCompleteListener( new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        String id = document.getString( "ID" );
                        ids.add( id );
                    }

                }

            }
        } );

db.collection( "IDS" )
        .whereArrayContains("IDArray", s.toString() ).limit(10).get()
        .addOnCompleteListener( new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        List<String> idsParts = (List<String>) document.get( "IDArray" );
                        String id = TextUtils.join( " ", idsParts );
                        ids.add( id);
                    }

                }

            }
        } );

This code works well however I'm trying to understand the way it works.

Let say I have 1 million documents in my IDS collection. When I use the queries above, does it searches all of the 1 million documents and checks each one? Or more specifically, does it mean that it will count as 1 million reads or eventually since I used .limit(10) it means that only 10 reads were used?

I'm asking because this app should be published soon and I would like to figure out if the methods above will be very expensive to query.

If there is another way to optimize the query I would be happy to know.

Thank you

benb
  • 351
  • 1
  • 2
  • 7

1 Answers1

0

The concept is simple:

Number of documents resulted = number of read operations charged

And yes when you use limit, you tell the query to give you your-specified number of result.

For eg. If you are executing query on 100 documents with which your query satisfies only 15 documents, you will be charged for just 15 documents. And further on if you use limit(10), you will be resulted with only 10 documents (with 10 reads).

s_o_m_m_y_e_e
  • 406
  • 4
  • 9
  • So in my case if I query 1 million document and only 5 give me the result it counts as 5 reads? Is there any source for this to understand it more deeply? – benb Sep 27 '20 at 08:19
  • Yes you will only be charged for 5 documents. Here are some details about it:https://firebase.google.com/docs/firestore/pricing. And maybe here too: https://stackoverflow.com/a/47147934/11292402 – s_o_m_m_y_e_e Sep 27 '20 at 08:24