Remove includes user from stories query to reduce num queries and memory (#2662)

* Add profile_image_90 to serialized data on article

* Remove includes user from stories query
This commit is contained in:
Ben Halpern 2019-05-02 15:58:58 -04:00 committed by GitHub
parent f12f00decd
commit 5e64ae874e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 25 additions and 36 deletions

View file

@ -8,6 +8,12 @@ function initializeFetchFollowedArticles() {
function insertArticle(article,parent,insertPlace) {
if (article){
var newItem = document.createElement("DIV");
if (article.cached_user) {
article.user = article.cached_user.table
}
if (article.cached_organization) {
article.organization = article.cached_organization.table
}
newItem.innerHTML = buildArticleHTML(article)
parent.insertBefore(newItem, insertPlace);
initializeReadingListIcons();

View file

@ -79,7 +79,7 @@ class StoriesController < ApplicationController
@tag = params[:tag].downcase
@page = (params[:page] || 1).to_i
@tag_model = Tag.find_by(name: @tag) || not_found
@moderators = User.with_role(:tag_moderator, @tag_model)
@moderators = User.with_role(:tag_moderator, @tag_model).select(:username, :profile_image, :id)
if @tag_model.alias_for.present?
redirect_to "/t/#{@tag_model.alias_for}"
return
@ -140,7 +140,6 @@ class StoriesController < ApplicationController
@user = @organization
@stories = ArticleDecorator.decorate_collection(@organization.articles.published.
limited_column_select.
includes(:user).
order("published_at DESC").page(@page).per(8))
@article_index = true
@organization_article_index = true
@ -252,7 +251,7 @@ class StoriesController < ApplicationController
def article_finder(num_articles)
tag = params[:tag]
articles = Article.published.includes(:user).limited_column_select.page(@page).per(num_articles)
articles = Article.published.limited_column_select.page(@page).per(num_articles)
articles = articles.cached_tagged_with(tag) if tag.present? # More efficient than tagged_with
articles
end

View file

@ -86,7 +86,7 @@ class Article < ApplicationRecord
:main_image, :main_image_background_hex_color, :updated_at, :slug,
:video, :user_id, :organization_id, :video_source_url, :video_code,
:video_thumbnail_url, :video_closed_caption_track_url, :language,
:experience_level_rating, :experience_level_rating_distribution,
:experience_level_rating, :experience_level_rating_distribution, :cached_user, :cached_organization,
:published_at, :crossposted_at, :boost_states, :description, :reading_time, :video_duration_in_seconds)
}
@ -548,6 +548,7 @@ class Article < ApplicationRecord
profile_image_90: organization.profile_image_90,
profile_image_url: organization.profile_image_url
}
self.cached_organization = OpenStruct.new(cached_org_object)
end
cached_user_object = nil
if user
@ -558,9 +559,8 @@ class Article < ApplicationRecord
profile_image_90: user.profile_image_90,
profile_image_url: user.profile_image_url
}
self.cached_user = OpenStruct.new(cached_user_object)
end
self.cached_user = OpenStruct.new(cached_user_object)
self.cached_organization = OpenStruct.new(cached_org_object)
end
def set_all_dates

View file

@ -1,5 +1,5 @@
<div class="container show-page-content-display"
data-details="<%= article.organization&.slug %>__<%= article.slug %>"
data-details="<%= article.cached_organization&.slug %>__<%= article.slug %>"
id="<%= classification == "boosted" ? "partner-content-display" : "classic_article_#{article.id}" %>">
<div class="content-classification">
<span class="content-classification-text"><%= classification_text %></span>
@ -8,7 +8,7 @@
<%= render "additional_content_boxes/article_content_area",
article: article,
classification: classification,
organization: article.organization %>
organization: article.cached_organization %>
</div>
<div class="secondary-content-display">
<%= render "additional_content_boxes/article_followable_area",

View file

@ -5,23 +5,18 @@
<img src="<%= asset_path("video-camera.svg") %>" /><%= story.video_duration_in_minutes %></div>
</a>
<% end %>
<% if story.organization && !@organization_article_index %>
<% if story.cached_organization && !@organization_article_index %>
<div class="article-organization-headline">
<a class="org-headline-filler" href="/<%= story.organization.slug %>">
<a class="org-headline-filler" href="/<%= story.cached_organization.slug %>">
<span class="article-organization-headline-inner">
<img alt="<%= story.organization.name %> logo" src="<%= story.organization.profile_image_90 %>">
<%= story.organization.name %>
<img alt="<%= story.cached_organization.name %> logo" src="<%= story.cached_organization.profile_image_90 %>"><%= story.cached_organization.name %>
</span>
</a>
</div>
<% end %>
<a href="<%= story.user.path %>" class="small-pic-link-wrapper">
<a href="/<%= story.cached_user.username %>" class="small-pic-link-wrapper">
<div class="small-pic">
<% if story.cached_tag_list_array.include?("hiring") && story.organization_id.present? && !@organization_article_index %>
<img src="<%= ProfileImage.new(story.organization).get(90) %>" alt="<%= story.organization.username %> profile" />
<% else %>
<img src="<%= ProfileImage.new(story.user).get(90) %>" alt="<%= story.user.username %> profile" />
<% end %>
<img src="<%= ProfileImage.new(story.cached_user).get(90) %>" alt="<%= story.cached_user.username %> profile" />
</div>
</a>
<a href="<%= story.path %>" class="small-pic-link-wrapper index-article-link" data-preload-image="<%= cloud_cover_url(story.main_image) %>" id="article-link-<%= story.id %>">
@ -32,7 +27,7 @@
</div>
</a>
<h4>
<a href="<%= story.user.path %>"><%= story.user.name %>・<%= story.readable_publish_date %><span class="time-ago-indicator-initial-placeholder" data-seconds="<%= story.published_at_int %>"></span></a>
<a href="/<%= story.cached_user.username %>"><%= story.cached_user.name %>・<%= story.readable_publish_date %><span class="time-ago-indicator-initial-placeholder" data-seconds="<%= story.published_at_int %>"></span></a>
</h4>
<div class="tags">

View file

@ -1,7 +1,4 @@
<% flare_tag = FlareTag.new(story, @tag).tag %>
<% if flare_tag %>
<span class="tag-identifier black-identifier" style="background:<%= flare_tag.bg_color_hex %>;color:<%= flare_tag.text_color_hex %>">
#<%= flare_tag.name %>
</span>
<% end %>
<%= story.title %>
#<%= flare_tag.name %></span><% end %><%= story.title %>

View file

@ -95,10 +95,10 @@
<%= @featured_story.title %>
</h3>
</div>
<a href="<%= @featured_story.user.path %>" class="featured-profile-button">
<img class="featured-profile-pic" src="<%= ProfileImage.new(@featured_story.user).get(90) %>" alt="<%= @featured_story.title %>" />
<a href="/<%= @featured_story.cached_user.username %>" class="featured-profile-button">
<img class="featured-profile-pic" src="<%= ProfileImage.new(@featured_story.cached_user).get(90) %>" alt="<%= @featured_story.title %>" />
</a>
<div class="featured-user-name"><a href="<%= @featured_story.user.path %>"><%= @featured_story.user.name %>
<div class="featured-user-name"><a href="/<%= @featured_story.cached_user.username %>"><%= @featured_story.cached_user.name %>
・<%= @featured_story.readable_publish_date %><span class="time-ago-indicator-initial-placeholder" data-seconds="<%= @featured_story.published_at_int %>"></span></a></div>
<div class="featured-tags tags">
<% @featured_story.cached_tag_list_array.each do |tag| %>

View file

@ -11,26 +11,18 @@
<%= @new_stories.to_json(
only: %i[title path id user_id comments_count positive_reactions_count organization_id
reading_time video_thumbnail_url video video_duration_in_minutes language
experience_level_rating experience_level_rating_distribution],
experience_level_rating experience_level_rating_distribution cached_user cached_organization],
methods: %i[readable_publish_date cached_tag_list_array flare_tag class_name
cloudinary_video_url video_duration_in_minutes published_at_int],
include: {
user: { only: %i[username name], methods: [:profile_image_90] },
organization: { only: %i[slug name], methods: [:profile_image_90] }
},
) %>">
</div>
<div id="home-articles-object" data-articles="
<%= @stories.to_json(
only: %i[title path id user_id comments_count positive_reactions_count organization_id
reading_time video_thumbnail_url video video_duration_in_minutes language
experience_level_rating experience_level_rating_distribution],
experience_level_rating experience_level_rating_distribution cached_user cached_organization],
methods: %i[readable_publish_date cached_tag_list_array flare_tag class_name
cloudinary_video_url video_duration_in_minutes published_at_int],
include: {
user: { only: %i[username name], methods: [:profile_image_90] },
organization: { only: %i[slug name], methods: [:profile_image_90] }
},
) %>">
<% 3.times do %>
<div class="single-article single-article-small-pic">