Skip to content

perf: add indexes for card relation queries - #566

Merged
hjball merged 1 commit into
kanbn:mainfrom
habralab:perf/card-relation-indexes
Sep 1, 2026
Merged

perf: add indexes for card relation queries#566
hjball merged 1 commit into
kanbn:mainfrom
habralab:perf/card-relation-indexes

Conversation

@bionicman

Copy link
Copy Markdown
Contributor

Description

Board and card relation loading repeatedly filters child tables by their foreign key and, for ordered relations, sorts by the relation's ordering column. These tables did not have matching indexes, so PostgreSQL performed repeated sequential scans as the dataset grew.

This adds indexes for:

  • card comments by card
  • card attachments by card and creation time
  • card checklists by card and position
  • checklist items by checklist and position
  • card activity by card and creation time

The schema declarations and generated Drizzle migration are included in the same change.

The issue was reproduced on a real Trello import containing 38 boards and 14,524 cards. The indexed relation tables contained 53,032 comments, 17,384 attachments, 2,221 checklists, 12,613 checklist items, and 67,556 activity rows. The measured board contained 3,236 stored cards across 45 lists; its normal API response included 709 active cards and was approximately 692 KB.

On that dataset, the database portion of the large board query dropped from about 2.9 seconds to 17–29 ms. End-to-end board loading dropped from about 13.1 seconds to 0.35–0.81 seconds. PostgreSQL also selected all five new indexes during normal application traffic after migration.

Type of change

  • Bug fix
  • Feature (requires an approved issue — see below)
  • Refactor / chore
  • Documentation

Checklist

  • I have linked the related issue below
  • My code follows the existing style and conventions
  • I have tested my changes locally
  • I have included screenshots for any UI changes

Testing

  • git diff --check upstream/main...HEAD
  • pnpm --filter @kan/db typecheck
  • generated migration applied successfully to PostgreSQL
  • verified all five indexes are valid, ready, and used by application queries

Linked issue

Not applicable: this is a database performance bug fix and does not change product behavior.

Add FK-first indexes for card comments, attachments, checklists,
checklist items, and activities to avoid repeated sequential scans
in board and card relational queries.
@bionicman
bionicman marked this pull request as ready for review August 31, 2026 23:12

@hjball hjball left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff @bionicman

@hjball
hjball merged commit 294b87f into kanbn:main Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants