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