···268268 }
269269270270 db.Exec(`CREATE INDEX IF NOT EXISTS idx_annotations_target_hash ON annotations(target_hash)`)
271271+ db.Exec(`CREATE INDEX IF NOT EXISTS idx_annotations_target_source ON annotations(target_source)`)
271272 db.Exec(`CREATE INDEX IF NOT EXISTS idx_annotations_author_did ON annotations(author_did)`)
272273 db.Exec(`CREATE INDEX IF NOT EXISTS idx_annotations_motivation ON annotations(motivation)`)
273274 db.Exec(`CREATE INDEX IF NOT EXISTS idx_annotations_created_at ON annotations(created_at DESC)`)
···287288 )`)
288289 db.Exec(`CREATE INDEX IF NOT EXISTS idx_highlights_target_hash ON highlights(target_hash)`)
289290 db.Exec(`CREATE INDEX IF NOT EXISTS idx_highlights_author_did ON highlights(author_did)`)
291291+ db.Exec(`CREATE INDEX IF NOT EXISTS idx_highlights_created_at ON highlights(created_at DESC)`)
290292291293 db.Exec(`CREATE TABLE IF NOT EXISTS bookmarks (
292294 uri TEXT PRIMARY KEY,
···302304 )`)
303305 db.Exec(`CREATE INDEX IF NOT EXISTS idx_bookmarks_source_hash ON bookmarks(source_hash)`)
304306 db.Exec(`CREATE INDEX IF NOT EXISTS idx_bookmarks_author_did ON bookmarks(author_did)`)
307307+ db.Exec(`CREATE INDEX IF NOT EXISTS idx_bookmarks_created_at ON bookmarks(created_at DESC)`)
305308306309 db.Exec(`CREATE TABLE IF NOT EXISTS replies (
307310 uri TEXT PRIMARY KEY,
···316319 )`)
317320 db.Exec(`CREATE INDEX IF NOT EXISTS idx_replies_parent_uri ON replies(parent_uri)`)
318321 db.Exec(`CREATE INDEX IF NOT EXISTS idx_replies_root_uri ON replies(root_uri)`)
322322+ db.Exec(`CREATE INDEX IF NOT EXISTS idx_replies_created_at ON replies(created_at DESC)`)
319323320324 db.Exec(`CREATE TABLE IF NOT EXISTS likes (
321325 uri TEXT PRIMARY KEY,
···338342 indexed_at ` + dateType + ` NOT NULL
339343 )`)
340344 db.Exec(`CREATE INDEX IF NOT EXISTS idx_collections_author_did ON collections(author_did)`)
345345+ db.Exec(`CREATE INDEX IF NOT EXISTS idx_collections_created_at ON collections(created_at DESC)`)
341346342347 db.Exec(`CREATE TABLE IF NOT EXISTS collection_items (
343348 uri TEXT PRIMARY KEY,
···350355 )`)
351356 db.Exec(`CREATE INDEX IF NOT EXISTS idx_collection_items_collection ON collection_items(collection_uri)`)
352357 db.Exec(`CREATE INDEX IF NOT EXISTS idx_collection_items_annotation ON collection_items(annotation_uri)`)
358358+ db.Exec(`CREATE INDEX IF NOT EXISTS idx_collection_items_created_at ON collection_items(created_at DESC)`)
353359354360 db.Exec(`CREATE TABLE IF NOT EXISTS sessions (
355361 id TEXT PRIMARY KEY,
+25-16
backend/internal/db/queries_annotations.go
···135135func (db *DB) GetPopularAnnotations(limit, offset int) ([]Annotation, error) {
136136 since := time.Now().AddDate(0, 0, -14)
137137 rows, err := db.Query(db.Rebind(`
138138- SELECT uri, author_did, motivation, body_value, body_format, body_uri, target_source, target_hash, target_title, selector_json, tags_json, created_at, indexed_at, cid
139139- FROM annotations
140140- WHERE created_at > ? AND (
141141- (SELECT COUNT(*) FROM likes WHERE subject_uri = annotations.uri) +
142142- (SELECT COUNT(*) FROM replies WHERE root_uri = annotations.uri)
143143- ) > 0
144144- ORDER BY (
145145- (SELECT COUNT(*) FROM likes WHERE subject_uri = annotations.uri) +
146146- (SELECT COUNT(*) FROM replies WHERE root_uri = annotations.uri)
147147- ) DESC, created_at DESC
138138+ SELECT
139139+ a.uri, a.author_did, a.motivation, a.body_value, a.body_format,
140140+ a.body_uri, a.target_source, a.target_hash, a.target_title,
141141+ a.selector_json, a.tags_json, a.created_at, a.indexed_at, a.cid
142142+ FROM annotations a
143143+ LEFT JOIN (
144144+ SELECT subject_uri, COUNT(*) as cnt FROM likes GROUP BY subject_uri
145145+ ) l ON l.subject_uri = a.uri
146146+ LEFT JOIN (
147147+ SELECT root_uri, COUNT(*) as cnt FROM replies GROUP BY root_uri
148148+ ) r ON r.root_uri = a.uri
149149+ WHERE a.created_at > ? AND (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) > 0
150150+ ORDER BY (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) DESC, a.created_at DESC
148151 LIMIT ? OFFSET ?
149152 `), since, limit, offset)
150153 if err != nil {
···159162 olderThan := time.Now().AddDate(0, 0, -1)
160163 since := time.Now().AddDate(0, 0, -14)
161164 rows, err := db.Query(db.Rebind(`
162162- SELECT uri, author_did, motivation, body_value, body_format, body_uri, target_source, target_hash, target_title, selector_json, tags_json, created_at, indexed_at, cid
163163- FROM annotations
164164- WHERE created_at < ? AND created_at > ? AND (
165165- (SELECT COUNT(*) FROM likes WHERE subject_uri = annotations.uri) +
166166- (SELECT COUNT(*) FROM replies WHERE root_uri = annotations.uri)
167167- ) = 0
165165+ SELECT
166166+ a.uri, a.author_did, a.motivation, a.body_value, a.body_format,
167167+ a.body_uri, a.target_source, a.target_hash, a.target_title,
168168+ a.selector_json, a.tags_json, a.created_at, a.indexed_at, a.cid
169169+ FROM annotations a
170170+ LEFT JOIN (
171171+ SELECT subject_uri, COUNT(*) as cnt FROM likes GROUP BY subject_uri
172172+ ) l ON l.subject_uri = a.uri
173173+ LEFT JOIN (
174174+ SELECT root_uri, COUNT(*) as cnt FROM replies GROUP BY root_uri
175175+ ) r ON r.root_uri = a.uri
176176+ WHERE a.created_at < ? AND a.created_at > ? AND (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) = 0
168177 ORDER BY RANDOM()
169178 LIMIT ? OFFSET ?
170179 `), olderThan, since, limit, offset)
+23-16
backend/internal/db/queries_bookmarks.go
···5959func (db *DB) GetPopularBookmarks(limit, offset int) ([]Bookmark, error) {
6060 since := time.Now().AddDate(0, 0, -14)
6161 rows, err := db.Query(db.Rebind(`
6262- SELECT uri, author_did, source, source_hash, title, description, tags_json, created_at, indexed_at, cid
6363- FROM bookmarks
6464- WHERE created_at > ? AND (
6565- (SELECT COUNT(*) FROM likes WHERE subject_uri = bookmarks.uri) +
6666- (SELECT COUNT(*) FROM replies WHERE root_uri = bookmarks.uri)
6767- ) > 0
6868- ORDER BY (
6969- (SELECT COUNT(*) FROM likes WHERE subject_uri = bookmarks.uri) +
7070- (SELECT COUNT(*) FROM replies WHERE root_uri = bookmarks.uri)
7171- ) DESC, created_at DESC
6262+ SELECT
6363+ b.uri, b.author_did, b.source, b.source_hash, b.title,
6464+ b.description, b.tags_json, b.created_at, b.indexed_at, b.cid
6565+ FROM bookmarks b
6666+ LEFT JOIN (
6767+ SELECT subject_uri, COUNT(*) as cnt FROM likes GROUP BY subject_uri
6868+ ) l ON l.subject_uri = b.uri
6969+ LEFT JOIN (
7070+ SELECT root_uri, COUNT(*) as cnt FROM replies GROUP BY root_uri
7171+ ) r ON r.root_uri = b.uri
7272+ WHERE b.created_at > ? AND (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) > 0
7373+ ORDER BY (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) DESC, b.created_at DESC
7274 LIMIT ? OFFSET ?
7375 `), since, limit, offset)
7476 if err != nil {
···9193 olderThan := time.Now().AddDate(0, 0, -1)
9294 since := time.Now().AddDate(0, 0, -14)
9395 rows, err := db.Query(db.Rebind(`
9494- SELECT uri, author_did, source, source_hash, title, description, tags_json, created_at, indexed_at, cid
9595- FROM bookmarks
9696- WHERE created_at < ? AND created_at > ? AND (
9797- (SELECT COUNT(*) FROM likes WHERE subject_uri = bookmarks.uri) +
9898- (SELECT COUNT(*) FROM replies WHERE root_uri = bookmarks.uri)
9999- ) = 0
9696+ SELECT
9797+ b.uri, b.author_did, b.source, b.source_hash, b.title,
9898+ b.description, b.tags_json, b.created_at, b.indexed_at, b.cid
9999+ FROM bookmarks b
100100+ LEFT JOIN (
101101+ SELECT subject_uri, COUNT(*) as cnt FROM likes GROUP BY subject_uri
102102+ ) l ON l.subject_uri = b.uri
103103+ LEFT JOIN (
104104+ SELECT root_uri, COUNT(*) as cnt FROM replies GROUP BY root_uri
105105+ ) r ON r.root_uri = b.uri
106106+ WHERE b.created_at < ? AND b.created_at > ? AND (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) = 0
100107 ORDER BY RANDOM()
101108 LIMIT ? OFFSET ?
102109 `), olderThan, since, limit, offset)
+23-16
backend/internal/db/queries_collections.go
···123123func (db *DB) GetPopularCollectionItems(limit, offset int) ([]CollectionItem, error) {
124124 since := time.Now().AddDate(0, 0, -14)
125125 rows, err := db.Query(db.Rebind(`
126126- SELECT uri, author_did, collection_uri, annotation_uri, position, created_at, indexed_at
127127- FROM collection_items
128128- WHERE created_at > ? AND (
129129- (SELECT COUNT(*) FROM likes WHERE subject_uri = collection_items.annotation_uri) +
130130- (SELECT COUNT(*) FROM replies WHERE root_uri = collection_items.annotation_uri)
131131- ) > 0
132132- ORDER BY (
133133- (SELECT COUNT(*) FROM likes WHERE subject_uri = collection_items.annotation_uri) +
134134- (SELECT COUNT(*) FROM replies WHERE root_uri = collection_items.annotation_uri)
135135- ) DESC, created_at DESC
126126+ SELECT
127127+ c.uri, c.author_did, c.collection_uri, c.annotation_uri,
128128+ c.position, c.created_at, c.indexed_at
129129+ FROM collection_items c
130130+ LEFT JOIN (
131131+ SELECT subject_uri, COUNT(*) as cnt FROM likes GROUP BY subject_uri
132132+ ) l ON l.subject_uri = c.annotation_uri
133133+ LEFT JOIN (
134134+ SELECT root_uri, COUNT(*) as cnt FROM replies GROUP BY root_uri
135135+ ) r ON r.root_uri = c.annotation_uri
136136+ WHERE c.created_at > ? AND (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) > 0
137137+ ORDER BY (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) DESC, c.created_at DESC
136138 LIMIT ? OFFSET ?
137139 `), since, limit, offset)
138140 if err != nil {
···155157 olderThan := time.Now().AddDate(0, 0, -1)
156158 since := time.Now().AddDate(0, 0, -14)
157159 rows, err := db.Query(db.Rebind(`
158158- SELECT uri, author_did, collection_uri, annotation_uri, position, created_at, indexed_at
159159- FROM collection_items
160160- WHERE created_at < ? AND created_at > ? AND (
161161- (SELECT COUNT(*) FROM likes WHERE subject_uri = collection_items.annotation_uri) +
162162- (SELECT COUNT(*) FROM replies WHERE root_uri = collection_items.annotation_uri)
163163- ) = 0
160160+ SELECT
161161+ c.uri, c.author_did, c.collection_uri, c.annotation_uri,
162162+ c.position, c.created_at, c.indexed_at
163163+ FROM collection_items c
164164+ LEFT JOIN (
165165+ SELECT subject_uri, COUNT(*) as cnt FROM likes GROUP BY subject_uri
166166+ ) l ON l.subject_uri = c.annotation_uri
167167+ LEFT JOIN (
168168+ SELECT root_uri, COUNT(*) as cnt FROM replies GROUP BY root_uri
169169+ ) r ON r.root_uri = c.annotation_uri
170170+ WHERE c.created_at < ? AND c.created_at > ? AND (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) = 0
164171 ORDER BY RANDOM()
165172 LIMIT ? OFFSET ?
166173 `), olderThan, since, limit, offset)
+23-16
backend/internal/db/queries_highlights.go
···6060func (db *DB) GetPopularHighlights(limit, offset int) ([]Highlight, error) {
6161 since := time.Now().AddDate(0, 0, -14)
6262 rows, err := db.Query(db.Rebind(`
6363- SELECT uri, author_did, target_source, target_hash, target_title, selector_json, color, tags_json, created_at, indexed_at, cid
6464- FROM highlights
6565- WHERE created_at > ? AND (
6666- (SELECT COUNT(*) FROM likes WHERE subject_uri = highlights.uri) +
6767- (SELECT COUNT(*) FROM replies WHERE root_uri = highlights.uri)
6868- ) > 0
6969- ORDER BY (
7070- (SELECT COUNT(*) FROM likes WHERE subject_uri = highlights.uri) +
7171- (SELECT COUNT(*) FROM replies WHERE root_uri = highlights.uri)
7272- ) DESC, created_at DESC
6363+ SELECT
6464+ h.uri, h.author_did, h.target_source, h.target_hash, h.target_title,
6565+ h.selector_json, h.color, h.tags_json, h.created_at, h.indexed_at, h.cid
6666+ FROM highlights h
6767+ LEFT JOIN (
6868+ SELECT subject_uri, COUNT(*) as cnt FROM likes GROUP BY subject_uri
6969+ ) l ON l.subject_uri = h.uri
7070+ LEFT JOIN (
7171+ SELECT root_uri, COUNT(*) as cnt FROM replies GROUP BY root_uri
7272+ ) r ON r.root_uri = h.uri
7373+ WHERE h.created_at > ? AND (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) > 0
7474+ ORDER BY (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) DESC, h.created_at DESC
7375 LIMIT ? OFFSET ?
7476 `), since, limit, offset)
7577 if err != nil {
···9294 olderThan := time.Now().AddDate(0, 0, -1)
9395 since := time.Now().AddDate(0, 0, -14)
9496 rows, err := db.Query(db.Rebind(`
9595- SELECT uri, author_did, target_source, target_hash, target_title, selector_json, color, tags_json, created_at, indexed_at, cid
9696- FROM highlights
9797- WHERE created_at < ? AND created_at > ? AND (
9898- (SELECT COUNT(*) FROM likes WHERE subject_uri = highlights.uri) +
9999- (SELECT COUNT(*) FROM replies WHERE root_uri = highlights.uri)
100100- ) = 0
9797+ SELECT
9898+ h.uri, h.author_did, h.target_source, h.target_hash, h.target_title,
9999+ h.selector_json, h.color, h.tags_json, h.created_at, h.indexed_at, h.cid
100100+ FROM highlights h
101101+ LEFT JOIN (
102102+ SELECT subject_uri, COUNT(*) as cnt FROM likes GROUP BY subject_uri
103103+ ) l ON l.subject_uri = h.uri
104104+ LEFT JOIN (
105105+ SELECT root_uri, COUNT(*) as cnt FROM replies GROUP BY root_uri
106106+ ) r ON r.root_uri = h.uri
107107+ WHERE h.created_at < ? AND h.created_at > ? AND (COALESCE(l.cnt, 0) + COALESCE(r.cnt, 0)) = 0
101108 ORDER BY RANDOM()
102109 LIMIT ? OFFSET ?
103110 `), olderThan, since, limit, offset)
+11-1
backend/internal/db/queries_recommendations.go
···422422}
423423424424func (db *DB) GetCandidateDocuments(userDID string, limit int) ([]CandidateDocument, error) {
425425+ // Note: We use NOT LIKE instead of !~* for cross-database compatibility and performance.
426426+ // The engagement count sub-select is also constrained to recent elements if possible, but
427427+ // for now we just optimize the regex and exact grouping.
425428 rows, err := db.Query(db.Rebind(`
426429 SELECT
427430 d.uri, d.author_did, d.site, d.path, d.title, d.description, d.tags_json,
···440443 AND (p.show_in_discover IS NULL OR p.show_in_discover = true)
441444 AND LENGTH(d.title) > 15
442445 AND (LENGTH(COALESCE(d.description, '')) >= 30 OR LENGTH(COALESCE(d.text_content, '')) >= 100)
443443- AND d.title !~* '(^test$|^test\\s|\\stest$|^testing|^hello\\sworld|^untitled|^draft|^asdf|^lorem|^foo$|^bar$|^placeholder)'
446446+ AND LOWER(d.title) NOT LIKE '%test%'
447447+ AND LOWER(d.title) NOT LIKE '%testing%'
448448+ AND LOWER(d.title) NOT LIKE '%hello world%'
449449+ AND LOWER(d.title) NOT LIKE '%untitled%'
450450+ AND LOWER(d.title) NOT LIKE '%draft%'
451451+ AND LOWER(d.title) NOT LIKE '%asdf%'
452452+ AND LOWER(d.title) NOT LIKE '%lorem%'
453453+ AND LOWER(d.title) NOT LIKE '%placeholder%'
444454 AND d.uri NOT IN (
445455 SELECT DISTINCT document_uri FROM annotation_embeddings
446456 WHERE author_did = ? AND document_uri IS NOT NULL