Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant db indexes #4828

Merged
merged 1 commit into from
Dec 10, 2024

Conversation

coalest
Copy link
Collaborator

@coalest coalest commented Dec 4, 2024

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 by a and b or by a alone. (But not queries only involving b). 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 the user_roles table

Before:
You can see it is using the index only on user_id (index_users_roles_on_user_id).
Screenshot from 2024-12-04 14-57-17

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.
Screenshot from 2024-12-04 14-57-51

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.

@cielf cielf requested a review from dorner December 4, 2024 14:21
Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

Interesting - the users_roles table was created via a generator from Rolify. But yeah, they do seem redundant. Thanks!

@dorner dorner merged commit a31207a into rubyforgood:main Dec 10, 2024
11 checks passed
@coalest coalest deleted the remove-redundant-db-indexes branch December 10, 2024 11:15
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