From dddb3129ccf26af8c8c3a7d1d268d6c35fe82a93 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Tue, 5 Oct 2021 09:36:07 -0700 Subject: [PATCH] Optimize `removeKnownAttachments` --- ts/sql/Server.ts | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index 39e662f5ea..8f2463366c 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -6182,7 +6182,7 @@ async function removeKnownAttachments( const lookup: Dictionary = fromPairs( map(allAttachments, file => [file, true]) ); - const chunkSize = 50; + const chunkSize = 500; const total = await getMessageCount(); logger.info( @@ -6193,20 +6193,20 @@ async function removeKnownAttachments( let complete = false; let id: string | number = ''; + const fetchMessages = db.prepare( + ` + SELECT json FROM messages + WHERE id > $id + ORDER BY id ASC + LIMIT $chunkSize; + ` + ); + while (!complete) { - const rows: JSONRows = db - .prepare( - ` - SELECT json FROM messages - WHERE id > $id - ORDER BY id ASC - LIMIT $chunkSize; - ` - ) - .all({ - id, - chunkSize, - }); + const rows: JSONRows = fetchMessages.all({ + id, + chunkSize, + }); const messages: Array = rows.map(row => jsonToObject(row.json) @@ -6239,20 +6239,20 @@ async function removeKnownAttachments( `removeKnownAttachments: About to iterate through ${conversationTotal} conversations` ); + const fetchConversations = db.prepare( + ` + SELECT json FROM conversations + WHERE id > $id + ORDER BY id ASC + LIMIT $chunkSize; + ` + ); + while (!complete) { - const rows = db - .prepare( - ` - SELECT json FROM conversations - WHERE id > $id - ORDER BY id ASC - LIMIT $chunkSize; - ` - ) - .all({ - id, - chunkSize, - }); + const rows = fetchConversations.all({ + id, + chunkSize, + }); const conversations: Array = map(rows, row => jsonToObject(row.json)