建立資料索引
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Firebase 可讓您使用任意子項鍵,對資料執行臨時查詢。如果您事先知道索引為何,可以透過 Firebase 即時資料庫安全性規則中的 .indexOn
規則定義索引,提升查詢效能。
定義資料索引
Firebase 提供強大的工具,可排序及查詢資料。具體來說,Firebase 可讓您使用任何常見的子項鍵,對節點集合執行臨時查詢。隨著應用程式成長,這項查詢的效能會降低。不過,如果您將要查詢的鍵告知 Firebase,Firebase 就會在伺服器上為這些鍵建立索引,進而提升查詢效能。
使用 orderByChild 建立索引
最簡單的說明方式就是舉例。Firebase 團隊一致認為恐龍很酷。以下是恐龍事實範例資料庫的程式碼片段。我們會用這個範例說明 .indexOn
如何與 orderByChild()
搭配運作。
{
"lambeosaurus": {
"height" : 2.1,
"length" : 12.5,
"weight": 5000
},
"stegosaurus": {
"height" : 4,
"length" : 9,
"weight" : 2500
}
}
假設在我們的應用程式中,我們經常需要依名稱、高度和長度排序恐龍,但從不需要依重量排序。我們可以將這項資訊提供給 Firebase,藉此提升查詢效能。由於恐龍名稱只是鍵,Firebase 會根據記錄的鍵 (也就是恐龍名稱) 進行查詢,因此已針對查詢進行最佳化。我們可以使用 .indexOn
,告訴 Firebase 也要針對高度和長度最佳化查詢:
{
"rules": {
"dinosaurs": {
".indexOn": ["height", "length"]
}
}
}
與其他規則一樣,您可以在規則中的任何層級指定 .indexOn
規則。
我們將其放在上述範例的根層級,因為所有恐龍資料都儲存在資料庫的根層級。
使用 orderByValue 建立索引
在這個範例中,我們將示範 .indexOn
如何與 orderByValue()
搭配運作。
假設我們正在製作恐龍運動分數排行榜,並使用下列資料:
{
"scores": {
"bruhathkayosaurus" : 55,
"lambeosaurus" : 21,
"linhenykus" : 80,
"pterodactyl" : 93,
"stegosaurus" : 5,
"triceratops" : 22
}
}
由於我們使用 orderByValue() 建立排行榜,因此可以在 /scores
節點新增 .value
規則,藉此最佳化查詢:
{
"rules": {
"scores": {
".indexOn": ".value"
}
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nFirebase allows you to do ad-hoc queries on your data using an arbitrary child key. If you know in advance what your indexes will be, you can define them via the `.indexOn` rule in your Firebase Realtime Database Security Rules to improve query performance.\n\nDefining Data Indexes\n\nFirebase provides powerful tools for\n[ordering](/docs/database/web/lists-of-data#sorting_and_filtering_data)\nand [querying](/docs/database/web/lists-of-data#data-order) your data.\nSpecifically, Firebase allows you to do ad-hoc queries on a collection of nodes using any\ncommon child key. As your app grows, the performance of this query degrades. However, if you\ntell Firebase about the keys you will be querying, Firebase will index those keys at the servers, improving\nthe performance of your queries.\n| A node's key is indexed automatically, so there is no need to index it explicitly.\n\nIndexing with orderByChild\n\nThe easiest way to explain this is through an example. All of us at Firebase agree that\ndinosaurs are pretty cool. Here's a snippet from a sample database of dinosaur facts. We\nwill use it to explain how `.indexOn` works with `orderByChild()`. \n\n```text\n{\n \"lambeosaurus\": {\n \"height\" : 2.1,\n \"length\" : 12.5,\n \"weight\": 5000\n },\n \"stegosaurus\": {\n \"height\" : 4,\n \"length\" : 9,\n \"weight\" : 2500\n }\n}\n```\n\nLet's imagine that in our app, we often need to order the dinosaurs by name, height, and\nlength, but never by weight. We can improve the performance of our queries by telling Firebase\nthis information. Since the name of the dinosaurs are just the keys, Firebase already\noptimizes for queries by dinosaur name, since this is the key of the record.\nWe can use `.indexOn` to tell Firebase to optimize queries for height and length as well: \n\n```text\n{\n \"rules\": {\n \"dinosaurs\": {\n \".indexOn\": [\"height\", \"length\"]\n }\n }\n}\n```\n\nLike other rules, you can specify an `.indexOn` rule at any level in your rules.\nWe placed it at the root level for the example above because all the dinosaur data is stored\nat the root of the database.\n\nIndexing with orderByValue\n\nIn this example, we'll demonstrate how `.indexOn` works with `orderByValue()`.\nLet's say we're making a leaderboard of dino sports scores with the following data: \n\n```text\n{\n \"scores\": {\n \"bruhathkayosaurus\" : 55,\n \"lambeosaurus\" : 21,\n \"linhenykus\" : 80,\n \"pterodactyl\" : 93,\n \"stegosaurus\" : 5,\n \"triceratops\" : 22\n }\n}\n```\n\nSince we're using orderByValue() to create the leaderboard, we can optimize our queries by adding a `.value` rule at our `/scores` node: \n\n```text\n{\n \"rules\": {\n \"scores\": {\n \".indexOn\": \".value\"\n }\n }\n}\n```\n| **Indexes are not required for development** :\n|\n|\n| Indexes are not required for development unless you are using the REST API. The realtime\n| client libraries can execute ad-hoc queries without specifying indexes. Performance will\n| degrade as the data you query grows, so it is important to add indexes before you launch\n| your app if you anticipate querying a large set of data."]]