0Pricing
Firebase Auth & Realtime Database Apps · Pelajaran

Mengindeks Kueri untuk Performa

Percepat kueri Realtime Database dan pastikan sesuai aturan dengan mendeklarasikan indeks menggunakan .indexOn, memahami pentingnya indeks, serta menghindari peringatan kueri tanpa indeks.

Mengindeks Kueri untuk Performa adalah pelajaran Firebase Auth & Realtime Database Apps gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Firebase Auth & Realtime Database Apps, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Firebase Auth & Realtime Database Apps mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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 orderByChild sorts on the client and warns
  • Declare indexes with .indexOn in Security Rules
  • Match the indexed field to your query field
  • Use .key / .value for key and value ordering
  • Index inside wildcards for dynamic parents

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Mengindeks Kueri untuk Performa” gratis?

Ya — teks lengkap “Mengindeks Kueri untuk Performa” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Firebase Auth & Realtime Database Apps, upgrade ke CoddyKit PRO. Kursus Firebase Auth & Realtime Database Apps mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Mengindeks Kueri untuk Performa”?

Percepat kueri Realtime Database dan pastikan sesuai aturan dengan mendeklarasikan indeks menggunakan .indexOn, memahami pentingnya indeks, serta menghindari peringatan kueri tanpa indeks. Kamu berlatih Firebase Auth & Realtime Database Apps dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Firebase Auth & Realtime Database Apps?

Tidak diperlukan pengalaman sebelumnya. Firebase Auth & Realtime Database Apps di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Mengindeks Kueri untuk Performa” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Firebase Auth & Realtime Database Apps ini?

Ya. Setiap pelajaran Firebase Auth & Realtime Database Apps menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Kueri Data Dasar
  2. Memfilter & Mengurutkan Data
  3. Teknik Paginasi Data
  4. Mengindeks Kueri untuk Performa
← Kembali ke Firebase Auth & Realtime Database Apps