perf(db): add indexes on foreign key columns - #248
Open
markcrivera wants to merge 1 commit into
Open
Conversation
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>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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
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:
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