หน้านี้มีตัวอย่างกลยุทธ์การจัดทำดัชนีที่ควรใช้กับคำค้นหาที่มีตัวกรองช่วงและความไม่เท่ากันในช่องหลายช่องเพื่อสร้างประสบการณ์การค้นหาที่มีประสิทธิภาพ
อ่านแนวคิดที่เกี่ยวข้องก่อนที่จะเพิ่มประสิทธิภาพข้อความค้นหา
เพิ่มประสิทธิภาพการค้นหาด้วยคำอธิบายคำค้นหา
หากต้องการทราบว่าคำค้นหาและดัชนีที่ใช้นั้นมีประสิทธิภาพสูงสุดหรือไม่ คุณสามารถใช้คำอธิบายการค้นหาเพื่อรับข้อมูลสรุปแผนการค้นหาและสถิติการดำเนินการของคำค้นหา
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" } } }
เราอนุมานจากความเชี่ยวชาญด้านโดเมนได้ว่าพนักงานส่วนใหญ่จะมีประสบการณ์อย่างน้อย 1 คน แต่มีเพียงไม่กี่คนที่มีเงินเดือนที่มากกว่า 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" } } }
ขั้นต่อไปคืออะไร
- เรียนรู้เกี่ยวกับคำอธิบายข้อความค้นหา
- ดูข้อมูลเกี่ยวกับแนวทางปฏิบัติแนะนำในการจัดทำดัชนี