Skip to content

Commit f560be7

Browse files
committed
Refactor and clean up SQL queries and indexes.
1 parent c17f0d1 commit f560be7

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

static/sql/queries.sql

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- FTS5 search with length-based ranking (shorter content ranks higher).
33
-- Exact content matches get extra boost via negative rank adjustment.
44
-- $1: lang, $2: raw query, $3: FTS query, $4: status, $5: offset, $6: limit
5-
SELECT DISTINCT e.*,
5+
SELECT e.*,
66
JSON_ARRAY_LENGTH(e.content) AS content_length,
77
-- Rank: weight - (50 - content_length). Shorter content = more negative = ranks first.
88
-- Exact matches get extra -1000 boost to always rank highest.
@@ -12,11 +12,10 @@ SELECT DISTINCT e.*,
1212
COUNT(*) OVER() AS total
1313
FROM entries e
1414
INNER JOIN entries_fts fts ON fts.rowid = e.id
15-
INNER JOIN relations r ON e.id = r.from_id
1615
WHERE entries_fts MATCH $3
16+
AND EXISTS (SELECT 1 FROM relations r WHERE r.from_id = e.id)
1717
AND ($1 = '' OR e.lang = $1)
1818
AND ($4 = '' OR e.status = $4)
19-
AND $2 != ''
2019
ORDER BY rank
2120
LIMIT $6 OFFSET $5;
2221

@@ -70,12 +69,8 @@ WHERE ($2 = '' OR e.lang = $2)
7069
ORDER BY sub.from_id, sub.relation_types, sub.relation_weight;
7170

7271
-- name: get-entry
73-
SELECT *, JSON_ARRAY_LENGTH(content) AS content_length FROM entries WHERE
74-
CASE
75-
WHEN $1 > 0 THEN id = $1
76-
WHEN $2 != '' THEN guid = $2
77-
ELSE 0
78-
END;
72+
SELECT *, JSON_ARRAY_LENGTH(content) AS content_length FROM entries
73+
WHERE ($1 > 0 AND id = $1) OR ($1 <= 0 AND $2 != '' AND guid = $2);
7974

8075
-- name: get-parent-relations
8176
SELECT e.*, r.id AS relation_id FROM entries e
@@ -150,7 +145,7 @@ SELECT json_object(
150145
'entries', (SELECT COUNT(*) FROM entries),
151146
'relations', (SELECT COUNT(*) FROM relations),
152147
'languages', (
153-
SELECT json_group_object(lang, cnt) FROM (
148+
SELECT JSON_GROUP_OBJECT(lang, cnt) FROM (
154149
SELECT lang, COUNT(*) AS cnt FROM entries GROUP BY lang
155150
)
156151
)
@@ -172,7 +167,7 @@ INSERT INTO entries (guid, content, initial, weight, tokens, lang, tags, phones,
172167
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
173168
WHERE NOT EXISTS (
174169
SELECT 1 FROM entries
175-
WHERE lower(substr(json_extract(content, '$[0]'), 1, 50)) = lower(substr(json_extract($2, '$[0]'), 1, 50))
170+
WHERE content_head = LOWER(SUBSTR(JSON_EXTRACT($2, '$[0]'), 1, 50))
176171
AND lang = $6 AND status != 'disabled'
177172
)
178173
RETURNING id;

static/sql/schema.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ CREATE TABLE IF NOT EXISTS entries (
3333
) STRICT;
3434

3535
CREATE INDEX IF NOT EXISTS idx_entries_initial ON entries(initial);
36-
CREATE INDEX IF NOT EXISTS idx_entries_lang ON entries(lang);
3736
CREATE INDEX IF NOT EXISTS idx_entries_status ON entries(status);
37+
CREATE INDEX IF NOT EXISTS idx_entries_lang_status ON entries(lang, status);
3838

3939
CREATE INDEX IF NOT EXISTS idx_entries_content_head ON entries(content_head);
4040

@@ -69,8 +69,9 @@ CREATE TABLE IF NOT EXISTS relations (
6969
UNIQUE(from_id, to_id)
7070
) STRICT;
7171

72-
CREATE INDEX IF NOT EXISTS idx_relations_from ON relations(from_id);
7372
CREATE INDEX IF NOT EXISTS idx_relations_to ON relations(to_id);
73+
CREATE INDEX IF NOT EXISTS idx_relations_status ON relations(status);
74+
CREATE INDEX IF NOT EXISTS idx_relations_from_status ON relations(from_id, status);
7475

7576
CREATE TABLE IF NOT EXISTS comments (
7677
id INTEGER PRIMARY KEY AUTOINCREMENT,

0 commit comments

Comments
 (0)