0

それはstackoverflowの私の最初の質問です。

私のアプリケーションでは、Webの動きを確認するためのリアルタイムセクションがあります。そのオンライン新聞とこのセクションは、新しい記事、新しいコメント、新しい投票、新しい討論.....ウェブ上で起こっていることすべてを示しています。

私はこのコードを持っています:

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

few notes:

  1. find(:all) is deprecated. use where() instead.
  2. instead of doing 5 db calls migrate them into one db call (create a sql query for that if needed).
  3. make sure you index the dates columns in your tables.
  4. don't sort sort and reverse. instead replace y <=> x so it will sort the way you need.

hope it helps..

于 2012-05-28T19:49:48.780 に答える