0Pricing
Firebase Auth & Realtime Database Apps · 课时

为查询建立索引以提升性能

通过声明 .indexOn 索引、了解索引的重要性并避免未建立索引的查询警告,让 Realtime Database 查询既快速又符合规则要求。

为查询建立索引以提升性能 是 CoddyKit 上的免费 Firebase Auth & Realtime Database Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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

常见问题解答

「为查询建立索引以提升性能」课时是免费的吗?

是的 — 「为查询建立索引以提升性能」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Firebase Auth & Realtime Database Apps 课程的其余内容,请升级到 CoddyKit PRO。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。

「为查询建立索引以提升性能」这节课中我会学到什么?

通过声明 .indexOn 索引、了解索引的重要性并避免未建立索引的查询警告,让 Realtime Database 查询既快速又符合规则要求。 你通过在浏览器中直接运行的动手代码来练习 Firebase Auth & Realtime Database Apps,全天候 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