(* Copyright (c) 2026 Thomas Gazagnaire. SPDX-License-Identifier: ISC *) (* A NATURAL or USING join equates the columns it names exactly as the ON [l.c = r.c] would (lang_select.html, section 3), and sqlite3 plans it as that ON (select.c, sqlite3ProcessJoin): on the fixture below, sqlite3 3.54.0 prints [SEARCH a USING INDEX args_set (arg_set_id=?)] for the inner table of each join here, and an automatic index on [arg_set_id] without one. Planned as a plain nested loop instead, a join of tables of [n] and [m] rows steps through [n * m] rows. The fixture is the shape obs writes a trace in: a slice table of [n + 1] rows, and an args table of [2n], indexed on [arg_set_id] or not. Each join is asked against the ON it stands for: the same plan, the same rows and the same work. The work is read off {!Sqlite.Stats.rows_scanned}, sqlite3's SQLITE_STMTSTATUS_FULLSCAN_STEP: a pass over [n] rows is [n - 1] steps, and a seek or a hash probe takes none. A join that reads each table at most once, by an index or by a hash, takes at most [n + (2n - 1)]; the nested loop takes [n + (n + 1) * (2n - 1)]. *) open Sqlite let n = 1000 let fixture ~index = let db = in_memory () in let exec sql = Raw.exec db sql ~params:[] in exec "CREATE TABLE slice (id INTEGER PRIMARY KEY, ts INTEGER, dur INTEGER, \ track_id INTEGER, category TEXT, name TEXT, depth INTEGER, arg_set_id \ INTEGER)"; exec "CREATE TABLE args (arg_set_id INTEGER, key TEXT, int_value INTEGER, \ string_value TEXT, real_value REAL)"; if index then exec "CREATE INDEX args_set ON args(arg_set_id)"; exec "BEGIN"; for i = 1 to n do let category = if i mod 2 = 0 then "probe" else "other" in Raw.exec db "INSERT INTO slice VALUES (?, ?, ?, 1, ?, ?, 0, ?)" ~params: [ Int (Int64.of_int i); Int (Int64.of_int (10 * i)); Int 5L; Text category; Text ("span" ^ string_of_int (i mod 7)); Int (Int64.of_int i); ]; Raw.exec db "INSERT INTO args VALUES (?, 'i', ?, NULL, NULL)" ~params:[ Int (Int64.of_int i); Int (Int64.of_int (i mod 10)) ]; Raw.exec db "INSERT INTO args VALUES (?, 's', NULL, 'x', NULL)" ~params:[ Int (Int64.of_int i) ] done; (* a slice with no args, which a LEFT JOIN pads *) Raw.exec db "INSERT INTO slice (id, arg_set_id) VALUES (?, ?)" ~params:[ Int (Int64.of_int (n + 1)); Int (Int64.of_int (n + 1)) ]; exec "COMMIT"; db let value = Alcotest.testable Value.pp ( = ) let rows = Alcotest.(list (list value)) (* The rows [sql] returns and the full-scan steps it took. *) let run db sql = let before = Stats.rows_scanned db in let r = Raw.query db sql ~params:[] in (r, Stats.rows_scanned db - before) (* One pass over each table: [n + 1] slices and [2n] args. *) let each_once = n + ((2 * n) - 1) (* [joined] is planned as [written], the ON it stands for: one plan, one answer, one cost, which reads each table at most once. *) let same_as_on ~index ~joined ~written ~want () = let db = fixture ~index in Alcotest.(check string) "the plan of the ON" (Raw.explain db written) (Raw.explain db joined); let got, steps = run db joined in let on, on_steps = run db written in Alcotest.check rows "the rows of the ON" on got; Alcotest.(check int) "the answer" want (List.length got); Alcotest.(check int) "the work of the ON" on_steps steps; (* the nested loop steps through [(n + 1) * (2n - 1)] rows of args *) if steps > each_once then Alcotest.failf "%d full-scan steps, past one pass over each table (%d)" steps each_once let select = "SELECT s.name, a.int_value, s.ts, s.dur FROM slice s " let where = " WHERE s.category = 'probe' AND a.key = 'i' AND a.int_value <= 3 ORDER BY \ s.id" (* The query of the trace: the probe slices with [int_value] 0 to 3, which is [i mod 10] in {0, 2}. *) let trace ~index = same_as_on ~index ~joined:(select ^ "JOIN args a USING(arg_set_id)" ^ where) ~written:(select ^ "JOIN args a ON s.arg_set_id = a.arg_set_id" ^ where) ~want:(n / 5) (* The two tables share [arg_set_id] alone. *) let natural ~index = same_as_on ~index ~joined:"SELECT s.id, a.key FROM slice s NATURAL JOIN args a" ~written: "SELECT s.id, a.key FROM slice s JOIN args a ON s.arg_set_id = \ a.arg_set_id" ~want:(2 * n) (* The padded side takes its key from its own USING: every arg, and the slice with none. *) let left ~index = same_as_on ~index ~joined:"SELECT s.id, a.key FROM slice s LEFT JOIN args a USING(arg_set_id)" ~written: "SELECT s.id, a.key FROM slice s LEFT JOIN args a ON s.arg_set_id = \ a.arg_set_id" ~want:((2 * n) + 1) let cases ~index = let on = if index then "with the index" else "without an index" in [ Alcotest.test_case ("USING, " ^ on) `Quick (trace ~index); Alcotest.test_case ("NATURAL, " ^ on) `Quick (natural ~index); Alcotest.test_case ("LEFT JOIN USING, " ^ on) `Quick (left ~index); ] let () = Alcotest.run "join_using" [ ("plan", cases ~index:true @ cases ~index:false) ]