এই পৃষ্ঠায় এমন কিছু ইনডেক্সিং কৌশলের উদাহরণ দেওয়া হয়েছে, যা ব্যবহার করে একাধিক ফিল্ডে রেঞ্জ ও ইনইকুয়ালিটি ফিল্টারসহ কোয়েরির মাধ্যমে একটি কার্যকর অভিজ্ঞতা তৈরি করা যায়।
আপনার কোয়েরিগুলো অপ্টিমাইজ করার আগে, সংশ্লিষ্ট ধারণাগুলো সম্পর্কে পড়ুন।
Query Explain ব্যবহার করে কোয়েরি অপ্টিমাইজ করুন
আপনার কোয়েরি এবং ইনডেক্সগুলো সর্বোত্তম কিনা তা নির্ধারণ করতে, আপনি কোয়েরি এক্সপ্লেইন (Query Explain) ব্যবহার করে কোয়েরিটির কোয়েরি প্ল্যান সামারি এবং এক্সিকিউশন স্ট্যাটিস্টিকস পেতে পারেন:
জাভা
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) ইনডেক্স ব্যবহার করে যে সাধারণ কোয়েরিটি চলে তা নিম্নরূপ:
জাভা
db.collection("employees")
.whereGreaterThan("salary", 100000)
.whereGreaterThan("experience", 0)
.orderBy("experience")
.orderBy("salary");
কোয়েরিটি ৯৫০০০ ইনডেক্স এন্ট্রি স্ক্যান করে মাত্র পাঁচটি ডকুমেন্ট ফেরত দেয়। যেহেতু কোয়েরি প্রেডিকেটটি সন্তুষ্ট হয় না, তাই বিপুল সংখ্যক ইনডেক্স এন্ট্রি পড়া হলেও সেগুলো ফিল্টার হয়ে যায়।
// 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" } } }
You can infer from domain expertise that most employees will have at least some experience but few will have a salary that is more than 100000. Given this insight, you can see that the salary constraint is more selective than the experience constraint. To influence the index that Cloud Firestore uses to execute the query, specify an orderBy clause that orders the salary constraint before the experience constraint.
জাভা
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" } } }
এরপর কী?
- কোয়েরি এক্সপ্লেইন সম্পর্কে জানুন।
- ইনডেক্সিংয়ের সর্বোত্তম অনুশীলন সম্পর্কে জানুন।