หน้านี้มีตัวอย่างกลยุทธ์การจัดทำดัชนีที่คุณใช้สำหรับการค้นหาได้ โดยใช้ตัวกรองช่วงและอสมการในหลายๆ ช่องเพื่อสร้าง ประสบการณ์การค้นหา
ก่อนที่จะเพิ่มประสิทธิภาพให้กับคำค้นหา โปรดอ่านแนวคิดที่เกี่ยวข้อง
เพิ่มประสิทธิภาพการค้นหาด้วยคำอธิบายคำค้นหา
หากต้องการพิจารณาว่าการค้นหาและดัชนีของคุณมีประสิทธิภาพสูงสุดหรือไม่ คุณสามารถใช้ข้อความค้นหา อธิบายเพื่อดูข้อมูลสรุปแผนการค้นหาและสถิติการดำเนินการ ของข้อความค้นหา:
Java
Query q = db.collection("employees").whereGreaterThan("salary",
100000).whereGreaterThan("experience", 0);
ExplainResults<QuerySnapshot> explainResults = q.explain(ExplainOptions.builder().analyze(true).build()).get();
ExplainMetrics metrics = explainResults.getMetrics();
PlanSummary planSummary = metrics.getPlanSummary();
ExecutionStats executionStats = metrics.getExecutionStats();
System.out.println(planSummary.getIndexesUsed());
System.out.println(stats.getResultsReturned());
System.out.println(stats.getExecutionDuration());
System.out.println(stats.getReadOperations());
System.out.println(stats.getDebugStats());
Node.js
let q = db.collection("employees")
.where("salary", ">", 100000)
.where("experience", ">",0);
let options = { analyze : 'true' };
let explainResults = await q.explain(options);
let planSummary = explainResults.metrics.planSummary;
let stats = explainResults.metrics.executionStats;
console.log(planSummary);
console.log(stats);
ตัวอย่างต่อไปนี้แสดงให้เห็นว่าการใช้การจัดลำดับดัชนีที่ถูกต้องช่วยลด จำนวนรายการดัชนีที่ Cloud Firestore สแกน
คำค้นหาแบบง่าย
ในตัวอย่างก่อนหน้านี้ เกี่ยวกับคอลเล็กชันพนักงาน ข้อความค้นหาที่เรียบง่าย
ที่เรียกใช้ด้วยดัชนี (experience ASC, salary ASC)
ดังนี้
Java
db.collection("employees")
.whereGreaterThan("salary", 100000)
.whereGreaterThan("experience", 0)
.orderBy("experience")
.orderBy("salary");
การค้นหาจะสแกนรายการดัชนี 95,000 รายการเพื่อแสดงผลเอกสารเพียง 5 รายการ เนื่องจากข้อความค้นหา ภาคแสดงไม่ถูกต้อง มีรายการดัชนีจำนวนมากที่ถูกอ่าน แต่กลับถูกอ่าน ถูกกรองออก
// Output query planning info { "indexesUsed": [ { "properties": "(experience ASC, salary ASC, __name__ ASC)", "query_scope": "Collection" } ], // Output Query Execution Stats "resultsReturned": "5", "executionDuration": "2.5s", "readOperations": "100", "debugStats": { "index_entries_scanned": "95000", "documents_scanned": "5", "billing_details": { "documents_billable": "5", "index_entries_billable": "95000", "small_ops": "0", "min_query_cost": "0" } } }
คุณสามารถอนุมานจากความเชี่ยวชาญในโดเมนได้ว่าพนักงานส่วนใหญ่จะมีประสบการณ์บ้าง แต่มีเพียงไม่กี่คนที่จะมีเงินเดือนมากกว่า 100,000 จากข้อมูลเชิงลึกนี้ คุณจะเห็นว่าข้อจำกัด salary
เลือกมากกว่าข้อจำกัด experience
เพื่อกำหนดดัชนีที่ Cloud Firestore ใช้เพื่อ
ดำเนินการค้นหา ระบุอนุประโยค orderBy
ที่สั่งซื้อ salary
ก่อนข้อจำกัด experience
Java
db.collection("employees")
.whereGreaterThan("salary", 100000)
.whereGreaterThan("experience", 0)
.orderBy("salary")
.orderBy("experience");
หากคุณใช้วลี orderBy()
เพื่อเพิ่มคำกริยาอย่างชัดเจน
Cloud Firestore ใช้ดัชนี (salary ASC, experience ASC)
ในการเรียกใช้การค้นหา
เนื่องจากการเลือกตัวกรองของช่วงแรกสูงกว่าในคำค้นหานี้
เมื่อเทียบกับการค้นหาก่อนหน้า การค้นหาจะทำงานเร็วกว่าและประหยัดต้นทุนมากกว่า
// Output query planning info { "indexesUsed": [ { "properties": "(salary ASC, experience ASC, __name__ ASC)", "query_scope": "Collection" } ], // Output Query Execution Stats "resultsReturned": "5", "executionDuration": "0.2s", "readOperations": "6", "debugStats": { "index_entries_scanned": "1000", "documents_scanned": "5", "billing_details": { "documents_billable": "5", "index_entries_billable": "1000", "small_ops": "0", "min_query_cost": "0" } } }
ขั้นตอนถัดไป
- เรียนรู้เกี่ยวกับคำอธิบายข้อความค้นหา
- ดูข้อมูลเกี่ยวกับแนวทางปฏิบัติแนะนำในการจัดทำดัชนี