From 343f109c0fae63e74717658e8ddf8df7e4d20623 Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Wed, 13 Mar 2019 02:38:22 +0300 Subject: [PATCH] Optimize stories (#1988) * Add db index to articles path * Slight stories optimizations * Select only needed fields for the flare tag * Revert the last commit (to check builds) * Return selecting only needed fields for tag --- app/helpers/application_helper.rb | 2 +- app/labor/flare_tag.rb | 2 +- app/labor/sticky_article_collection.rb | 11 +++-------- app/views/articles/_sticky_nav.html.erb | 2 +- .../20190306082543_add_index_to_articles_path.rb | 5 +++++ db/schema.rb | 3 ++- 6 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20190306082543_add_index_to_articles_path.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e41f1adb8..8546c41b7 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -139,7 +139,7 @@ module ApplicationHelper def tag_colors(tag) Rails.cache.fetch("view-helper-#{tag}/tag_colors", expires_in: 5.hours) do - if found_tag = Tag.find_by_name(tag) + if found_tag = Tag.select(%i[bg_color_hex text_color_hex]).find_by_name(tag) { background: found_tag.bg_color_hex, color: found_tag.text_color_hex } else { background: "#d6d9e0", color: "#606570" } diff --git a/app/labor/flare_tag.rb b/app/labor/flare_tag.rb index 276c891d2..911f8aaec 100644 --- a/app/labor/flare_tag.rb +++ b/app/labor/flare_tag.rb @@ -19,7 +19,7 @@ class FlareTag def tag @tag ||= Rails.cache.fetch("article_flare_tag-#{article.id}-#{article.updated_at}", expires_in: 12.hours) do flare = FLARES.detect { |f| article.cached_tag_list_array.include?(f) } - flare && flare != except_tag ? Tag.find_by_name(flare) : nil + flare && flare != except_tag ? Tag.select(%i[name bg_color_hex text_color_hex]).find_by_name(flare) : nil end end diff --git a/app/labor/sticky_article_collection.rb b/app/labor/sticky_article_collection.rb index aa2dc6cf5..788880d48 100644 --- a/app/labor/sticky_article_collection.rb +++ b/app/labor/sticky_article_collection.rb @@ -3,7 +3,6 @@ class StickyArticleCollection def initialize(article, author) @article = article @author = author - @article_tags = article_tags @reaction_count_num = Rails.env.production? ? 15 : -1 @comment_count_num = Rails.env.production? ? 7 : -2 end @@ -18,16 +17,15 @@ class StickyArticleCollection end def suggested_stickies - (tag_articles + more_articles).sample(8) + (tag_articles.load + more_articles).sample(8) end def tag_articles - Article.tagged_with(article_tags, any: true). + @tag_articles ||= Article.tagged_with(article_tags, any: true). includes(:user). where("positive_reactions_count > ? OR comments_count > ?", reaction_count_num, comment_count_num). where(published: true). where.not(id: article.id, user_id: article.user_id). - limited_column_select. where("featured_number > ?", 5.days.ago.to_i). order("RANDOM()"). limit(8) @@ -39,7 +37,6 @@ class StickyArticleCollection Article.tagged_with(["career", "productivity", "discuss", "explainlikeimfive"], any: true). includes(:user). where("comments_count > ?", comment_count_num). - limited_column_select. where(published: true). where.not(id: article.id, user_id: article.user_id). where("featured_number > ?", 5.days.ago.to_i). @@ -48,8 +45,6 @@ class StickyArticleCollection end def article_tags - tags = article.cached_tag_list_array - tags.delete("discuss") - tags + @article_tags ||= article.cached_tag_list_array - ["discuss"] end end diff --git a/app/views/articles/_sticky_nav.html.erb b/app/views/articles/_sticky_nav.html.erb index 3c98df33b..ead59d06b 100644 --- a/app/views/articles/_sticky_nav.html.erb +++ b/app/views/articles/_sticky_nav.html.erb @@ -86,7 +86,7 @@ <% @sticky_collection = StickyArticleCollection.new(@article, @organization || @user) %> <% @sticky_articles = @sticky_collection.suggested_stickies %> <% @user_stickies = @sticky_collection.user_stickies %> - <% if @user_stickies && @user_stickies.any? %> + <% if @user_stickies.any? %> <% @user_stickies.each_with_index do |article, index| %>
<% if index == 0 %> diff --git a/db/migrate/20190306082543_add_index_to_articles_path.rb b/db/migrate/20190306082543_add_index_to_articles_path.rb new file mode 100644 index 000000000..77ae46edf --- /dev/null +++ b/db/migrate/20190306082543_add_index_to_articles_path.rb @@ -0,0 +1,5 @@ +class AddIndexToArticlesPath < ActiveRecord::Migration[5.1] + def change + add_index :articles, :path + end +end diff --git a/db/schema.rb b/db/schema.rb index 296671d1b..114709099 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20190305221008) do +ActiveRecord::Schema.define(version: 20190306082543) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -128,6 +128,7 @@ ActiveRecord::Schema.define(version: 20190305221008) do t.index ["boost_states"], name: "index_articles_on_boost_states", using: :gin t.index ["featured_number"], name: "index_articles_on_featured_number" t.index ["hotness_score"], name: "index_articles_on_hotness_score" + t.index ["path"], name: "index_articles_on_path" t.index ["published_at"], name: "index_articles_on_published_at" t.index ["slug"], name: "index_articles_on_slug" t.index ["user_id"], name: "index_articles_on_user_id"