0

这是我在stackoverflow中的第一个问题。

在我的应用程序中,我有一个实时部分,可以查看网络的运动。它是一份在线报纸,该部分显示:新文章、新评论、新投票、新辩论……所有正在网络上发生的事情。

我有这个代码:

def index
    @objects = Article.find(:all, :conditions => { :created_at => (Time.now-48.hours)..(Time.now), :published => true })
    @objects = @objects + Debate.find(:all, :conditions => { :created_at => (Time.now-48.hours)..(Time.now), :active => true })
    @objects = @objects + Event.find(:all, :conditions => { :created_at => (Time.now-48.hours)..(Time.now), :active => true })

    @objects = @objects + Comment.find(:all, :conditions => {  :created_at => (Time.now-48.hours)..(Time.now), :active => true })
    @objects = @objects + Vote.find(:all, :conditions => {  :created_at => (Time.now-48.hours)..(Time.now) })

    @objects.sort! {|x,z| x.created_at <=> z.created_at}
    @objects.reverse!

end

我在过去 48 小时内加载了所有内容。我一直在阅读有关在 Rails 中缓存的信息,因为我认为这不是我的解决方案。

我该怎么做才能更快地加载此列表?因为现在需要 7... 或 8 秒...

谢谢大家 :D

4

1 回答 1

0

几点注意事项:

  1. find(:all)弃用。改为使用where()
  2. 而不是进行 5 个 db 调用,而是将它们迁移到一个 db 调用中(如果需要,为此创建一个 sql 查询)。
  3. 确保索引表中的日期列。
  4. 不要排序排序和反向。而是替换y <=> x,这样它就会按照您需要的方式进行排序。

希望能帮助到你..

于 2012-05-28T19:49:48.780 回答