Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete Single-field indexes that generated automatically by firestore?

update:
TLDR;

if you reached here, you should recheck the way you build your DB. Your document(s) probably gets expended over time (due to nested list or etc.).

Original question:
I have a collection of documents that have a lot of fields. I do not query documents even no simple queries- I am using only-

db.collection("mycollection").doc(docName).get().then(....);

in order to read the docs, so I don't need any indexing for this collection.

The issue is that firestore generates Single-field indexes automatically, and due to the amount of fields cause limitation exceeding of indexing: enter image description here And if I trying to add a field to one of the documents it throws me an error:

Uncaught (in promise) Error: Too many indexed properties for entity: app: "s~myapp",path <  Element {    type: "tags",    name: "aaaa"  }>
    at new FirestoreError (index.cjs.js:346)
    at index.cjs.js:6058
    at W.<anonymous> (index.cjs.js:6003)
    at Ab (index.js:23)
    at W.g.dispatchEvent (index.js:21)
    at Re.Ca (index.js:98)
    at ye.g.Oa (index.js:86)
    at dd (index.js:42)
    at ed (index.js:39)
    at ad (index.js:37)

I couldn't find any way to delete these single-field-indexing or to tell firestore to stop generating them. I found this in firestore console: enter image description here

but there is no way to disable this, and to disable auto indexing for a specific collection. Any way to do it?

like image 395
ykorach Avatar asked Jul 12 '18 08:07

ykorach


People also ask

How do I remove a firestore index?

Remove indexesGo to the Cloud Firestore section of the Firebase console. Click the Indexes tab. Hover over the index you want to delete and select Delete from the context menu. Confirm that you want to delete it by clicking Delete from the alert.

What types of indexes are automatically created in cloud firestore?

Cloud Firestore uses two types of indexes: single-field and composite.

Is there any way to update a specific index from the array in firestore?

Is there any way to update a specific index from the array in Firestore? No, there is not! This is not possible because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.

How do I remove a field from a document in firebase?

If You Want To Delete A Specific Field From A Firebase Firestore Document, Use The Fieldvalue. Delete() Method When You Update A Document. If you want to delete a specific field from a firebase document, use the `FieldValue. delete()` method when you update a document.


2 Answers

You can delete simple Indexes in Firestore firestore.

See this answer for more up to date information on creating and deleting indexes.

Firestore composite index permutation explosion?

If you go in to Indexes after selecting the firestore database and then select "single" indexes there is an Add exemption button which allows you to specify which fields in a Collection (or Sub-collection) have simple indexes generated by Firestore. You have to specify the Collection followed by the field. You then specify every field individually as you cannot specify a whole collection. There does not seem to be any checking on valid Collections or field names.

The only way I can think to check this has worked is to do a query using the field and it should fail.

I do this on large string fields which have normal text in them as they would take a long time to index and I know I will never search using this field.

Firestore creates two indexes for every simple field (ascending and descending) but it is also possible to create an exemption which removes one of these if you will never need the second one which helps improve performance and makes it less likely to hit the index limits. In addition you can select whether arrays are indexed or not. If you create a lot of entries it an Array, then this can very quickly hit the firestore limits on the number of indexes, so care has to be taken when using indexes and it will often be best to take the indexes off Arrays since the designer may have no control over how many Array data items are added with the result that the maximum index limit is reached and the application will get an error as the original poster explained.

You can also remove any simple indexes if you are not using them even if a field is included in a complex index. The complex index will still work.

Other things to keep an eye on.

If you are indexing a timestamp field (or any field that increases or decreases sequentially between documents) and you are not using this to force a sequence in queries, then there is a maximum write rate of 500 writes per second for the collection. In this case, this limit can be removed by removing the increasing and decreasing indexes.

Note that unlike the Realtime Database, fields created with Auto-ID do not guarantee any ordering as they are generated by firestore to spread writes and avoid hotspots or bottlenecks where all writes (and therefore reads) end up at a single location. This means that a timestamp is often needed to generate ordering but you may be able to design your collections / sub-collections data layout to avoid the need for a timestamp. For example, if you are using a timestamp to find the last document added to a collection, it might be better to just store the ID of the last document added.

Large array or map fields can also cause the 20,000 index entries per document limit to be reached, so you can exempt the array from indexing (see screenshot below).

enter image description here

Once you have added one exemption, then you will get this screen. Exemption screen once one has been added

See this link as well.

https://firebase.google.com/docs/firestore/query-data/index-overview

like image 108
Philip Avatar answered Sep 27 '22 17:09

Philip


The short answer is you can't do that right now with Firebase. However, this is a good signal that you need to restructure your database models to avoid hitting limits such as the 1MB per document.

The documentation talks about the limitations on your data:

You can't run queries on nested lists. Additionally, this isn't as scalable as other options, especially if your data expands over time. With larger or growing lists, the document also grows, which can lead to slower document retrieval times.

See this page for more information about the advantages and disadvantages on the different strategies for structuring your data: https://firebase.google.com/docs/firestore/manage-data/structure-data

like image 39
mtflud Avatar answered Sep 27 '22 17:09

mtflud