เพิ่มประสิทธิภาพการค้นหาด้วยตัวกรองช่วงและความไม่เท่ากันในช่องข้อมูลหลายช่อง

หน้านี้แสดงตัวอย่างกลยุทธ์การจัดทำดัชนีที่คุณใช้กับการค้นหาได้ โดยมีตัวกรองช่วงและตัวกรองความไม่เท่ากันในหลายฟิลด์เพื่อสร้างประสบการณ์การค้นหาที่มีประสิทธิภาพ

โปรดอ่านเกี่ยวกับแนวคิดที่เกี่ยวข้องก่อนเพิ่มประสิทธิภาพคำค้นหา

เพิ่มประสิทธิภาพการค้นหาด้วยคำอธิบายการค้นหา

หากต้องการดูว่าการค้นหาและดัชนีของคุณมีประสิทธิภาพสูงสุดหรือไม่ คุณสามารถใช้ Query Explain เพื่อดูสรุปแผนการค้นหาและสถิติการดำเนินการ ของการค้นหาได้

JavaNode.js
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());
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() clause อย่างชัดเจนเพื่อเพิ่มเพรดิเคต 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"
        }
    }
}

ขั้นตอนถัดไป