2011-02-17 21 views
1

這是一般策略的問題。我一直在尋找一個搜索結果頁面的加載時間,我正在用sunspot_rails進行處理,我注意到了一些奇怪的發現。通過Rails加載搜索結果的最輕量級方式是什麼?

  1. 如果其他東西與它們一起加載,有時結果加載速度會更快。

  2. 越能避免加入表格,我可以輕點數據庫。

我很好奇,如果我錯過了一些其他偉大的主食以及。下面是我解析的搜索結果的一個示例,其中包含項目在將其隔離並自行加載時的平均加載時間的評論。

All things Commented out = Completed in 554ms (View: 487, DB: 16) 
All things loaded = Completed in 9093ms (View: 9008, DB: 37) 

搜索圖像

.left.search_image_holder 
    - if result.search_image.exists? 
    = image_tag result.search_image.url(:thumb) 
    - elsif result.logo.exists? 
    = image_tag result.logo.url(:thumb) 
    - else 
    = image_tag 'search_image_default.jpg' 

Completed in 5791ms (View: 5728, DB: 10) 

的基本信息

.grid_3.omega 
    %h1< 
    = link_to truncate(result.name.titleize, :length => 30), organization_path(result.hq_url.blank? ? result : result.hq_url), :title => "Find out more about #{result.name} in #{result.city}" 
    .clear 
    %h3< 
    =h result.city.titleize 
    .clear 
    .class7 
    =h truncate(result.quick_description.titleize, :length => 60) 
    .clear 

Completed in 4158ms (View: 3979, DB: 16) 

圖標

.grid_4.omega.alpha 
    .left{:style => 'margin-right: 12px; width: 40px'} 
    - if result.contact_24 
     = link_to image_tag('24hr-icon.png'), contact_organization_path(result), :title => "This local business or organization guarentees tocontact you within 24 hours" 
    - else 
     &nbsp; 
    .left{:style => 'margin-right: 12px; width: 40px'} 
    - if result.deals.count > 0 
     = link_to image_tag('hq-card-icon.png'), view_organization_deals_path(result), :title => "This local business or organization features promotions, deals, or steals" 
    - else 
     &nbsp; 
    .left{:style => 'margin-right: 12px; width: 40px'} 
    - if result.video_count > 0 
     = link_to image_tag('videos-icon.png'), organization_path(result.hq_url.blank? ? result : result.hq_url, :show_video => true), :title => "This local business or organization features a video" 
    - else 
     &nbsp; 
    - if result.reviews.count > 0 
    %a{:href => organization_reviews_path(result)} 
     .left.star_rating_icon 
     = result.rating 
    .clear 

Completed in 4125ms (View: 3929, DB: 36) 

關於文本

.grid_4.omega.alpha{:style => 'height: 25px; overflow: hidden;'} 
    %p< 
    = truncate(sanitize(simple_format(result.about_us), :tags => ''), :length => 100).titleize 

Completed in 4500ms (View: 4311, DB: 12) 
+0

一方面,您使用haml和網格佈局。但爲什麼內聯樣式? – corroded 2011-02-17 15:40:23

回答

1

一個可以加快速度的方法是預先計算的

truncate(sanitize(simple_format(result.about_us), :tags => ''), :length => 100).titleize 

這樣,你的看法是剛剛吐出text/html的結果。你會有一個before_save方法將上述內容放入另一個字段。

但這只是一個猜測 - 它看起來似乎是你的意見加載得太慢,所以這可能是是原因。

相關問題