Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136(* What one row costs the write path, and what a VIRTUAL generated column costs a read. Six shapes: a prepared [INSERT] is the floor; the same on a STRICT table pays the declared type per row; a multi-row [INSERT] is the bulk-load shape; [INSERT ... SELECT] arrives from the executor; and two reads fold a table without and with a VIRTUAL column. Usage: write_bench.exe [n] [repeats]. *)
let () = Observe.Memtrace.trace_if_requested ()
let int_arg i default = try int_of_string Sys.argv.(i) with Failure _ | Invalid_argument _ -> default
let n = int_arg 1 20000let repeats = int_arg 2 5
(* Built once so the measured call is the engine, not this. *)let tuples = String.concat "," (List.init n (fun i -> Fmt.str "(%d,%d,'key_%08d')" (i + 1) (i mod 10) i))
(* One multi-row INSERT, the shape a bulk load takes: one statement, one catalog, one autocommit at the end. *)let insert_into ?(cols = "") table = Fmt.str "INSERT INTO %s%s VALUES %s" table cols tuples
let db_with ddl = let db = Sqlite.in_memory () in List.iter (fun sql -> Sqlite.Raw.exec db sql ~params:[]) ddl; db
(* Words allocated and microseconds spent per row of [f]. *)let measured f = let w0 = Gc.minor_words () and t0 = Unix.gettimeofday () in f (); let us = (Unix.gettimeofday () -. t0) *. 1e6 /. float n in ((Gc.minor_words () -. w0) /. float n, us)
(* Allocation is the same every run, so the first is as good as any; the time is the best of [repeats], the run least disturbed by the machine. *)let measure f = let runs = List.init repeats (fun _ -> f ()) in (fst (List.hd runs), List.fold_left (fun a (_, us) -> min a us) infinity runs)
let plain_ddl = "CREATE TABLE t (a INTEGER PRIMARY KEY, b INT, c TEXT)"let strict_ddl = "CREATE TABLE t (a INTEGER PRIMARY KEY, b INT, c TEXT) STRICT"
let virtual_ddl = "CREATE TABLE t (a INTEGER PRIMARY KEY, b INT, c TEXT, d INT AS (b + 1) \ VIRTUAL)"
(* The same rows as bound parameters, built once for the same reason. *)let bound = Array.init n (fun i -> let key = Fmt.str "key_%08d" i in [ Sqlite.Int (Int64.of_int (i + 1)); Sqlite.Int (Int64.of_int (i mod 10)); Sqlite.Text key; ])
(* One parse, [n] executions, one transaction: the per-row funnel with the parser held out of it. *)let prepared_into ddl () = let db = db_with ddl in let stmt = Sqlite.Raw.prepare db "INSERT INTO t VALUES (?, ?, ?)" in measured (fun () -> Sqlite.with_transaction db (fun () -> Array.iter (fun params -> Sqlite.Raw.exec_stmt stmt ~params) bound))
let prepared ddl = prepared_into [ ddl ]
(* The parent is ten rows and every key is in it, so what this measures is the foreign-key check's per-row cost, not the search it ends in. *)let fk_ddl = [ "PRAGMA foreign_keys = ON"; "CREATE TABLE p (k INTEGER PRIMARY KEY)"; "INSERT INTO p VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)"; "CREATE TABLE t (a INTEGER PRIMARY KEY, b INT REFERENCES p(k), c TEXT)"; ]
let bulk ddl () = let db = db_with [ ddl ] in measured (fun () -> Sqlite.Raw.exec db (insert_into "t") ~params:[])
(* Rows arrive from the executor rather than a literal tuple list. *)let insert_select () = let db = db_with [ "CREATE TABLE src (a INTEGER PRIMARY KEY, b INT, c TEXT)"; "CREATE TABLE t (a INTEGER PRIMARY KEY, b INT, c TEXT)"; ] in Sqlite.Raw.exec db (insert_into "src") ~params:[]; measured (fun () -> Sqlite.Raw.exec db "INSERT INTO t SELECT a, b, c FROM src" ~params:[])
(* A full scan through the row codec: every stored record is spread back over the declared columns, which is where a VIRTUAL column is recomputed. *)let scan ?cols ddl () = let db = db_with [ ddl ] in Sqlite.Raw.exec db (insert_into ?cols "t") ~params:[]; measured (fun () -> ignore (Sqlite.Table.fold_table db "t" ~init:0 ~f:(fun _ _ acc -> acc + 1)))
let () = let one_by_one = measure (prepared plain_ddl) in let one_by_one_strict = measure (prepared strict_ddl) in let one_by_one_fk = measure (prepared_into fk_ddl) in let plain = measure (bulk plain_ddl) in let from_select = measure insert_select in let read = measure (scan plain_ddl) in let read_virtual = measure (scan ~cols:" (a, b, c)" virtual_ddl) in let line what (words, us) = Fmt.pr " %-16s %9.1f words/row %7.2f us/row\n" what words us in let delta what (words, us) (words', us') = Fmt.pr " %-16s %+9.1f words/row %+7.2f us/row (%+.1f%% words)\n" what (words' -. words) (us' -. us) ((words' -. words) /. words *. 100.) in Fmt.pr "%d rows:\n" n; line "insert one by one" one_by_one; line "insert STRICT" one_by_one_strict; line "insert FK child" one_by_one_fk; line "insert VALUES" plain; line "insert..select" from_select; line "scan" read; line "scan VIRTUAL" read_virtual; delta "STRICT" one_by_one one_by_one_strict; delta "FK" one_by_one one_by_one_fk; delta "VIRTUAL" read read_virtual; Fmt.pr "%!"