Firebase는 데이터에 대해 임의의 하위 키를 사용하는 임시 쿼리를 허용합니다. 색인을 미리 계획할 수 있으면 Firebase 실시간 데이터베이스 보안 규칙에서 .indexOn 규칙을 통해 색인을 정의하여 쿼리 성능을 높일 수 있습니다.
데이터 색인 정의
Firebase는 데이터 정렬 및 쿼리를 위한 강력한 도구를 제공합니다.
특히 Firebase는 노드 컬렉션에 대해 임의의 공통 하위 키를 사용한 즉석 쿼리를 허용합니다. 이 쿼리는 앱의 규모가 커질수록 성능이 저하됩니다. 그러나 쿼리 대상 키를 Firebase에 미리 알리면 Firebase가 서버에서 이 키를 색인화하여 쿼리 성능이 향상됩니다.
orderByChild로 색인 생성
예제를 통해 알기 쉽게 설명해 드리겠습니다. Firebase 개발팀 모두는 공룡을 매우
좋아해서 공룡을 예로 들어보겠습니다. 다음은 공룡 자료 샘플 데이터베이스의 스니펫입니다. 이 스니펫을 사용하여 .indexOn와 orderByChild()를 함께 사용하는 방법을 설명하려 합니다.
앱에서 공룡을 이름, 키, 길이 순으로 자주 정렬하지만 무게로는 정렬하지 않는다고 가정하겠습니다. Firebase에 이 정보를 미리 알리면 쿼리의 성능을 높일 수
있습니다. 공룡의 이름 자체가 레코드의 키이므로 Firebase에서 이미 공룡 이름으로 쿼리가 최적화된 상태입니다.
.indexOn을 사용하여 Firebase에 높이 및 길이에 대한 쿼리를 최적화하도록 지시할 수도 있습니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-03(UTC)"],[],[],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."]]