การทำดัชนีคำค้นเพื่อประสิทธิภาพ
ทำให้คำค้นของ Realtime Database รวดเร็วและสอดคล้องกับกฎ ด้วยการประกาศดัชนีด้วย .indexOn ทำความเข้าใจว่าเหตุใดดัชนีจึงสำคัญ และหลีกเลี่ยงคำเตือนเรื่องคำค้นที่ไม่มีดัชนี
การทำดัชนีคำค้นเพื่อประสิทธิภาพ เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 11
- บทเรียน
- 44
คำถามที่พบบ่อย
บทเรียน “การทำดัชนีคำค้นเพื่อประสิทธิภาพ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทำดัชนีคำค้นเพื่อประสิทธิภาพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทำดัชนีคำค้นเพื่อประสิทธิภาพ”
ทำให้คำค้นของ Realtime Database รวดเร็วและสอดคล้องกับกฎ ด้วยการประกาศดัชนีด้วย .indexOn ทำความเข้าใจว่าเหตุใดดัชนีจึงสำคัญ และหลีกเลี่ยงคำเตือนเรื่องคำค้นที่ไม่มีดัชนี คุณปฏิบัติ Firebase Auth & Realtime Database Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คำค้นข้อมูลพื้นฐาน
- การกรองและการจัดลำดับข้อมูล
- เทคนิคการแบ่งหน้าแสดงข้อมูล
- การทำดัชนีคำค้นเพื่อประสิทธิภาพ