user559555
user559555

Reputation: 71

Rails Indexing has_many_through, how to do it?

My app will have quite large join tables. From googling it seems that indexing is the answer to my prayers, though I haven't really found anything that explains HOW to technically do it for rails.

For simplicity I'm using Parent, ParenChild (join) and Child. Now the ParentChild table is the one that is growing quite big, searching through the whole of ParentChild is starting to take some time.

Which of my migration files do I need to edit in order to get the indexing done, how to do it for this example? AND is editing the migration file the only thing I'll have to do?

Upvotes: 0

Views: 63

Answers (1)

jdc
jdc

Reputation: 743

You use add_index from any migration you want. So, for example, if you want to add an index on both parent_id and child_id, you'd do this:

add_index :parent_child, [:parent_id, :child_id]

Which columns you index and whether they're in the same index or separate ones depend on your queries.

Upvotes: 3

Related Questions