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

migration 6_add_missing_indexes_on_taggings redundant index? #1052

Open
akostadinov opened this issue Oct 18, 2021 · 2 comments
Open

migration 6_add_missing_indexes_on_taggings redundant index? #1052

akostadinov opened this issue Oct 18, 2021 · 2 comments

Comments

@akostadinov
Copy link

akostadinov commented Oct 18, 2021

In 6_add_missing_indexes_on_taggings.rb [1] I see two seemingly redundant indices created:

add_index ActsAsTaggableOn.taggings_table, :tagger_id
add_index ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]

Same goes for

add_index ActsAsTaggableOn.taggings_table, :taggable_id
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'

And there is one additional reduntant one from 2_add_missing_unique_indices.rb#L12:

    add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists? ActsAsTaggableOn.taggings_table, :tag_id
vs
    add_index ActsAsTaggableOn.taggings_table,
              [:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
              unique: true, name: 'taggings_idx'

Because the composite index starts with :tagger_id it seems that a separate index just on :tagger_id is probably redundant because DB engine can use the composite index when querying tagger_id [2]. Same as the other two cases.

Did any practical results showed that both indices are needed or is there any other reason to have them both?

P.S. The rollback of this migration does nothing because everything is in if condition. So when running rollback nothing is rolled back.

[1]

add_index ActsAsTaggableOn.taggings_table, :tagger_id unless index_exists? ActsAsTaggableOn.taggings_table, :tagger_id
add_index ActsAsTaggableOn.taggings_table, :context unless index_exists? ActsAsTaggableOn.taggings_table, :context
unless index_exists? ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]
add_index ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]

[2] https://user3141592.medium.com/single-vs-composite-indexes-in-relational-databases-58d0eb045cbe

@gerard76
Copy link
Contributor

gerard76 commented Nov 23, 2021

I came here wondering about the redundant indices as well. Both MySQL and Postgresql can use the composite indices for those.

@akostadinov akostadinov changed the title migration 6_add_missing_indexes_on_taggings redundant inxed? migration 6_add_missing_indexes_on_taggings redundant index? Nov 23, 2021
@fatkodima
Copy link
Contributor

is probably redundant because DB engine can use the composite index when querying

It is not "probably", it is for sure in every relational dbms.

These indexes are redundant. Unfortunately, this gem has some problems with correct/non-redundant/useful indexes and PRs involving adding new indexes should be better reviewed. And overall all existing indexes should be reviewed.

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

No branches or pull requests

3 participants