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

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

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

क्वेरी की जानकारी की मदद से क्वेरी ऑप्टिमाइज़ करें

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

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 रुपये से ज़्यादा की हो सकती है. इस अहम जानकारी के आधार पर, हम इस नतीजे पर पहुंचते हैं कि 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"
        }
    }
}

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