Why does this firestore query require an index?

29,142

Solution 1

Why does this Firestore query require an index?

As you probably noticed, queries in Cloud Firestore are very fast and this is because Firestore automatically creates an index for any field you have in your document. So when you simply filter with a range comparison, Firestore creates the required index automatically. If you also try to order your results, another index is required. This kind of index is not created automatically. You should create it yourself. This can be done, by creating it manually in your Firebase Console or you'll find in your logs a message that sounds like this:

FAILED_PRECONDITION: The query requires an index. You can create it here: ...

You can simply click on that link or copy and paste the URL into a web browser and your index will be created automatically.

So Firestore requires an index so you can have very fast queries.

Solution 2

An index is simply a database inventory or a record of what is where. And each index is a specific inventory of a specific thing—for example, how many propertyX fields exist in a collection and what their values are, sorted (the fact that they are sorted is critical).

If this inventory didn't exist, to query for documents where propertyX is someValue, the machine would have to iterate over the entire collection to determine (1) which documents contain propertyX and (2) which documents contain propertyX equal to someValue. By keeping an inventory (or index) of queried properties, when a query is performed on propertyX, the machine can go straight to the propertyX index and gather the locations of all the documents that equal someValue and then fetch those documents from the collection and return them. Not only does the machine not need to touch the collection to know where the documents are but it doesn't even need to iterate over the entire index because it's always in order.

Indexes are why collection sizes have no impact on the performance of Firestore queries and why we only need to index properties that are ever queried.

Share:
29,142
tokism
Author by

tokism

Updated on July 09, 2022

Comments

  • tokism
    tokism almost 2 years

    I have a query with a where() method with an equality operator and then an orderBy() method and I can't figure out why it requires an index. The where method checks for a value in an object (a map) and the order by is with a number.

    The documentation says

    If you have a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field

    So I would have thought that an equality filter would be fine.

    Here is my query code:

    this.afs.collection('posts').ref
    .where('tags.' + this.courseID,'==',true)
    .orderBy("votes")
    .limit(5)
    .get().then(snap => {
      snap.forEach(doc => {
        console.log(doc.data());
      });
    });
    

    Here is an example of the database structure enter image description here

  • Devashish
    Devashish almost 4 years
    But what happens in the case where the collection is constantly being updated and new documents are being added all the time? Do I have to rebuild the index again and again or once I build it (right now with only 5 documents in the collection, for testing/development), it will automatically scale it for more documents later on in production mode?
  • Alex Mamo
    Alex Mamo almost 4 years
    @Devashish There is no need to do anything, once you create an index. It will work with any number of documents.
  • Rachit Rawat
    Rachit Rawat almost 3 years
    I cannot find the link for creating the index in the new versions of cloud firestore. I have to manually build indexes from firebase console. My error only shows, "Error: [cloud_firestore/failed-precondition] Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console.". How to fix this?
  • Alex Mamo
    Alex Mamo almost 3 years
    @RachitRawat As the warning message states, most likely you should add the index directly in the Firebase Console.
  • Rachit Rawat
    Rachit Rawat almost 3 years
    But i have a lot of indexes that needs to be built. Is it possible to get this link from somewhere to ease out the process?
  • Alex Mamo
    Alex Mamo almost 3 years
    @RachitRawat You can use the Firebase CLI.
  • Oooha
    Oooha over 2 years
    @AlexMamo what if the collection id is dynamic?Like there can be many collection names that are unknown at the start.
  • Alex Mamo
    Alex Mamo over 2 years
    @Oooha I think that this answer will help.