Add DB indexes for performance

This commit is contained in:
2025-07-19 15:50:48 +02:00
parent 03605149d5
commit 74bc0a18da
6 changed files with 75 additions and 10 deletions

View File

@ -97,6 +97,7 @@ const ignoredUsers: string[] =
let databaseReady = false;
if (enableVerboseLog) {
logger.verbose('Enabling verbose DB log');
sqlite3.verbose();
db.on('change', (t, d, table, rowid) => {
logger.verbose('DB change event', t, d, table, rowid);
@ -106,8 +107,16 @@ if (enableVerboseLog) {
logger.verbose('Running', sql);
});
db.on('profile', (sql) => {
logger.verbose('Finished', sql);
db.on('profile', (sql, time) => {
if (sql.startsWith('EXPLAIN')) {
return;
}
logger.verbose('Finished', sql, 'time', time);
if (sql.startsWith('SELECT')) {
db.get('EXPLAIN QUERY PLAN ' + sql, (a: sqlite3.RunResult, b: Error) => {
logger.verbose(sql, a, b);
});
}
});
}
@ -153,6 +162,9 @@ async function applyMigration(migration: Migration) {
db.on('open', () => {
logger.info('Opened database');
db.serialize();
db.run('pragma journal_mode = WAL;');
db.run('pragma synchronous = normal;');
db.run('pragma journal_size_limit = 6144000;');
db.run('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer,"name" TEXT, PRIMARY KEY (id))');
db.all('SELECT id FROM migrations', (err, rows: Migration[]) => {
if (err !== null) {
@ -344,6 +356,26 @@ function getMigrations(): Migration[] {
name: 'song tidal id',
statement: `
ALTER TABLE songs ADD COLUMN tidalId TEXT NULL;`
},
{
id: 10,
name: 'add indexes',
statement: `
CREATE INDEX posts_created_at ON posts(created_at);
CREATE INDEX accounts_acct ON accounts(acct);
CREATE INDEX accounts_username ON accounts(username);`
},
{
id: 11,
name: 'add FK indexes',
statement: `
CREATE INDEX migrations_id ON migrations(id);
CREATE INDEX accountsavatars_account_url ON accountsavatars(account_url);
CREATE INDEX posts_account_id ON posts(account_id);
CREATE INDEX poststags_post_id ON poststags(post_id);
CREATE INDEX poststags_tag_url ON poststags(tag_url);
CREATE INDEX songs_post_url ON songs(post_url);
CREATE INDEX songsthumbnails_song_thumbnailUrl ON songsthumbnails(song_thumbnailUrl);`
}
];
}
@ -618,7 +650,6 @@ function getSongData(postIdsParams: string, postIds: string[]): Promise<Map<stri
},
new Map()
);
logger.verbose('songMap', songMap);
resolve(songMap);
}
);