通过Rails加载搜索结果的最轻量方法是什么?

时间:2011-02-17 15:36:10

标签: ruby-on-rails performance sunspot

这是一般策略的问题。我一直在为搜索结果页面加载时间,我正在使用sunspot_rails进行处理,我发现了一些奇怪的发现。

  1. 如果加载了其他内容,有时结果加载速度会更快。

  2. 我可以越多地避免连接表,我可以点击数据库。

  3. 我很好奇我是否也错过了其他一些好主食。这是一个搜索结果的示例,我正在解析项目所占用的平均加载时间,如果我将其隔离并自行加载。

    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)
    

1 个答案:

答案 0 :(得分:1)

可以加快速度的一种方法是预先计算

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

这样你的观点就是吐出文字/ html。你有一个before_save方法可以将上述内容放到另一个字段中。

但这只是一个猜测 - 看起来您的视图加载速度太慢,因此可能是的原因。