Repository navigation
feat: Add indexes to unindexed foreign keys - #8245
Merged
david-yz-liu merged 2 commits intoOct 31, 2026
Merged
david-yz-liu merged 2 commits into
david-yz-liu merged 2 commits into
Conversation
`rails active_record_doctor:unindexed_foreign_keys` reports 22 columns on master: 17 foreign keys, and 5 _id columns without a constraint (groupings_tags.grouping_id, notes(noteable_type, noteable_id), test_runs.autotest_test_id, lti_deployments.lms_course_id and assignment_properties.remote_autotest_settings_id). Without an index, a lookup by one of these columns, and the foreign key check Postgres runs when a parent row is deleted, read the whole table. The migration adds an index on each column, after which the detector reports nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
Coverage Report for CI Build 41132411413Coverage remained the same at 91.063%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
david-yz-liu
approved these changes
Oct 31, 2026
david-yz-liu
left a comment
Collaborator
There was a problem hiding this comment.
Nice work, @YheChen! I especially appreciated the information on the timing evaluation; I don't think it's necessary to include that script in this repository, however.
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.
Proposed Changes
Adds an index on each of the 22 columns that
rails active_record_doctor:unindexed_foreign_keysreports on master. 17 are foreign keys, and 5 are_idcolumns without a constraint:groupings_tags.grouping_id,notes(noteable_type, noteable_id),test_runs.autotest_test_id,lti_deployments.lms_course_idandassignment_properties.remote_autotest_settings_id. Without an index, a lookup by one of these columns, and the foreign key check Postgres runs when a parent row is deleted, read the whole table.20261030000000_add_indexes_to_unindexed_foreign_keys.rb, with anadd_indexper column.notesgets one index on(noteable_type, noteable_id).db/structure.sqland the model annotations, as regenerated by the migration.Timings
I timed each column before and after on synthetic data in Postgres 17, with a warm cache:
SELECT * FROM table WHERE column = value, median of 15 values.EXPLAIN ANALYZE, median of 5.Sized like one 1,000-student CSC108 term. I estimated each table's size from how MarkUs creates its rows, assuming 40 TAs, 12 individual assignments, 3 marks spreadsheets and 4 test runs per student per assignment. Most flagged tables hold 100 rows or fewer.
test_runs.autotest_test_idannotations.result_idmemberships.grouping_idannotations.annotation_text_idgroupings.group_idgrades.grade_entry_student_idannotation_texts.creator_idgrade_entry_students.assessment_idTestRun#autotest_test_id_uniquenessfilterstest_runsbyautotest_test_idfor every test run MarkUs creates. So at the end of a term, a 1,000-group batch run spends about 2 s on these scans, versus about 0.03 s with the index.1,000,000 rows in every flagged table.
Full-table scans grow with the number of rows, while index lookups stay about the same, so an instance that holds several courses gains more than a single course does.
Screenshots of your changes (if applicable)
Type of Change
(Write an
Xor a brief description next to the type or types that best describe your changes.)Checklist
(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the
[ ]into a[x]in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)Before opening your pull request:
After opening your pull request:
Questions and Comments
active_record_doctor:unindexed_foreign_keysin CI, so new foreign keys get an index from the start.