فهرسة الاستعلامات لتحسين الأداء
اجعل استعلامات Realtime Database سريعة ومتوافقة مع القواعد عبر تعريف الفهارس باستخدام .indexOn، وفهم أهميتها، وتجنب تحذير الاستعلام غير المفهرس.
فهرسة الاستعلامات لتحسين الأداء درس مجاني في Firebase Auth & Realtime Database Apps على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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
orderByChildsorts on the client and warns - Declare indexes with
.indexOnin Security Rules - Match the indexed field to your query field
- Use
.key/.valuefor key and value ordering - Index inside wildcards for dynamic parents
تعلم Firebase Auth & Realtime Database Apps مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 11
- الدروس
- 44
الأسئلة الشائعة
هل درس «فهرسة الاستعلامات لتحسين الأداء» مجاني؟
نعم — نص درس «فهرسة الاستعلامات لتحسين الأداء» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Firebase Auth & Realtime Database Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Firebase Auth & Realtime Database Apps 4 دروس في المجموع.
ماذا ستتعلم في «فهرسة الاستعلامات لتحسين الأداء»؟
اجعل استعلامات Realtime Database سريعة ومتوافقة مع القواعد عبر تعريف الفهارس باستخدام .indexOn، وفهم أهميتها، وتجنب تحذير الاستعلام غير المفهرس. تتمرن على Firebase Auth & Realtime Database Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Firebase Auth & Realtime Database Apps؟
لا تُشترط خبرة سابقة. Firebase Auth & Realtime Database Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «فهرسة الاستعلامات لتحسين الأداء»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Firebase Auth & Realtime Database Apps هذا؟
نعم. كل درس في Firebase Auth & Realtime Database Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- استعلامات البيانات الأساسية
- تصفية البيانات وترتيبها
- تقنيات تقسيم الصفحات للبيانات
- فهرسة الاستعلامات لتحسين الأداء