Repository navigation
結合で index を使う - #32
Merged
Merged
Conversation
…dex files under their join values 左の行ごとにその結合値で index を検索し、結合が残す右の行だけを読む。 積のように左の行ごとに右を全部読み、条件を上の select に任せることをしない。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ndex on the table 左の行ごとに右のテーブルの index を検索する結合の計画。 コストは、左を 1 回読むブロックに、左の行ごとの index の検索と、返る組ごとのテーブルの 1 ブロックを足して見積もる。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the where clause lets it be where 句が後から足すテーブルの index のフィールドを、左のフィールドと等しいとするなら、積の代わりに index で結合する。 右が定数の index でも読めるときは、積と結合のコストを比べて安い方を選ぶ。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…it adds 結合の index は plan で実装済みで、ここでは server が開く DB で、SQL の結合が index を通して答えられることを確かめる。 問い合わせの planner を BasicQueryPlanner に戻すと落ちることを確認した。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 句が 2 つのテーブルのフィールドを等しいとするとき(from other, test where oid = idなど)、積を作ってから条件を確かめる代わりに、左の行ごとに右のテーブルの index を検索して、合う行だけを読むようにした。SimpleDB のIndexJoinScan/IndexJoinPlanに相当するものを追加し、IndexQueryPlannerが結合にも index を使う。これまでの結合は
ProductPlanだけで、左の行ごとに右のテーブルを最初から全部読み直していた。index で結合すると、右のテーブルを全部読む代わりに、左の行ごとに index を 1 回検索して、合う行だけを読む。indexJoinScan(query.Scanの実装):左の行ごとに、その行の結合フィールドの値で index を検索し、右のテーブルのスキャンを返った record id の行へ動かす。値は、右にあるフィールドなら右から、なければ左から読むplanIndexJoinPlan:Openでindex.JoinScanを返すIndexQueryPlanner:from 句の次のテーブルを足すとき、where 句がそのテーブルの index のあるフィールドを左のフィールドと等しいとするなら、ProductPlanの代わりにIndexJoinPlanを使うserver はすでに
IndexQueryPlannerを使っているので、server の変更はない。設計上の判断
IndexJoinPlan.BlocksAccessedは「左を 1 回読む + 左の行数 × index の 1 回の検索 + 返る組の数」。 最後の項は、返る組ごとに右のテーブルの行を読む分で、IndexSelectPlanと同じく、1 組につき 1 ブロックとみなす。返る組の数は「左の行数 × 右のテーブルで 1 つの値を持つ行の数の平均」(SimpleDB と同じ)。BlocksAccessedの小さい方を選び、同じなら結合の index を選ぶ(cheaperJoin)。定数の index が使えないときは、使える結合の index をいつも使う(問い合わせで index を使う #31 の「使える index はいつも使う」と同じ)。from a, b where a.x = b.yで使うのは b.y の index だけで、a.x の index は使わない。順番の入れ替えは、コストを比べる planner の仕事として残す。name = 'name1'など)を確かめるため。既知の制限
UPDATE/DELETEの where 句には、まだ index を使わない。BasicQueryPlannerとIndexQueryPlannerの、ビューを展開する部分はほぼ同じで、2 か所に置いたまま(問い合わせで index を使う #31 と同じ)。テスト
index.JoinScan:左の値ごとに右の行と組になること、右に合う行がない左の行を飛ばすこと、左の 1 行に右の 2 行が合う場合、何も合わない場合、左が空の場合、左の順番で返ること、MoveBeforeFirstRecordでの読み直し、HasFieldIndexJoinPlan:Openで読める組(右にない値を飛ばす場合、左が空の場合を含む)、コスト(3 行と 20 行のテーブルの統計から手で計算した値)、DistinctValues、スキーマDistinctValuesで左を見ない、Openで右を読まない)、それぞれ対応するアサーションが落ちることを確かめたIndexQueryPlanner:結合の index を使うか(使うなら左のどのフィールドで結合するか)と、読める組の両方を確かめる。from 句の順番、等号の左右、index の有無、index のないフィールド、定数の index と結合の index の両方が使える場合cheaperJoin:テスト用の DB ではどのフィールドの統計も同じ式で見積もられ、2 つの計画のコストがほとんど同じになるので、コストを決められる fake の計画で、積の方が安いときに積を選ぶことを確かめたserver:planner を通さずに右のテーブルへ直接書いた行(index には入らない)が、SQL の結合で組にならないこと。問い合わせの planner をBasicQueryPlannerに戻すと落ちることを確かめた🤖 Generated with Claude Code