Skip to content

Commit

Permalink
Optimizing DB access
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenldl committed Dec 29, 2024
1 parent b5287e9 commit 6dbfca2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
8 changes: 4 additions & 4 deletions lib/index.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ]
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 11 additions & 17 deletions lib/params.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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

0 comments on commit 6dbfca2

Please sign in to comment.