Optimize published_articles_by_tag fetch speed (#12584)

This commit is contained in:
Mac Siri 2021-02-05 09:25:04 -05:00 committed by GitHub
parent d45d9be7c6
commit 0f5da03ccb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 4 deletions

View file

@ -28,11 +28,10 @@ module Articles
end
def published_articles_by_tag
articles = Article.published.limited_column_select
articles = @tag.present? ? Tag.find_by(name: @tag).articles : Article
articles.published.limited_column_select
.includes(top_comments: :user)
.page(@page).per(@number_of_articles)
articles = articles.cached_tagged_with(@tag) if @tag.present? # More efficient than tagged_with
articles
end
# Timeframe values from Timeframe::DATETIMES

View file

@ -15,7 +15,7 @@ RSpec.describe Articles::Feeds::LargeForemExperimental, type: :service do
describe "#published_articles_by_tag" do
let(:unpublished_article) { create(:article, published: false) }
let(:tag) { "foo" }
let(:tagged_article) { create(:article, tags: tag) }
let!(:tagged_article) { create(:article, tags: tag) }
it "returns published articles" do
result = feed.published_articles_by_tag