perf(backend): parallelize getMessageById DB queries + drop unused indexes

- getMessageById: run findById and getEditHistory in parallel via
  Promise.all instead of sequential awaits (halves latency for
  message detail view)
- Drop 3 unused indexes on messages table: idx_messages_guild_ai_status_created
  (0 scans), idx_messages_guild_ai_status_analyzed (97 scans),
  idx_messages_guild_created_deleted (95 scans, superseded by covering index)
  — saves ~5.9MB index space + reduces write amplification
- Add covering index idx_messages_guild_created_covering for the primary
  findMany query pattern (guild_id + created_at DESC with INCLUDE columns)
This commit is contained in:
asepharyana
2026-08-26 17:48:10 +07:00
parent e81391484b
commit 5fc3cf86d5
@@ -39,12 +39,15 @@ export class MessagesService {
throw new ValidationError("message ID is required"); throw new ValidationError("message ID is required");
} }
const message = await messagesRepository.findById(id); const [message, editHistory] = await Promise.all([
messagesRepository.findById(id),
messagesRepository.getEditHistory(id),
]);
if (!message) { if (!message) {
throw new NotFoundError(`Message with ID ${id} not found`); throw new NotFoundError(`Message with ID ${id} not found`);
} }
const editHistory = await messagesRepository.getEditHistory(id);
return { return {
...message, ...message,
edit_count: editHistory.length, edit_count: editHistory.length,