These indexes weren't helping in practice, so remove them. Avoid some queries during a full refresh to load in maybe 1/3rd the time.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3800 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-01-29 19:00:44 +00:00
parent d77c452120
commit 0ddb86b5a8
3 changed files with 70 additions and 53 deletions

View File

@ -7,8 +7,9 @@ var g_ready = false;
var g_selected = null;
var g_following_cache = {};
var g_followers_cache = {};
var g_following_deep_cache = {};
var g_stats = {};
var g_sequence = {};
async function following(db, id) {
if (g_following_cache[id]) {
@ -41,6 +42,7 @@ async function following(db, id) {
f.users.delete(row.contact);
}
f.sequence = row.sequence;
g_sequence[id] = row.sequence;
});
var as_set = f.users;
f.users = Array.from(f.users).sort();
@ -76,34 +78,38 @@ async function getAbout(db, id) {
if (!f || f.version != k_version) {
f = {about: {}, sequence: 0, version: k_version};
}
await ssb.sqlStream(
"SELECT "+
" sequence, "+
" content "+
"FROM messages "+
"WHERE "+
" sequence > ?1 AND "+
" author = ?2 AND "+
" json_extract(content, '$.type') = 'about' AND "+
" json_extract(content, '$.about') = author "+
"UNION SELECT MAX(sequence) as sequence, NULL FROM messages WHERE author = ?2 "+
"ORDER BY sequence",
[f.sequence, id],
function(row) {
f.sequence = row.sequence;
if (row.content) {
var about = {};
try {
about = JSON.parse(row.content);
} catch {
if (g_sequence[id] > f.sequence) {
await ssb.sqlStream(
"SELECT "+
" sequence, "+
" content "+
"FROM messages "+
"WHERE "+
" author = ?1 AND "+
" sequence > ?2 AND "+
" json_extract(content, '$.type') = 'about' AND "+
" json_extract(content, '$.about') = ?1 "+
"UNION SELECT MAX(sequence) as sequence, NULL FROM messages WHERE author = ?1 "+
"ORDER BY sequence",
[id, f.sequence],
function(row) {
g_stats.rows_read = (g_stats.rows_read || 0) + 1;
f.sequence = row.sequence;
if (row.content) {
var about = {};
try {
about = JSON.parse(row.content);
} catch {
}
delete about.about;
delete about.type;
f.about = Object.assign(f.about, about);
}
delete about.about;
delete about.type;
f.about = Object.assign(f.about, about);
}
});
});
}
var j = JSON.stringify(f);
if (o != j) {
g_stats.rows_written = (g_stats.rows_written || 0) + 1;
await db.set(id + ":about", j);
}
return f.about;
@ -250,27 +256,29 @@ async function getVotes(db, id) {
if (!f || f.version != k_version) {
f = {votes: [], sequence: 0, version: k_version};
}
await ssb.sqlStream(
"SELECT "+
" author, "+
" id, "+
" sequence, "+
" timestamp, "+
" content "+
"FROM messages "+
"WHERE "+
" author = ? AND "+
" sequence > ? AND "+
" json_extract(content, '$.type') = 'vote' "+
"UNION SELECT NULL, NULL, MAX(sequence), NULL, NULL FROM messages WHERE author = ? "+
"ORDER BY sequence DESC LIMIT ?",
[f.sequence, id, id, k_votes_max],
function(row) {
if (row.id) {
votes.push(row);
}
f.sequence = Math.max(f.sequence, row.sequence);
});
if (g_sequence[id] > f.sequence) {
await ssb.sqlStream(
"SELECT "+
" author, "+
" id, "+
" sequence, "+
" timestamp, "+
" content "+
"FROM messages "+
"WHERE "+
" author = ? AND "+
" sequence > ? AND "+
" json_extract(content, '$.type') = 'vote' "+
"UNION SELECT NULL, NULL, MAX(sequence), NULL, NULL FROM messages WHERE author = ? "+
"ORDER BY sequence DESC LIMIT ?",
[f.sequence, id, id, k_votes_max],
function(row) {
if (row.id) {
votes.push(row);
}
f.sequence = Math.max(f.sequence, row.sequence);
});
}
f.votes = [].concat(votes, f.votes).slice(0, k_votes_max);
var j = JSON.stringify(f);
if (o != j) {
@ -308,8 +316,8 @@ async function refresh(selected) {
var timing = [];
timing.push({name: 'start', time: new Date()});
g_following_cache = {};
g_followers_cache = {};
g_following_deep_cache = {};
g_sequence = {};
await app.postMessage({clear: true});
var whoami = await ssb.whoami();
var db = await database("ssb");
@ -353,13 +361,24 @@ async function refresh(selected) {
timing.push({name: 'get_root_posts', time: new Date()});
await Promise.all(all_posts.map(x => app.postMessage({message: x})));
timing.push({name: 'send_posts', time: new Date()});
await Promise.all(all_followed.map(id => getAbout(db, id).then(results => app.postMessage({user: {user: id, about: results}}))));
await Promise.all(all_followed.map(id => getAbout(db, id).then(results => Object.keys(results).length ? app.postMessage({user: {user: id, about: results}}) : null)));
timing.push({name: 'about', time: new Date()});
await Promise.all(all_followed.map(id => getVotes(db, id).then(results => results.length ? app.postMessage({votes: results}) : null)));
timing.push({name: 'votes', time: new Date()});
await Promise.all(all_followed.map(id => following(db, id).then(results => app.postMessage({following: {id: id, users: [...results]}}))));
await all_followed.map(
id => app.postMessage(
{
following: {
id: id,
users: [...(g_following_cache[id] || [])],
}
}
)
);
timing.push({name: 'following', time: new Date()});
print(JSON.stringify(g_stats));
var times = {};
var previous = null;
for (let t of timing) {