user9290695
user9290695

Reputation: 43

Queries depending on dataset in Firestore

Recently I have migrated from firebase realtime database to firebase firestore because of the fact that it says the speed of the query depends on the size of the dataset(number of documents) being retreived from collection and not on number of documents in a collection. I checked with varying number of documents in a collection 100, 5000 and 10000 and I was retreiving 20 documents in one query. What I saw was the resulting time for the query increased when I moved from 100, 5000 and 10000 documents in a collection. Why is this happening ? Is it because firestore is in beta ?

Querying on android (collection with 21000 docs)

   collectionReference= FirebaseFirestore.getInstance().collection("_countries").document("+91").collection("_classes-exams").document(String.valueOf(mItem)).collection("_profiles").document(mOtherUserUid==null?uid:mOtherUserUid).collection("user_data");


collectionReference.document("_content").collection(articlesOrQuestions)

                    .orderBy("mTimeStampAsDate", Query.Direction.DESCENDING).limit(20).get().addOnCompleteListener(mCompleteListener)

                    .addOnFailureListener(new OnFailureListener() {

                        @Override

                        public void onFailure(@NonNull Exception e) {

                            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();

                        }

                    });

Image of android monitor when querying the above collection reference: https://i.sstatic.net/QZaVX.jpg

You can see the query took almost one minute by looking at the heap(after one minute memory didn't changed much and remained constant and there is sudden spike in network section after 1 minute by which you can infer onComplete is called). What is happening between calling 'get()' function and 'onComplete' callback. This doesn't happen when querying small collections. and why the query on web is fast but slow on android ? Link to jsbin: http://jsbin.com/fusoxoviwa/1/edit?html,css,js,console,output

Upvotes: 4

Views: 397

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599716

Did you write these collections from the same Android client that's now loading a subset of documents from them? If so, that would explain.

The client-side cache in that case will contain information about all docs, and your app is spending time churning through that information.

If you try on a "clean client", it won't have any information cached and it should only spend time on documents that the client is requesting (or has requested before).

The behavior you're currently seeing should improve before Firestore exits beta, since the index will become more efficient, but also because it'll get some form of GC.

Upvotes: 2

Related Questions