fix(storage): optimize attachment retrieval by batching cipher IDs to improve performance

This commit is contained in:
shuaiplus
2026-02-19 01:42:55 +08:00
parent 69f4fde5a2
commit ba9710cdf0
+25 -19
View File
@@ -471,26 +471,32 @@ export class StorageService {
const grouped = new Map<string, Attachment[]>(); const grouped = new Map<string, Attachment[]>();
if (cipherIds.length === 0) return grouped; if (cipherIds.length === 0) return grouped;
const placeholders = cipherIds.map(() => '?').join(','); const uniqueCipherIds = [...new Set(cipherIds)];
const res = await this.db const chunkSize = LIMITS.performance.bulkMoveChunkSize;
.prepare(`SELECT id, cipher_id, file_name, size, size_name, key FROM attachments WHERE cipher_id IN (${placeholders})`)
.bind(...cipherIds)
.all<any>();
for (const row of (res.results || [])) { for (let i = 0; i < uniqueCipherIds.length; i += chunkSize) {
const item: Attachment = { const chunk = uniqueCipherIds.slice(i, i + chunkSize);
id: row.id, const placeholders = chunk.map(() => '?').join(',');
cipherId: row.cipher_id, const res = await this.db
fileName: row.file_name, .prepare(`SELECT id, cipher_id, file_name, size, size_name, key FROM attachments WHERE cipher_id IN (${placeholders})`)
size: row.size, .bind(...chunk)
sizeName: row.size_name, .all<any>();
key: row.key,
}; for (const row of (res.results || [])) {
const list = grouped.get(item.cipherId); const item: Attachment = {
if (list) { id: row.id,
list.push(item); cipherId: row.cipher_id,
} else { fileName: row.file_name,
grouped.set(item.cipherId, [item]); size: row.size,
sizeName: row.size_name,
key: row.key,
};
const list = grouped.get(item.cipherId);
if (list) {
list.push(item);
} else {
grouped.set(item.cipherId, [item]);
}
} }
} }