bastodon/app/lib
Kaspar V 47f0d7021e
refactor(vacuum statuses): reduce amount of db queries and load for each query - improve performance (#21487)
* refactor(statuses_vacuum): remove dead code - unused

Method is not called inside class and private.
Clean up dead code.

* refactor(statuses_vacuum): make retention_period present test explicit

This private method only hides functionality.
It is best practice to be as explicit as possible.

* refactor(statuses_vacuum): improve query performance

- fix statuses_scope having sub-select for Account.remote scope by
  `joins(:account).merge(Account.remote)`
- fix statuses_scope unnecessary use of `Status.arel_table[:id].lt`
  because it is inexplicit, bad practice and even slower than normal
  `.where('statuses.id < ?'`
- fix statuses_scope remove select(:id, :visibility) for having reusable
  active record query batches (no re queries)
- fix vacuum_statuses! to use in_batches instead of find_in_batches,
  because in_batches delivers a full blown active record query result,
  in stead of an array - no requeries necessary
- send(:unlink_from_conversations) not to perform another db query, but
  reuse the in_batches result instead.
- remove now obsolete remove_from_account_conversations method
- remove_from_search_index uses array of ids, instead of mapping
  the ids from an array - this should be more efficient
- use the in_batches scope to call delete_all, instead of running
  another db query for this - because it is again more efficient
- add TODO comment for calling models private method with send

* refactor(status): simplify unlink_from_conversations

- add `has_many through:` relation mentioned_accounts
- use model scope local instead of method call `Status#local?`
- more readable add account to inbox_owners when account.local?

* refactor(status): searchable_by way less sub selects

These queries all included a sub-select. Doing the same with a joins
should be more efficient.
Since this method does 5 such queries, this should be significant,
since it technically halves the query count.

This is how it was:

```ruby
[3] pry(main)> Status.first.mentions.where(account: Account.local, silent: false).explain
  Status Load (1.6ms)  SELECT "statuses".* FROM "statuses" WHERE "statuses"."deleted_at" IS NULL ORDER BY "statuses"."id" DESC LIMIT $1  [["LIMIT", 1]]
  Mention Load (1.5ms)  SELECT "mentions".* FROM "mentions" WHERE "mentions"."status_id" = $1 AND "mentions"."account_id" IN (SELECT "accounts"."id" FROM "accounts" WHERE "accounts"."domain" IS NULL) AND "mentions"."silent" = $2  [["status_id", 109382923142288414], ["silent", false]]
=> EXPLAIN for: SELECT "mentions".* FROM "mentions" WHERE "mentions"."status_id" = $1 AND "mentions"."account_id" IN (SELECT "accounts"."id" FROM "accounts" WHERE "accounts"."domain" IS NULL) AND "mentions"."silent" = $2 [["status_id", 109382923142288414], ["silent", false]]
                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=0.15..23.08 rows=1 width=41)
   ->  Seq Scan on accounts  (cost=0.00..10.90 rows=1 width=8)
         Filter: (domain IS NULL)
   ->  Index Scan using index_mentions_on_account_id_and_status_id on mentions  (cost=0.15..8.17 rows=1 width=41)
         Index Cond: ((account_id = accounts.id) AND (status_id = '109382923142288414'::bigint))
         Filter: (NOT silent)
(6 rows)
```

This is how it is with this change:

```ruby
[4] pry(main)> Status.first.mentions.joins(:account).merge(Account.local).active.explain
  Status Load (1.7ms)  SELECT "statuses".* FROM "statuses" WHERE "statuses"."deleted_at" IS NULL ORDER BY "statuses"."id" DESC LIMIT $1  [["LIMIT", 1]]
  Mention Load (0.7ms)  SELECT "mentions".* FROM "mentions" INNER JOIN "accounts" ON "accounts"."id" = "mentions"."account_id" WHERE "mentions"."status_id" = $1 AND "accounts"."domain" IS NULL AND "mentions"."silent" = $2  [["status_id", 109382923142288414], ["silent", false]]
=> EXPLAIN for: SELECT "mentions".* FROM "mentions" INNER JOIN "accounts" ON "accounts"."id" = "mentions"."account_id" WHERE "mentions"."status_id" = $1 AND "accounts"."domain" IS NULL AND "mentions"."silent" = $2 [["status_id", 109382923142288414], ["silent", false]]
                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=0.15..23.08 rows=1 width=41)
   ->  Seq Scan on accounts  (cost=0.00..10.90 rows=1 width=8)
         Filter: (domain IS NULL)
   ->  Index Scan using index_mentions_on_account_id_and_status_id on mentions  (cost=0.15..8.17 rows=1 width=41)
         Index Cond: ((account_id = accounts.id) AND (status_id = '109382923142288414'::bigint))
         Filter: (NOT silent)
(6 rows)
```
2022-11-27 20:41:18 +01:00
..
activitypub Add featured tag add/remove activity handler (#19408) 2022-10-22 11:49:41 +02:00
admin Change incoming activity processing to happen in ingress queue (#20264) 2022-11-10 14:21:51 +01:00
connection_pool
importer Fix ambiguous column names in tootctl search deploy (#18993) 2022-08-17 22:07:12 +01:00
nodeinfo
ostatus
rss Fix errors when rendering RSS feeds (#18531) 2022-05-26 23:02:42 +02:00
settings
translation_service Add mention of the translation provider when translating a post (#19433) 2022-10-24 18:37:57 +02:00
vacuum refactor(vacuum statuses): reduce amount of db queries and load for each query - improve performance (#21487) 2022-11-27 20:41:18 +01:00
access_token_extension.rb Fix single Redis connection being used across all threads (#18135) 2022-04-28 17:47:34 +02:00
account_reach_finder.rb
activity_tracker.rb
application_extension.rb Fix confirmation redirect to app without Location header (#18523) 2022-05-26 22:03:54 +02:00
ascii_folding.rb Change how hashtags are normalized (#18795) 2022-07-13 15:03:28 +02:00
cache_buster.rb
delivery_failure_tracker.rb Fix single Redis connection being used across all threads (#18135) 2022-04-28 17:47:34 +02:00
emoji_formatter.rb Fix emoji substitution not applying only to text nodes in backend code (#20641) 2022-11-14 20:26:21 +01:00
entity_cache.rb
extractor.rb
fast_geometry_parser.rb
fast_ip_map.rb
feed_manager.rb Fix improperly checking for blocked domain on followed hashtags (#19472) 2022-10-26 20:40:56 +02:00
hash_object.rb
hashtag_normalizer.rb Improve performance by avoiding regex construction (#20215) 2022-11-10 05:49:30 +01:00
html_aware_formatter.rb
inline_renderer.rb Add caching for payload serialization during fan-out (#19642) 2022-11-04 13:21:06 +01:00
link_details_extractor.rb
permalink_redirector.rb Change public accounts pages to mount the web UI (#19319) 2022-10-20 14:35:29 +02:00
plain_text_formatter.rb
potential_friendship_tracker.rb
rate_limiter.rb
redis_configuration.rb Add retention policy for cached content and media (#19232) 2022-09-27 03:08:19 +02:00
request.rb fixes ArgumentError when proxy is used (#20420) 2022-11-11 21:31:03 +01:00
request_pool.rb
response_with_limit.rb
scope_parser.rb
scope_transformer.rb
search_query_parser.rb
search_query_transformer.rb
status_cache_hydrator.rb Fix filter handling in status cache hydration (#19963) 2022-11-07 19:47:48 +01:00
status_filter.rb
status_finder.rb
status_reach_finder.rb Fix reblogs being discarded after the reblogged status (#19731) 2022-11-04 16:31:44 +01:00
suspicious_sign_in_detector.rb
tag_manager.rb Fix TagManager#local_url? erroring out on invalid URL (#18580) 2022-05-31 18:25:24 +02:00
text_formatter.rb
themes.rb
toc_generator.rb
translation_service.rb Change “Translate” button to only show up when a translation backend is configured (#19434) 2022-10-24 18:30:58 +02:00
user_settings_decorator.rb
vacuum.rb Add retention policy for cached content and media (#19232) 2022-09-27 03:08:19 +02:00
validation_error_formatter.rb
video_metadata_extractor.rb
webfinger.rb Improve error reporting and logging when processing remote accounts (#15605) 2022-09-20 23:30:26 +02:00
webfinger_resource.rb