ssb: Faster loads by encouraging the right queries with CTEs in fetch_about.

This commit is contained in:
Cory McWilliams 2025-05-07 17:52:02 -04:00
parent b3cedf2baa
commit f1ced31f69
2 changed files with 56 additions and 47 deletions

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "🦀", "emoji": "🦀",
"previous": "&YeFLcHjD39FzsciGDfXuLd0hPzu4Rrp7QlMA7NaUCio=.sha256" "previous": "&RkHUl/WRe0UpvTB2W6cw0f4AeupL/2PEI1pXg5PDXBg=.sha256"
} }

View File

@ -177,6 +177,7 @@ class TfElement extends LitElement {
} }
} }
const k_account_chunk_size = 4096;
const k_chunk_size = 1024; const k_chunk_size = 1024;
let min_row_id = 0; let min_row_id = 0;
console.log( console.log(
@ -189,43 +190,49 @@ class TfElement extends LitElement {
); );
try { try {
while (true) { while (true) {
let max_seen;
for (let account_chunk = 0; account_chunk < ids.length; account_chunk += k_account_chunk_size) {
let ids_chunk = ids.slice(account_chunk, account_chunk + k_account_chunk_size);
let abouts = await tfrpc.rpc.query( let abouts = await tfrpc.rpc.query(
` `
SELECT * FROM ( WITH
past AS (
SELECT SELECT
messages.rowid AS rowid, messages.author, json(messages.content) AS content, messages.sequence messages.rowid AS rowid, messages.author, json(messages.content) AS content, messages.sequence
FROM FROM
messages, messages
json_each(?1) AS following
WHERE WHERE
messages.author = following.value AND
messages.content ->> 'type' = 'about' AND
messages.rowid > ?3 AND messages.rowid > ?3 AND
messages.rowid <= ?4 messages.rowid <= ?4 AND
UNION messages.content ->> 'type' = 'about'
),
current AS (
SELECT SELECT
messages.rowid AS rowid, messages.author, json(messages.content) AS content, messages.sequence messages.rowid AS rowid, messages.author, json(messages.content) AS content, messages.sequence
FROM FROM
messages, messages
json_each(?2) AS following
WHERE WHERE
messages.author = following.value AND
messages.content ->> 'type' = 'about' AND
messages.rowid > ?6 AND messages.rowid > ?6 AND
messages.rowid <= ?4 messages.rowid <= ?4 AND
messages.content ->> 'type' = 'about'
) )
SELECT * FROM past
JOIN json_each(?1) AS following
ON past.author = following.value
UNION SELECT * FROM current
JOIN json_each(?2) AS following
ON current.author = following.value
ORDER BY rowid LIMIT ?5 ORDER BY rowid LIMIT ?5
`, `,
[ [
JSON.stringify(ids.filter((id) => cache.about[id])), JSON.stringify(ids_chunk.filter((id) => cache.about[id])),
JSON.stringify(ids.filter((id) => !cache.about[id])), JSON.stringify(ids_chunk.filter((id) => !cache.about[id])),
cache.last_row_id, cache.last_row_id,
max_row_id, max_row_id,
k_chunk_size, k_chunk_size,
min_row_id, min_row_id,
] ]
); );
let max_seen;
for (let about of abouts) { for (let about of abouts) {
let content = JSON.parse(about.content); let content = JSON.parse(about.content);
if (content.about === about.author) { if (content.about === about.author) {
@ -238,6 +245,8 @@ class TfElement extends LitElement {
} }
max_seen = about.rowid; max_seen = about.rowid;
} }
console.log(account_chunk, '/', ids.length, 'accounts');
}
console.log( console.log(
'cache =', 'cache =',
cache.last_row_id, cache.last_row_id,