docbrown/app/services/articles
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
..
feeds Optimize column selection for articles belonging to "any" given tag (#12420) 2021-01-25 17:34:10 +01:00
builder.rb Rubocop: fix Metrics/CyclomaticComplexity and disable Metrics/AbcSize (#9221) 2020-07-10 15:26:05 +02:00
creator.rb [deploy] Limit articles created by newer users (#10686) 2020-10-06 21:02:05 -04:00
destroyer.rb [deploy] Add dependent clauses to Article and fix Comment validation (#9990) 2020-08-28 18:24:28 +02:00
detect_language.rb Move LanguageDetector to service (#12035) 2021-01-04 19:12:24 -05:00
get_user_stickies.rb Optimize column selection for articles belonging to "any" given tag (#12420) 2021-01-25 17:34:10 +01:00
suggest.rb Move ArticleSuggester to Articles::Suggest service (#11370) 2020-11-11 13:06:33 -05:00
suggest_stickies.rb Optimize column selection for articles belonging to "any" given tag (#12420) 2021-01-25 17:34:10 +01:00
updater.rb Rubocop: enable and fix new Rails cops (#9537) 2020-07-29 11:14:19 +02:00