Should I joint-index an ActiveRecord polymorphic association?

半城伤御伤魂 提交于 2019-12-03 11:53:41

Ultimately, this is worth benchmarking before and after adding the index, on a realistic data set - realistic in size and data.

However, you're not creating an index on a field with just a few values. The index is on the combination of the two fields, which is likely to have a lot of different value combinations. The index on the combined fields is a smart idea.

It is best practice to put the most selective field first when creating an index on multiple fields. Since you only have 4-5 values of commentable_type, you would be better off doing:

add_index :comments, [:commentable_id, :commentable_type]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!