activerecord performance problem
The query below is making t开发者_开发技巧rouble 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
- Index the
created_at
column inentries
table - Index the
entry_id
column intraining_entries
table.
精彩评论