From 6dbfca2ff32bbdae78ef7df795f46c6d29af6ed0 Mon Sep 17 00:00:00 2001 From: Darren Li Date: Sun, 29 Dec 2024 19:54:31 +1100 Subject: [PATCH] Optimizing DB access --- lib/index.ml | 8 ++++---- lib/params.ml | 28 +++++++++++----------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/lib/index.ml b/lib/index.ml index 26a1a09..046734a 100644 --- a/lib/index.ml +++ b/lib/index.ml @@ -452,7 +452,7 @@ let word_of_pos db ~doc_hash pos : string = ON word.doc_id = p.doc_id AND word.id = p.word_id WHERE p.doc_id = @doc_id - AND pos = @pos + AND p.pos = @pos |} ~names:[ ("@doc_id", INT doc_id) ; ("@pos", INT (Int64.of_int pos)) ] @@ -791,11 +791,11 @@ module Search = struct SELECT DISTINCT word.id AS word_id, word.word AS word - FROM word - JOIN position p + FROM position p + JOIN word ON p.doc_id = word.doc_id AND p.word_id = word.id - WHERE word.doc_id = @doc_id + WHERE p.doc_id = @doc_id AND p.pos BETWEEN @start AND @end_inc |} ~names:[ ("@doc_id", INT doc_id) diff --git a/lib/params.ml b/lib/params.ml index 0e229fc..d8fd4b8 100644 --- a/lib/params.ml +++ b/lib/params.ml @@ -37,36 +37,31 @@ CREATE TABLE IF NOT EXISTS line_info ( start_pos integer, end_inc_pos integer, page_num integer, - line_num_in_page integer - -- FOREIGN KEY (doc_id) REFERENCES doc_info (id) + line_num_in_page integer, + PRIMARY KEY (doc_id, global_line_num) ); -CREATE INDEX IF NOT EXISTS line_info_index_1 ON line_info (doc_id); - CREATE TABLE IF NOT EXISTS position ( doc_id integer, pos integer, word_id integer, global_line_num integer, - pos_in_line integer - -- FOREIGN KEY (doc_id) REFERENCES doc_info (id), - -- FOREIGN KEY (word_id) REFERENCES word (id) + pos_in_line integer, + PRIMARY KEY (doc_id, pos) ); -CREATE INDEX IF NOT EXISTS position_index_1 ON position (doc_id); -CREATE INDEX IF NOT EXISTS position_index_2 ON position (pos); +CREATE INDEX IF NOT EXISTS position_index_1 ON position (doc_id, word_id); +CREATE INDEX IF NOT EXISTS position_index_2 ON position (doc_id, word_id, pos); CREATE TABLE IF NOT EXISTS page_info ( doc_id integer, page_num integer, line_count integer, start_pos integer, - end_inc_pos integer - -- FOREIGN KEY (doc_id) REFERENCES doc_info (id) + end_inc_pos integer, + PRIMARY KEY (doc_id, page_num) ); -CREATE INDEX IF NOT EXISTS page_info_index_1 ON page_info (doc_id); - CREATE TABLE IF NOT EXISTS doc_info ( id integer PRIMARY KEY AUTOINCREMENT, hash varchar(500), @@ -80,12 +75,11 @@ CREATE INDEX IF NOT EXISTS doc_info_index_1 ON doc_info (hash); CREATE TABLE IF NOT EXISTS word ( id integer, doc_id integer, - word varchar(500) - -- FOREIGN KEY (doc_id) REFERENCES doc_info (id) + word varchar(500), + PRIMARY KEY (doc_id, id) ); -CREATE INDEX IF NOT EXISTS word_index_1 ON word (word); -CREATE INDEX IF NOT EXISTS word_index_2 ON word (doc_id); +CREATE INDEX IF NOT EXISTS word_index_3 ON word (word); |} let db_path : string option ref = ref None