Ruby On Rails is slow…?

后端 未结 11 1816
说谎
说谎 2021-01-31 23:50

I\'m writing a web application to monitor a furniture factory production flow. It has thousand of data to handle. So far, I run RoR on Mongrel + MySQL and it\'s really really sl

11条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 00:19

    You might profile the code first before doing anything, though, queries inside for loops are a very common cause for performance problems and at first sight this seems your problem. You might anyway find a practical profiler here:

    As already said on the other answers, if both models are related you should eager load the associations, which implies instructing Active Record to perform join queries:

    #left outer join
    ofkbs=Ofkb.includes(:operation).where(name: "banana")
    

    If you do not need the ofkbs but only the operations, you could perform an inner join

    #inner join (discards the Ofkbs that do not have any operation)
    operations=Operation.joins(:ofkb).where(ofkb:{name:"banana"})
    

    This solution only preforms one query, and allows you to afterwards iterate through the data that will have already been collected from the DB:

    operations=ofkbs.map{|of| of.operations}.flatten
    
    operations.each do |o|
      do_whatever_you_want_with_operation(o)
    end
    

    If the queries are very complicated you should use arel instead.

提交回复
热议问题