I have a table orders on an MS SQL 2008R2 server
orders has a primary key of id
orders has a column called ordering_date that is type datetime
I created an index of column ordering_date called ordering_date_ndx on the server using a snippet of SQL. I did try to use a migration, but running the migration timed-out on this table of 23 million records.
2 Questions:
- What, if anything, should I put in app/models/order.rb to make use
of the index. - Will code like this make use of the presence of the index and optimize the SQL query?
list=Orders.find(ordering_date.year == 2006)
Thanks!
Afaik Rails doesn’t care about indexes on databases. There is no query optimization on the basis of indexes in Rails. If you do sorting or filtering on a column (especially on big tables) it’s helpful to add indexes. This helps your database server to do these operations more effective.
However the better approach to add an index is to let Rails do it via migrations than doing it manually. It could be if a SQL snipped as well. This ensures that the index is in place on all environments and servers.
Why this takes to long in Rails I have no idea. But you can run SQL in a migration:
I don’t know about MS SQL syntax but I guess it can be adapted.