0Pricing
Firebase Auth & Realtime Database Apps · レッスン

パフォーマンスのためのクエリインデックス

.indexOnでインデックスを宣言し、その重要性を理解して、インデックス未設定クエリの警告を避けることで、Realtime Databaseのクエリを高速かつルールに準拠したものにします。

「パフォーマンスのためのクエリインデックス」はCoddyKit上の無料Firebase Auth & Realtime Database Appsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFirebase Auth & Realtime Database Apps学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Firebase Auth & Realtime Database Appsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Indexing Matters

Querying with orderByChild works on small data, but as nodes grow, unindexed queries force the client to download and sort everything. That is slow and expensive.

Indexes tell Firebase to pre-sort data on the server for a given field.

The Unindexed Warning

Run an orderByChild query on a field with no index and Firebase logs a warning: it had to perform the sort on the client. In large datasets this is a real performance problem.

Declaring an Index

Indexes are declared in your Security Rules using the .indexOn directive at the parent of the records you query.

{
  "rules": {
    "users": {
      ".indexOn": ["age"]
    }
  }
}

Matching the Query to the Index

The indexed field must match the field you order by. This query benefits from the age index above.

import { ref, query, orderByChild } from 'firebase/database';

const q = query(ref(db, 'users'), orderByChild('age'));

Indexing Multiple Fields

You can index several fields under the same node by listing them. Each supports a different orderByChild query.

{
  "rules": {
    "products": {
      ".indexOn": ["price", "rating", "createdAt"]
    }
  }
}

Indexing on $key and $value

For queries using orderByKey or orderByValue, use the special tokens .key and .value in the index list.

{
  "rules": {
    "leaderboard": {
      ".indexOn": ".value"
    }
  }
}

Indexing Under Dynamic Paths

When records sit under a dynamic parent (like per-user lists), put .indexOn inside a wildcard segment so it applies to every child group.

{
  "rules": {
    "posts": {
      "$uid": {
        ".indexOn": ["timestamp"]
      }
    }
  }
}

Indexes Are Free to Maintain

Unlike some databases, Realtime Database indexes add no extra storage cost and are maintained automatically. The trade-off is simply that you must declare them in rules ahead of time.

Index vs Data Structure

Indexing speeds queries, but it does not replace good data modeling. If you constantly query by a field, consider also restructuring data so the common access pattern is a direct lookup.

Limitations to Remember

Keep these constraints in mind:

  • You can order by only one field per query
  • The index field must exist on the child for it to appear in results
  • Deeply nested data is harder to index efficiently

Verifying Your Index

After deploying rules, re-run the query and confirm the unindexed warning is gone from the logs. That confirms the server is now doing the sort.

Quick Check

Test your understanding of query indexing.

Recap

Your queries are now ready to scale.

  • Unindexed orderByChild sorts on the client and warns
  • Declare indexes with .indexOn in Security Rules
  • Match the indexed field to your query field
  • Use .key / .value for key and value ordering
  • Index inside wildcards for dynamic parents

よくある質問

「パフォーマンスのためのクエリインデックス」レッスンは無料ですか?

はい。「パフォーマンスのためのクエリインデックス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Firebase Auth & Realtime Database Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Firebase Auth & Realtime Database Appsコースには全4レッスンが含まれています。

「パフォーマンスのためのクエリインデックス」で何を学びますか?

.indexOnでインデックスを宣言し、その重要性を理解して、インデックス未設定クエリの警告を避けることで、Realtime Databaseのクエリを高速かつルールに準拠したものにします。 ブラウザで直接実行するハンズオンコードでFirebase Auth & Realtime Database Appsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Firebase Auth & Realtime Database Appsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFirebase Auth & Realtime Database Appsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「パフォーマンスのためのクエリインデックス」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFirebase Auth & Realtime Database Appsレッスンでコードを書いて実行できますか?

はい。すべてのFirebase Auth & Realtime Database Appsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 基本的なデータクエリ
  2. データのフィルタリングと並べ替え
  3. データのページネーション技法
  4. パフォーマンスのためのクエリインデックス
← Firebase Auth & Realtime Database Appsに戻る