As shown in this question:
How do I make a column unique and index it in a Ruby on Rails migration?
You can write:
add_index :the_table, [:foo_column, :bar_column], unique: true
to add an multiple column index.
But is it still required to add an single indexes for each of those columns that you already have specified a multi-column index?
I mean something like writing below code in additional to the code shown above.
add_index :the_table, :foo_column
add_index :the_table, :bar_column
For MySQL :
MySQL will be able to use the index [:foo_column, :bar_column] to query for conditions on both columns, and also for conditions on the left column only, but NOT the right column.
More info here : http://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html
So you should do
To make sure you index everything properly
MySQL indexes columns left-to-right so if you have a multi-column index like this :
[:col1, :col2, :col3, :col4], you can query this index on :So you can query the left-most columns
If you need anything else, you’ll have to create more indexes
Again, that’s only for MySQL, postgres may work differently