Repository navigation
問い合わせで index を使う - #31
Merged
Merged
Conversation
index を検索するには、述語が「フィールド = 定数」とする定数そのものが要る。 既存の equatesWithConstant もこの関数を使う形にまとめ、項のたどり方を 1 か所にした。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
index が返す record id へテーブルのスキャンを動かし、間の行を読まずに目的の行だけを読む。 行の値は index ではなくテーブルから読む。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
述語が index のフィールドを定数と等しいとするとき、テーブルを読む代わりに index で目的の行だけを読む計画。 コストは index を読むブロックに、返す行ごとのテーブルの 1 ブロックを足して見積もる。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…re clause lets it be read through where 句が index のフィールドを定数と等しいとするテーブルは、index で目的の行だけを読む。 複数の index が使えるときはコストの小さい方、同じならフィールド名が先の方を選び、カタログの並びに左右されないようにした。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…heir where clauses let them be BasicQueryPlanner は index があってもテーブルを全部読む。 server が開く DB は IndexQueryPlanner を通し、更新だけでなく問い合わせでも index を使う。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
selectの where 句が index のあるフィールドを定数と等しいとするとき、テーブルを全部読む代わりに、index でその値の行だけを読むようにした。SimpleDB のIndexSelectScan/IndexSelectPlanに相当するものを追加し、server が開く DB は、index を使う問い合わせの planner(IndexQueryPlanner)を使う。前の PR(#30)で、index は文の実行に合わせて保たれるようになった。この PR で、保たれた index が初めて問い合わせの答えに使われる。
indexSelectScan(query.Scanの実装):index で record id を得て、テーブルのスキャンをその行へ動かす。行の値はテーブルから読むplanIndexSelectPlan:Openでindex.SelectScanを返す。コストはIndexInfoから見積もるIndexQueryPlanner(QueryPlannerの実装):BasicQueryPlannerと同じく、テーブルの積、select、project の順に組む。ただし、where 句が index のあるフィールドを定数と等しいとするテーブルは、TablePlanの代わりにIndexSelectPlanで読むconstantEquatedWith:述語から「フィールド = 定数」の定数を取り出す。既存のequatesWithConstantもこれを使う形にまとめたserverOpenが planner に渡す問い合わせの planner を、BasicQueryPlannerからIndexQueryPlannerに変えた設計上の判断
SelectScanはindexパッケージに置く。indexはすでにqueryを import しているので、queryに置くと循環する。SimpleDB のsimpledb.index.queryに対応する。index.IndexSelectScanだと名前が重なるので、SelectScanとした。BasicQueryPlannerは残し、IndexQueryPlannerを別に作る。BasicQueryPlannerは「何も比べない基準」という位置づけなので、そのままにした。TablePlannerと同じ。IndexSelectPlanとTablePlanのコストを比べることはしない。小さいテーブルでは表を読む方が安いことが多いが、どの計画になるかを予想しやすい方を選んだ。コストを比べるのは、結合の順番を考える planner を作るときにまとめて入れる。id = 7 and name = 'x'のname = 'x'など)を確かめるのに必要。IndexSelectPlan.BlocksAccessedは、index を読むブロックに、返す行ごとのテーブルの 1 ブロックを足す。 同じ値の行は隣り合っているとは限らないので、1 行ごとに別のブロックを読むとみなす(SimpleDB と同じ)。既知の制限
where a = bの形)にはまだ index を使わない。次の PR で、IndexJoinScan/IndexJoinPlanを入れる予定。UPDATE/DELETEの where 句にはまだ index を使わない。今はテーブルを全部読んで、述語が残す行を探す。</>)には使わない。Indexインターフェースが等号の検索しか持たないため。BasicQueryPlannerとIndexQueryPlannerの、ビューを展開する部分はほぼ同じ。ビューの中も同じ planner で計画するため、共通にするには planner を引数に取る形になるので、今は 2 か所に置いている。テスト
constantEquatedWith:定数が右・左にある場合、別の項と組み合わさった場合、同じフィールドの項が 2 つある場合(最初の項を取る)、フィールド同士の項・別のフィールドの項・項なしindex.SelectScan:先頭・途中・末尾の値、同じ値の 2 行、どの行にもない値、MoveBeforeFirstRecordでの読み直し、HasFieldIndexSelectPlan:Openで読める行、コスト(10 行のテーブルの統計から手で計算した値)、スキーマIndexQueryPlanner:計画が index を使うか(使うなら何の値で検索するか)と、読める行の両方を確かめる。index の有無、index のないフィールドへの条件、where 句なし、2 つの index から選ぶ場合、index が受け持たない項が残る場合、ビュー、結合、存在しないテーブルserver:planner を通さずにテーブルへ直接書いた行(index には入らない)が、where 句で index を使う問い合わせでは見つからないこと。server のテストからは計画の中身を見られないので、index を通して読んだときにしか起きない結果で確かめている🤖 Generated with Claude Code