danke
danke

Reputation: 59

activerecord performance problem

The query below is making trouble in heroku. It takes too long to respond and sometimes it causes application error since heroku has 30 sec timeout limit. Any help will be appreciated.

@entries = Entry.where(:created_at => Time.now-2.day..Time.now).joins("left join training_entries on training_entries.entry_id = entries.id").where(:training_entries => { :entry_id => nil}).order('entries.created_at DESC')



  ←[1m←[36mEntry Load (523.0ms)←[0m  ←[1mSELECT `entries`.* FROM `entries` left join training_entries on training_entries.entry_id
 = entries.id WHERE `training_entries`.`entry_id` IS NULL
 AND (`entries`.`created_at` BETWEEN '2011-07-31 21:00:00' AND '2011-08-02 21:00:00') ORDER BY entries.created_at DESC LIMIT 1
0 OFFSET 0←[0m

Upvotes: 0

Views: 217

Answers (1)

Harish Shetty
Harish Shetty

Reputation: 64373

  • Index the created_at column in entries table
  • Index the entry_id column in training_entries table.

Upvotes: 2

Related Questions