docbrown/app/services/article_api_index_service.rb
rhymes def0cf2437
Optimize column selection for articles belonging to "any" given tag (#12420)
When `acts-as-taggable-on`'s `.tagged_with()` is used with `any: true`,
the gem will use `SELECT *` regardless of any previous (or following) requests
of selecting a limited amount of columns.

Given that the `articles` table has [73 columns](https://dev.to/admin/blazer/queries/314-number-of-columns-in-all-tables)
that will amount to wasted RAM memory for columns we don't need.

By "unscoping" any previous `select()` we can optimize used memory.

Before:

```ruby
[24] pry(main)> Article.tagged_with([:ruby], any: true).select(:id, :name).to_sql
=> "SELECT \"articles\".*, \"articles\".\"id\", \"name\" FROM \"articles\" WHERE EXISTS (SELECT * FROM \"taggings\" WHERE \"taggings\".\"taggable_id\" = \"articles\".\"id\" AND \"taggings\".\"taggable_type\" = 'Article' AND \"taggings\".\"tag_id\" IN (SELECT \"tags\".\"id\" FROM \"tags\" WHERE (\"tags\".\"name\" LIKE 'ruby' ESCAPE '!')))"
```

Note, how the SQL query is `articles.*, articles.column_a`

After:

[25] pry(main)> Article.tagged_with([:ruby], any: true).unscope(:select).select(:id, :name).to_sql
=> "SELECT \"articles\".\"id\", \"name\" FROM \"articles\" WHERE EXISTS (SELECT * FROM \"taggings\" WHERE \"taggings\".\"taggable_id\" = \"articles\".\"id\" AND \"taggings\".\"taggable_type\" = 'Article' AND \"taggings\".\"tag_id\" IN (SELECT \"tags\".\"id\" FROM \"tags\" WHERE (\"tags\".\"name\" LIKE 'ruby' ESCAPE '!')))"
```

`articles.*` is gone :-)

- https://github.com/mbleigh/acts-as-taggable-on/issues/936
- 47da5036de/lib/acts_as_taggable_on/taggable/tagged_with_query/any_tags_query.rb (L2-L8)
2021-01-25 17:34:10 +01:00

132 lines
3.7 KiB
Ruby

class ArticleApiIndexService
DEFAULT_PER_PAGE = 30
MAX_PER_PAGE = 1000
def initialize(params)
@page = params[:page]
@tag = params[:tag]
@tags = params[:tags]
@tags_exclude = params[:tags_exclude]
@username = params[:username]
@state = params[:state]
@top = params[:top]
@collection_id = params[:collection_id]
@per_page = params[:per_page]
end
def get
if tag.present?
tag_articles
elsif tags.present? || tags_exclude.present?
tagged_articles
elsif username.present?
username_articles
elsif state.present?
state_articles(state)
elsif top.present?
top_articles
elsif collection_id.present?
collection_articles(collection_id)
else
base_articles
end
end
private
attr_reader :tag, :tags, :tags_exclude, :username, :page, :state, :top, :collection_id, :per_page
def username_articles
num = if @state == "all"
MAX_PER_PAGE
else
DEFAULT_PER_PAGE
end
if (user = User.find_by(username: username))
user.articles.published
.includes(:organization)
.order(published_at: :desc)
.page(page)
.per(per_page || num)
elsif (organization = Organization.find_by(slug: username))
organization.articles.published
.includes(:user)
.order(published_at: :desc)
.page(page)
.per(per_page || num)
else
Article.none
end
end
def tag_articles
articles = Article.published.cached_tagged_with(tag).includes(:user, :organization)
articles = if Tag.find_by(name: tag)&.requires_approval
articles.where(approved: true).order(featured_number: :desc)
elsif top.present?
articles.where("published_at > ?", top.to_i.days.ago)
.order(public_reactions_count: :desc)
else
articles.order(hotness_score: :desc)
end
articles.page(page).per(per_page || DEFAULT_PER_PAGE)
end
def tagged_articles
articles = Article.published.includes(:user, :organization)
articles = articles.tagged_with(tags, any: true).unscope(:select) if tags
articles = articles.tagged_with(tags_exclude, exclude: true) if tags_exclude
articles
.order(public_reactions_count: :desc)
.page(page).per(per_page || DEFAULT_PER_PAGE)
end
def top_articles
Article.published.includes(:user, :organization)
.where("published_at > ?", top.to_i.days.ago)
.order(public_reactions_count: :desc)
.page(page).per(per_page || DEFAULT_PER_PAGE)
end
def state_articles(state)
articles = Article.published.includes(:user, :organization)
articles = case state
when "fresh"
articles.where(
"public_reactions_count < ? AND featured_number > ? AND score > ?", 2, 7.hours.ago.to_i, -2
)
when "rising"
articles.where(
"public_reactions_count > ? AND public_reactions_count < ? AND featured_number > ?",
19, 33, 3.days.ago.to_i
)
else
Article.none
end
articles.page(page).per(per_page || DEFAULT_PER_PAGE)
end
def collection_articles(collection_id)
Article.published
.where(collection_id: collection_id)
.includes(:user, :organization)
.order(:published_at)
.page(page)
.per(per_page || DEFAULT_PER_PAGE)
end
def base_articles
Article.published
.where(featured: true)
.includes(:user, :organization)
.order(hotness_score: :desc)
.page(page)
.per(per_page || DEFAULT_PER_PAGE)
end
end