複数のフィールドに対する範囲フィルタと不等式フィルタを使用してクエリを最適化する
このページでは、効率的なクエリ エクスペリエンスを実現するために、複数のフィールドに対して範囲フィルタと不等式フィルタを使用したクエリに対して使用すべきインデックス戦略の例を紹介します。
クエリを最適化する前に、関連するコンセプトをお読みください。
クエリ Explain を使用してクエリを最適化する
使用されるクエリとインデックスが最適であるかどうかを判断するには、クエリ Explain を使用して、クエリプランの概要とクエリの実行統計情報を取得します。
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);
次の例は、正しいインデックスの順序指定を使用することで、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
制約よりも選択的であると考えることができます。Firestore がクエリの実行に使用するインデックスに影響を与えるには、experience
制約の前に salary
制約を順序付ける orderBy
句を指定します。
Java
db.collection("employees")
.whereGreaterThan("salary", 100000)
.whereGreaterThan("experience", 0)
.orderBy("salary")
.orderBy("experience");
orderBy()
句を明示的に使用して述語を追加すると、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" } } }
次のステップ
- クエリ Explainについて確認する。
- ベスト プラクティスのインデックス登録を学習します。