html - Rendering many partials in rails (500+) is slow -
i made partial of song model artist
, title
, duration
, release
etc.
i want show artists songs on 1 page, have ton. query takes 0.08 seconds, see this:
rendered artist/_song_listing.html.haml (18.2ms) rendered artist/_song_listing.html.haml (8.5ms) rendered artist/_song_listing.html.haml (7.5ms) rendered artist/_song_listing.html.haml (9.6ms) rendered artist/_song_listing.html.haml (18.6ms) rendered artist/_song_listing.html.haml (12.6ms) rendered artist/_song_listing.html.haml (7.8ms) rendered artist/_song_listing.html.haml (19.5ms)
(etc etc)
ultimately takes 14 seconds, way long.
is wrong use partials in way? there way make faster?
edit:
artistcontroller:
def show @artist = artist.find(params[:id]) @songs = @artist.songs.order('published_at desc').includes(:members)
show.html.haml:
.panel.panel-default - @artist.songs.each |song| =render 'song_listing', song: song
exerpt _song_listing
(~500 times):
.row .col-xs-12 .summary -if song.summary.present? ="#{song.summary.sanitize.strip.gsub("\u00a0"," ")}" -else %i no summary .row .col-xs-10 %ul.list-inline - song.members.each |member| %li %span %img{src:"#{member.image_url}", width: '20px', height: '20px', class: 'img-circle'}
is load coming sql queries? try pre-loading in controller.
@songs = song.all.includes(:artist,:title)
then
@songs.each |song| # render logic here each song end
in includes put related associations, not loaded automatically.
this should make each partial rendering faster, if sql queries problem.
further instructions include first up:
@artist = artist.find(params[:id]).includes(:songs => [:members]) @songs = @artist.songs.order('published_at desc')
i believe trick you. had earlier still ok. not sure if brackets around ":members" required, saying, include artist's songs, , each song include it's members.
Comments
Post a Comment