firestore: manage indexes

Manage Indexes in Cloud Firestore

Cloud Firestore requires an index for every query, to ensure the best performance. All document fields are automatically indexed, so queries that only use equality clauses don't need additional indexes. If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.

Follow the generated link to the Firebase console, review the automatically populated info, and click Create.

Note:  You must select ascending or descending ordering on every field, even if you don't need the field for ordering. Your choice doesn't impact the behavior of equalities in the query.

Use the Firebase console

To manually create a new index from the Firebase console:

  1. Go to the Database section of the Firebase console.
  2. Go to the Indexes tab and click Add Index.
  3. Enter the collection name and set the fields you want to order the index by. Click Create.

Indexes can take a few minutes to build, depending on the size of the query. After you create them, you can see your indexes and their status in the Composite Indexes section. If they're still building, the Firebase console includes a building status bar.

Use the Firebase CLI

You can also deploy indexes with the Firebase CLI. To get started, run firebase init firestore in your project directory. During setup, the Firebase CLI generates a JSON file with the default indexes in the correct format. Edit the file to add more indexes and deploy it with the firebase deploycommand. If you only want to deploy indexes, add the --only firestore:indexes flag. If you make edits to the indexes using the Firebase console, make sure you also update your local indexes file.

Remove indexes

To delete an index:

  1. Go to the Database section of the Firebase console.
  2. Click the Indexes tab.
  3. Hover over the index you want to delete and select Delete from the context menu.
  4. Confirm that you want to delete it by clicking Delete from the alert.

猜你喜欢

转载自blog.csdn.net/leonqiu/article/details/79428118