Ruby on Rails Tuesday, December 16, 2014



On Monday, 15 December 2014 14:59:02 UTC-5, Ruby-Forum.com User wrote:
Hi,


I have around 2000 objects to render. Unfortunately the view rendering
with jbuilder takes very long time, although the objects are small like
this:


center: {lat: 48.8130685274225, lon: 10.0405494495457}
lat: 48.8130685274225
lon: 10.0405494495457
n: "Aalen/Hirtenteich"
st: null
sy: null


The result query I pass to the view looks like this
@resorts   = Resort.where(:id => resort_ids).includes(:snow_reports)

A resort has many snow_reports. st and sy are fields from snow reports.

the view looks like this:

   json.array!(@resorts) do |resort|
  json.cache!("resort_light_#{resort.id}") do

    #json.partial! 'json_partials/snow_in_resort', resort: resort
    json.n resort.name
    json.sy resort.try(:snow_reports).last.try(:snow_valley) if
resort.snow_reports

How many snow reports does a typical resort have? I ask because you're loading *all* of them with the `includes` above, but only using the last one. If you've got 2000 resorts and each one has 100 reports or so, that means instantiating 200000 SnowReport objects, and that is not going to be fast.

--Matt Jones
 
    json.st resort.try(:snow_reports).last.try(:snow_summit) if
resort.snow_reports
    json.center do
        json.lat resort.centroid.lat
        json.lon resort.centroid.lon
    end
  end
end


  Rendered resorts/find.json.jbuilder (1649.0ms)
Completed 200 OK in 1889ms (Views: 1593.1ms | ActiveRecord: 77.8ms |
Solr: 0.0ms)
In development mode the request takes 5s in chrome to run.

The first thing I ve done to speed up is changing the json gem to

      gem 'oj'
      gem 'oj_mimic_json'

It speeds up slightly but not enough. I also tried active model
serialization wich was about the same time, so I prefer to stick to
jbuilder since it is convenient to assemble json objects.
So how can I speed up rendering?

Help is highly appreciated since I am stuck for a week now.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2f7a3cb3-6779-41d6-9e66-fcf3a3ede17d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment