تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تتيح لك Firebase إجراء طلبات بحث مخصّصة على بياناتك باستخدام مفتاح فرعي عشوائي. إذا كنت تعرف مسبقًا الفهارس التي ستستخدمها، يمكنك تحديدها من خلال قاعدة .indexOn في "قواعد الأمان" في "قاعدة بيانات Firebase في الوقت الفعلي" لتحسين أداء طلبات البحث.
تحديد فهارس البيانات
توفّر Firebase أدوات فعّالة
لترتيبوالبحث في بياناتك.
على وجه التحديد، تتيح لك Firebase إجراء طلبات مخصّصة على مجموعة من العُقد باستخدام أي مفتاح فرعي مشترك. ومع زيادة حجم تطبيقك، يتراجع أداء هذا الطلب. ومع ذلك، إذا أبلغت Firebase بالمفاتيح التي ستُجري بحثًا عنها، سيفهرس Firebase هذه المفاتيح على الخوادم، ما يؤدي إلى تحسين أداء طلبات البحث.
الفهرسة باستخدام orderByChild
أسهل طريقة لتوضيح ذلك هي من خلال مثال. نتفق جميعًا في Firebase على أنّ الديناصورات رائعة. في ما يلي مقتطف من قاعدة بيانات نموذجية تتضمّن معلومات عن الديناصورات. سنستخدمها لتوضيح طريقة عمل .indexOn مع orderByChild().
لنفترض أنّنا نحتاج في تطبيقنا إلى ترتيب الديناصورات حسب الاسم والارتفاع والطول، ولكن ليس حسب الوزن. يمكننا تحسين أداء طلبات البحث من خلال إخبار Firebase بهذه المعلومات. بما أنّ أسماء الديناصورات هي المفاتيح فقط، تعمل Firebase تلقائيًا على تحسين عمليات البحث حسب اسم الديناصور، لأنّ هذا هو مفتاح السجلّ.
يمكننا استخدام .indexOn لإخبار Firebase بتحسين طلبات البحث عن الارتفاع والطول أيضًا:
كما هو الحال مع القواعد الأخرى، يمكنك تحديد قاعدة .indexOn على أي مستوى في قواعدك.
لقد وضعناه في المستوى الجذر للمثال أعلاه لأنّ جميع بيانات الديناصورات مخزّنة في جذر قاعدة البيانات.
الفهرسة باستخدام orderByValue
في هذا المثال، سنوضّح كيفية عمل .indexOn مع orderByValue().
لنفترض أنّنا ننشئ قائمة بأفضل النتائج في مباريات الديناصورات الرياضية باستخدام البيانات التالية:
تاريخ التعديل الأخير: 2025-07-25 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-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."]]