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.
Doesn't resolve any issue.
Description
While familiarizing myself with the database schema, I noticed we have some indexes that are redundant.
Context: Composite indexes are able to be used when query conditions include fields involving the leftmost fields. So an index on
(a, b)
can be used for querying bya
andb
or bya
alone. (But not queries only involvingb
). We currently have a few indexs on both(a, b)
and(a)
, so this PR is to remove the indexes on(a)
leaving only the composite index.Why remove these redundant indexes? It's not a priority but they do take up storage space in the database and they have a performance impact on write operations (because whenever those columns are updated, the indexes also need to be updated).
Type of change
Technical debt?
How Has This Been Tested?
See screenshots below.
Screenshots
Example before and after for removing the index on
user_id
from theuser_roles
tableBefore:
You can see it is using the index only on
user_id
(index_users_roles_on_user_id
).After:
That index no longer exists after the migration, but it is still able to perform an index scan by using the compound index on
user_id, role_id
.Note: If you want to test this locally, you might have to insert rows into the database because when the table is too small postgres sometimes chooses to use a sequential scan.