From 8a66e74074df5ec666352da3482446601b836e66 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 30 Mar 2025 13:18:16 -0400 Subject: [PATCH] ssb: Faster *and* correct. I think. --- apps/ssb/tf-tab-news-feed.js | 17 +++++++++-------- src/ssb.db.c | 6 ++++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/apps/ssb/tf-tab-news-feed.js b/apps/ssb/tf-tab-news-feed.js index 0eb2cdb0..13bf33c8 100644 --- a/apps/ssb/tf-tab-news-feed.js +++ b/apps/ssb/tf-tab-news-feed.js @@ -165,20 +165,21 @@ class TfTabNewsFeedElement extends LitElement { all_news AS ( SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature FROM messages - JOIN json_each(?) AS following ON messages.author = following.value - WHERE timestamp >= 0 AND timestamp < ?3), + JOIN json_each(?) AS following ON messages.author = following.value), news AS ( SELECT * FROM all_news - WHERE (?2 IS NULL OR all_news.timestamp >= ?2) AND all_news.timestamp < ?3 + WHERE all_news.timestamp < ?3 AND (?2 IS NULL OR all_news.timestamp >= ?2) ORDER BY timestamp DESC LIMIT 20 + ), + refs AS ( + SELECT refs_out.ref AS ref FROM messages_refs refs_out JOIN news ON refs_out.message = news.id + UNION + SELECT refs_in.message AS ref FROM messages_refs refs_in JOIN news ON refs_in.ref = news.id ) SELECT TRUE AS is_primary, news.* FROM news UNION - SELECT FALSE AS is_primary, messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature - FROM news - JOIN messages_refs refs_in ON news.id = refs_in.ref - JOIN messages_refs refs_out ON news.id = refs_out.message - JOIN messages ON (messages.id = refs_out.ref OR messages.id = refs_in.message) + SELECT FALSE AS is_primary, all_news.rowid, all_news.id, all_news.previous, all_news.author, all_news.sequence, all_news.timestamp, all_news.hash, json(all_news.content) AS content, all_news.signature + FROM all_news JOIN refs ON all_news.id = refs.ref `, [JSON.stringify(this.following), start_time, end_time] ); diff --git a/src/ssb.db.c b/src/ssb.db.c index ff180edc..6f283885 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -289,8 +289,10 @@ void tf_ssb_db_init(tf_ssb_t* ssb) _tf_ssb_db_exec(db, "DROP TRIGGER IF EXISTS messages_ad_refs"); _tf_ssb_db_exec(db, "CREATE TRIGGER IF NOT EXISTS messages_ad_refs AFTER DELETE ON messages BEGIN DELETE FROM messages_refs WHERE messages_refs.message = old.id; END"); - _tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS messages_refs_message_idx ON messages_refs (message)"); - _tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS messages_refs_ref_idx ON messages_refs (ref)"); + _tf_ssb_db_exec(db, "DROP INDEX IF EXISTS messages_refs_message_idx"); + _tf_ssb_db_exec(db, "DROP INDEX IF EXISTS messages_refs_ref_idx"); + _tf_ssb_db_exec(db, "CREATE UNIQUE INDEX IF NOT EXISTS messages_refs_message_ref_idx ON messages_refs (message, ref)"); + _tf_ssb_db_exec(db, "CREATE UNIQUE INDEX IF NOT EXISTS messages_refs_ref_message_idx ON messages_refs (ref, message)"); if (!_tf_ssb_db_has_rows(db, "PRAGMA table_list('blobs_refs')")) {