0

I have a 10,000+ row table that I am trying to query in Rails 4. Let's call it events and there are many, many years of past events still in the database. I need to grab only the events with a date of today or in the future. I could do something like:

@events = Event.where('date >= ?', todays_date)

but this still takes a long time as it has to go through every record. Is there a simple way to speed this up? I've even considered that starting from the most recent rows, moving backwards, and limiting to say 1,000 total would be "good enough", although it could potentially leave out an event created a long time ago but with a date in the near future. I'm not even sure how to go about starting from the last row, however. And I feel like there has to be a better solution anyhow.

Any ideas? Thank you in advance!

backpackerhh
  • 2,333
  • 1
  • 18
  • 21

2 Answers2

4

One thing you can do to speed up the search is to add index to the created_at field (or to the custom date field you are using). Index makes the searching process faster in the database.

You can refer this answer to add index.

Community
  • 1
  • 1
Yedhu Krishnan
  • 1,225
  • 15
  • 31
3

It sounds like adding an index to your date column could help with efficiency here.

Migration code below:

class AddIndexToEvents < ActiveRecord::Migration
  def change
    add_index :events, :date
  end
end
hp4k
  • 356
  • 2
  • 9