एक से ज़्यादा फ़ील्ड पर, रेंज और इनक्वलिटी फ़िल्टर की मदद से क्वेरी ऑप्टिमाइज़ करें

इस पेज पर इंडेक्सिंग की रणनीति के उदाहरण दिए गए हैं. इनका इस्तेमाल, कई फ़ील्ड पर रेंज और असमानता वाले फ़िल्टर के साथ क्वेरी के लिए किया जा सकता है. इससे क्वेरी का बेहतर अनुभव मिलता है.

अपनी क्वेरी को ऑप्टिमाइज़ करने से पहले, इससे जुड़े कॉन्सेप्ट के बारे में पढ़ें.

क्वेरी की व्याख्या करने वाली सुविधा की मदद से क्वेरी ऑप्टिमाइज़ करना

यह पता लगाने के लिए कि आपकी क्वेरी और इंडेक्स ऑप्टिमल हैं या नहीं, क्वेरी की व्याख्या करें सुविधा का इस्तेमाल करें. इससे आपको क्वेरी प्लान की खास जानकारी और क्वेरी के एक्ज़ीक्यूशन के आंकड़े मिलेंगे:

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 इंडेक्स एंट्री स्कैन करती है. क्वेरी प्रेडिकेट की शर्त पूरी नहीं हुई है. इसलिए, इंडेक्स की कई एंट्री पढ़ी जाती हैं, लेकिन उन्हें फ़िल्टर कर दिया जाता है.

// 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,00,000 से ज़्यादा होगी. इस अहम जानकारी से पता चलता है कि experience की तुलना में salary ज़्यादा चुनिंदा है. क्वेरी को पूरा करने के लिए, Cloud Firestore जिस इंडेक्स का इस्तेमाल करता है उस पर असर डालने के लिए, orderBy क्लॉज़ तय करें. इसमें experience कंस्ट्रेंट से पहले salary कंस्ट्रेंट को क्रम से लगाएं.

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"
        }
    }
}

आगे क्या करना है