Indexing and Performance

Indexing is a foundational feature in Oracle Backend with Firebase APIs that enables efficient querying, filtering, and sorting of documents. Without proper indexing, queries may fail or perform poorly. Oracle Backend with Firebase APIs supports both automatic and functional indexing strategies, each tailored to different query patterns and data models.

This chapter explains the types of indexes, their behavior, and how to configure them for advanced use cases like compound filters, collection group queries, and join collections.

Overview

In Oracle Backend with Firebase APIs, every document is stored in a structured format - either as a JSON-style document (Collection Model) or as a relational row (Relational Model). To retrieve documents efficiently, Oracle Backend with Firebase APIs relies on indexes to:

Without the appropriate index, Oracle Backend with Firebase APIs rejects the query with an error indicating that an index is missing.

Types of Indexes

Oracle Backend with Firebase APIs supports two primary types of indexes:

Index Type Description Use Cases
Automatic Indexes Created implicitly for each field in a document. Simple filters, single-field ordering.
Functional Indexes Explicitly defined by the developer through the Console. Compound filters, joins, collection group queries, multi-field ordering.

Index and Security Rule

Every indexed query must be backed by a security rule that:

If a query is supported by an index but lacks a matching rule, it will be rejected.

Best Practices

Automatic Indexes

Automatic indexes are created for every field in a document when a collection is created. Automatic indexing can be enabled from the Console. When enabled, Automatic indexing creates a search index on each field for any collection present in the project. Developers can exempt specific collections from auto-indexing using the Console.

Automatic indexes support:

However, automatic Indexing has these limitations because it:

Note:

Functional Indexes

Functional indexes are explicitly defined by the developer in the Oracle Backend with Firebase APIs Console. These indexes are required when:

Characteristics

How to Create Functional Indexes

Functional indexes are created in the Console by:

  1. Navigating to the project’s indexing section

  2. Defining the collection or join path

  3. Selecting the fields to index

  4. Publishing the index

Each functional index must be paired with a corresponding security rule to be usable.

Compound Query Indexing

A compound query filters or sorts on multiple fields. These queries require a functional index that includes all involved fields in the correct order.

Example 19-1 Compound Query

where("category", "==", "Dessert")
  .orderBy("views", "desc")

Required Index:

If the index is missing, Oracle Backend with Firebase APIs rejects the query with an error.

Compound query indexes are used to optimize filtering (conditions) and sorting (explicitOrder) in getDocs() queries.

Collection Group Indexing

A Collection Group allows querying across all subcollections with the same name, regardless of their parent document path. Querying a collection group, for example, to query all notes subcollections under different recipes, you require:

Example 19-2 Security Rule

match /users/{userId}/recipes/{recipeId}/notes/{noteId} {
  allow list: if request.auth != null;
}

Join Collection Indexing

Join Collections are derived from relational tables and allow querying across multiple tables using defined relationships.

The following must be completed to query join collections:

Example Join

A join between employee and department tables, which is indexed on employee.department_id and department.name

Example 19-3 Security Rule

match /EMP_DEP/_docId {
  allow get: if request.auth != null;
}

Index Management

This section describes how to manage indexes in the Oracle Backend with Firebase APIs Console. Indexes are essential for optimizing query performance, enabling advanced filtering, and supporting collection group queries.

Creating and Dropping Indexes

Indexes allow Oracle Backend with Firebase APIs to efficiently retrieve documents based on field values. You can create indexes manually or rely on automatic indexing. Indexes can be single-field, multi-field, or composite.

Indexes have the following types:

Console Steps: Creating Indexes:

  1. Go to the Index Management section of your project.

  2. Click Create Index.

  3. Choose the collection or collection group.

  4. Select fields to index.

  5. Specify index type (single/composite).

  6. Click Create.

Console Steps: Dropping Indexes:

  1. Navigate to the Index Management tab.

  2. Locate the index you want to remove.

  3. Click Delete.

  4. Confirm deletion.

Enabling/Disabling Automatic Indexing

Oracle Backend with Firebase APIs supports automatic indexing of fields during document creation. This can be toggled on or off depending on performance and control requirements.

Console Steps:

  1. Open the Index Settings panel.

  2. Locate the Automatic Indexing toggle.

  3. Switch to Enable or Disable.

  4. Save changes.

Note:

Collection Group Index Setup

To query across subcollections with the same name, a collection group index must be created. This enables cross-document hierarchy queries.

Console Steps:

  1. Go to Index Management.

  2. Click Create Collection Group Index.

  3. Enter the subcollection name (for example, recipes).

  4. Define fields to index.

  5. Click Create.

Security Rule Requirement:

Each collection group must have a corresponding security rule. Example:

match /.*/recipes {
  allow read: if request.auth != null && resource.data.difficulty == "Easy";
}