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
+8 -2
View File
@@ -471,10 +471,15 @@ 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 chunkSize = LIMITS.performance.bulkMoveChunkSize;
for (let i = 0; i < uniqueCipherIds.length; i += chunkSize) {
const chunk = uniqueCipherIds.slice(i, i + chunkSize);
const placeholders = chunk.map(() => '?').join(',');
const res = await this.db const res = await this.db
.prepare(`SELECT id, cipher_id, file_name, size, size_name, key FROM attachments WHERE cipher_id IN (${placeholders})`) .prepare(`SELECT id, cipher_id, file_name, size, size_name, key FROM attachments WHERE cipher_id IN (${placeholders})`)
.bind(...cipherIds) .bind(...chunk)
.all<any>(); .all<any>();
for (const row of (res.results || [])) { for (const row of (res.results || [])) {
@@ -493,6 +498,7 @@ export class StorageService {
grouped.set(item.cipherId, [item]); grouped.set(item.cipherId, [item]);
} }
} }
}
return grouped; return grouped;
} }