[null,null,["最后更新时间 (UTC):2025-08-12。"],[],[],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."]]