0

I am using rails, mongoid with mongohq on heroku to develop my site.

However, I got very serious performance issue.

I queried on each user page, it cost more than 2 seconds to load all the data The data are not that much, only like 30 musics and each music could have about 1 to 3 different styles.

Anyone knows how to improve the performance?

I have already tried a tons of methods but still not working and mongo shows each individual query is very fast.

Here's the code:

     @musics = []           #get all user downloaded musics
     @uploaders = []        #record uploader of each music
     @styles = []           #record the styles of each music
     @user.mydownloads.each do |download_music|
        music = Music.find(download_music)                   #find the music by id
        @musics.insert(0, music)        
        uploader = music.user                                #find the uploader
        @uploaders.insert(0,uploader)
        @styles.insert(0,shortstyle(music.music_styles,10))  
        #shortstyle is a helper function in application helper to make the stylelooks nicer
     end

If all the data are required, how can i improve the performance?

Thank you guys very very much!

1 Answers1

0

What are your indexes?

Also, how many items will be in @user.mydownloads?

This will cause an N+1 query, which could be very bad if @user.mydownloads is large(bigger than 1 or 2 items, see this SO thread)

Community
  • 1
  • 1
Yan
  • 56
  • 1
  • 2
  • 5
  • Thanks for your help! I have index on id of each model. There are about 30 items in the user downloands. – MrCellophane Mar 14 '14 at 01:12
  • I tried to do eager loading on music styles, but rails would say that this is a many to many reference, so eager loading is not allowed..... – MrCellophane Mar 14 '14 at 01:13