[英]What is the lightest way to load search results via Rails?
This is a question of general strategy. 这是一个总体战略问题。 I've been pinging load time for a search results page I'm doctoring with sunspot_rails, and I've noticed some strange discoveries. 我一直在为Sunspot_rails调整搜索结果页面的加载时间,并且发现了一些奇怪的发现。
Sometimes results load faster, if other things are loaded with them. 有时,如果结果加载得更快,结果加载速度会更快。
The more I can avoid join tables, the lighter I can tap the database. 我越能避免联接表,就越轻松地利用数据库。
I'm curious if I'm missing some other great staples as well. 我很好奇我是否也想念其他一些好主食。 Here's an example of a search result I'm parsing with comments on the average load time that item takes if I isolate it and load it by itself. 这是一个搜索结果的示例,如果要隔离并单独加载该项目,我将对该项目的平均加载时间进行注释。
All things Commented out = Completed in 554ms (View: 487, DB: 16)
All things loaded = Completed in 9093ms (View: 9008, DB: 37)
The Search Image 搜索图片
.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)
The Basic Info 基本信息
.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)
Icons 图示
.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
.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
.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
- 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)
About Text 关于文字
.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)
One way you can speed things up is to pre-calculate the results of 加快速度的一种方法是预先计算结果
truncate(sanitize(simple_format(result.about_us), :tags => ''), :length => 100).titleize
That way your view is just spitting out text/html. 这样,您的视图只会吐出text / html。 You'd have a before_save method that does the above into another field. 您将拥有一个before_save方法,可以将上述操作用于其他字段。
But that's just a guess -- it does seem that your views are loading too slowly, so this might be the cause. 但这只是一个猜测-看来您的视图加载速度太慢,所以这可能是原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.