0Pricing
Firebase Auth & Realtime Database Apps · Lekcja

Indeksowanie zapytań na potrzeby wydajności

Przyspiesz zapytania Realtime Database i zapewnij ich zgodność z regułami, deklarując indeksy za pomocą .indexOn, rozumiejąc ich znaczenie i unikając ostrzeżeń o zapytaniach bez indeksu.

Indeksowanie zapytań na potrzeby wydajności to bezpłatna lekcja Firebase Auth & Realtime Database Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Firebase Auth & Realtime Database Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Firebase Auth & Realtime Database Apps zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Indeksowanie zapytań na potrzeby wydajności” jest bezpłatna?

Tak — pełny tekst „Indeksowanie zapytań na potrzeby wydajności” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Firebase Auth & Realtime Database Apps, przejdź na CoddyKit PRO. Kurs Firebase Auth & Realtime Database Apps zawiera 4 lekcji w sumie.

Co nauczysz się w „Indeksowanie zapytań na potrzeby wydajności”?

Przyspiesz zapytania Realtime Database i zapewnij ich zgodność z regułami, deklarując indeksy za pomocą .indexOn, rozumiejąc ich znaczenie i unikając ostrzeżeń o zapytaniach bez indeksu. Ćwiczysz Firebase Auth & Realtime Database Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Firebase Auth & Realtime Database Apps?

Nie wymagamy żadnego doświadczenia. Firebase Auth & Realtime Database Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Indeksowanie zapytań na potrzeby wydajności”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Firebase Auth & Realtime Database Apps?

Tak. Każda lekcja Firebase Auth & Realtime Database Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Podstawowe zapytania dotyczące danych
  2. Filtrowanie i sortowanie danych
  3. Techniki stronicowania danych
  4. Indeksowanie zapytań na potrzeby wydajności
← Powrót do Firebase Auth & Realtime Database Apps