Skip to content

perf(db): add indexes on foreign key columns - #248

Open
markcrivera wants to merge 1 commit into
mainfrom
perf/fk-indexes
Open

perf(db): add indexes on foreign key columns#248
markcrivera wants to merge 1 commit into
mainfrom
perf/fk-indexes

Conversation

@markcrivera

Copy link
Copy Markdown
Collaborator

Summary

No foreign key column in the schema has an index. Sequelize does not create them automatically on Postgres so cascaded deletes and per-system aggregate queries degrade to full-table scans as AssessmentItems grows. This adds a migration creating nine indexes on the hot FK columns, which resolves the customer-reported multi-minute system deletes, multi-hour boundary deletes, and postgres pinned at 100% CPU.

Changes

  • New migration 2026.08.19T14.45.00.AddForeignKeyIndexes.js adding indexes on:
    • AssessmentItems: AssessmentId, previousId, StigDatumId
    • Assessments: SystemId, StigId, succeededByAssessmentId
    • EvaluationItems: EvaluationId, StigDatumId
    • Stig_Systems: SystemId

Testing

Measured on a VM matching the affected deployment (4 cores, 16GB, postgres 15.1) seeded with 1000 systems, 16,000 STIG assignments, and 792k assessment items carrying realistic finding data:

  • Delete one system: 34.9s before, 0.128s after
  • Boundary page (systems/list) with 400 systems: 93.2s before, 2.9s after
  • Boundary page (systems/list) with 25 systems: 5.47s before, 0.22s after

Index build took about 13 seconds total on that dataset and added 52MB to AssessmentItems. Migration verified with full chain runs on fresh databases: postgres up, down, and re-up, and sqlite up.

Related

No foreign key column in the schema has an index, so cascaded
deletes and per-system aggregates degrade to full-table scans as
AssessmentItems grows. Postgres validates the self-referential
AssessmentItems.previousId constraint with a sequential scan for
every row deleted, which makes single-system deletes take minutes
and boundary deletes hours at scale.

Signed-off-by: Mark Rivera <mcrivera@gmail.com>
@Amndeep7
Amndeep7 temporarily deployed to tir-perf-fk-indexes-hrrl7vntmu August 22, 2026 01:51 Inactive
@sonarqubecloud

Copy link
Copy Markdown

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