Skip to content

Commit 0b8a10a

Browse files
committed
fix: improve markAllAsRead performances
- make an index on unread+convoId - make the message update trigger only run when the body changed
1 parent e3006ae commit 0b8a10a

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

ts/data/data.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,16 @@ async function getUnreadByConversation(conversationId: string): Promise<MessageC
494494

495495
async function markAllAsReadByConversationNoExpiration(
496496
conversationId: string,
497-
returnMessagesUpdated: boolean // for performances reason we do not return them because usually they are not needed
497+
returnMessagesUpdated: boolean // for performance reason we do not return them because usually they are not needed
498498
): Promise<Array<number>> {
499+
// tslint:disable-next-line: no-console
500+
console.time('markAllAsReadByConversationNoExpiration');
499501
const messagesIds = await channels.markAllAsReadByConversationNoExpiration(
500502
conversationId,
501503
returnMessagesUpdated
502504
);
505+
// tslint:disable-next-line: no-console
506+
console.timeEnd('markAllAsReadByConversationNoExpiration');
503507
return messagesIds;
504508
}
505509

ts/node/database_utility.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ export function rebuildFtsTable(db: BetterSqlite3.Database) {
264264
CREATE TRIGGER messages_on_delete AFTER DELETE ON ${MESSAGES_TABLE} BEGIN
265265
DELETE FROM ${MESSAGES_FTS_TABLE} WHERE id = old.id;
266266
END;
267-
CREATE TRIGGER messages_on_update AFTER UPDATE ON ${MESSAGES_TABLE} BEGIN
267+
CREATE TRIGGER messages_on_update AFTER UPDATE ON ${MESSAGES_TABLE} WHEN new.body <> old.body BEGIN
268268
DELETE FROM ${MESSAGES_FTS_TABLE} WHERE id = old.id;
269269
INSERT INTO ${MESSAGES_FTS_TABLE}(
270270
id,

ts/node/migration/sessionMigrations.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const LOKI_SCHEMA_VERSIONS = [
7878
updateToSessionSchemaVersion26,
7979
updateToSessionSchemaVersion27,
8080
updateToSessionSchemaVersion28,
81+
updateToSessionSchemaVersion29,
8182
];
8283

8384
function updateToSessionSchemaVersion1(currentVersion: number, db: BetterSqlite3.Database) {
@@ -1181,6 +1182,28 @@ function updateToSessionSchemaVersion28(currentVersion: number, db: BetterSqlite
11811182
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
11821183
}
11831184

1185+
function updateToSessionSchemaVersion29(currentVersion: number, db: BetterSqlite3.Database) {
1186+
const targetVersion = 29;
1187+
if (currentVersion >= targetVersion) {
1188+
return;
1189+
}
1190+
1191+
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
1192+
1193+
db.transaction(() => {
1194+
dropFtsAndTriggers(db);
1195+
db.exec(`CREATE INDEX messages_unread_by_conversation ON ${MESSAGES_TABLE} (
1196+
unread,
1197+
conversationId
1198+
);`);
1199+
rebuildFtsTable(db);
1200+
// Keeping this empty migration because some people updated to this already, even if it is not needed anymore
1201+
writeSessionSchemaVersion(targetVersion, db);
1202+
})();
1203+
1204+
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
1205+
}
1206+
11841207
// function printTableColumns(table: string, db: BetterSqlite3.Database) {
11851208
// console.info(db.pragma(`table_info('${table}');`));
11861209
// }

0 commit comments

Comments
 (0)