techdreams
techdreams

Reputation: 5583

add_index for a single column twice rails migration active record

I have to run a query where a single column is called twice .

Transaction.where("datetime >= ? && datetime <= ?", params[:date_one], :params[:date_two])

Now while indexing this is the basic we do

add_index :transactions, :datetime

Now my question is can I do something like.....

add_index :transactions, [:datetime, :datetime]

Will it really speedup the search or benefit performance wise. Thanks in advance

Upvotes: 0

Views: 155

Answers (1)

IvanSelivanov
IvanSelivanov

Reputation: 760

You don't have to do that. adding index to column speeds up queries to that column. It does not matter how many times you use this column in your query, only the presence or absence of index matters. Also, you can rewrite your query like this:

   Transaction.where("datetime BETWEEN ? AND ?", params[:date_one], :params[:date_two])

Upvotes: 3

Related Questions