discourse/spec
Isaac Janzen db10dd5319
PERF: Improve performance of most_replied_to_users (#26373)
This PR improves the performance of the `most_replied_to_users` method on the `UserSummary` model.

### Old Query
```ruby
    post_query
      .joins(
        "JOIN posts replies ON posts.topic_id = replies.topic_id AND posts.reply_to_post_number = replies.post_number",
      )
      # We are removing replies by @user, but we can simplify this by getting the using the user_id on the posts.
      .where("replies.user_id <> ?", @user.id)
      .group("replies.user_id")
      .order("COUNT(*) DESC")
      .limit(MAX_SUMMARY_RESULTS)
      .pluck("replies.user_id, COUNT(*)")
      .each { |r| replied_users[r[0]] = r[1] }
```
 
### Old Query with corrections

```ruby
post_query
  .joins(
    "JOIN posts replies ON posts.topic_id = replies.topic_id AND replies.reply_to_post_number = posts.post_number",
  )
  # Remove replies by @user but instead look on loaded posts (we do this so we don't count self replies)
  .where("replies.user_id <> posts.user_id")
  .group("replies.user_id")
  .order("COUNT(*) DESC")
  .limit(MAX_SUMMARY_RESULTS)
  .pluck("replies.user_id, COUNT(*)")
  .each { |r| replied_users[r[0]] = r[1] }
```

### New Query
```ruby
    post_query
      .joins(
        "JOIN posts replies ON posts.topic_id = replies.topic_id AND posts.reply_to_post_number = replies.post_number",
      )
      # Only include regular posts in our joins, this makes sure we don't have the bloat of loading private messages
      .joins(
        "JOIN topics ON replies.topic_id = topics.id AND topics.archetype <> 'private_message'",
      )
      # Only include visible post types, so exclude posts like whispers, etc
      .joins(
        "AND replies.post_type IN (#{Topic.visible_post_types(@user, include_moderator_actions: false).join(",")})",
      )
      .where("replies.user_id <> posts.user_id")
      .group("replies.user_id")
      .order("COUNT(*) DESC")
      .limit(MAX_SUMMARY_RESULTS)
      .pluck("replies.user_id, COUNT(*)")
      .each { |r| replied_users[r[0]] = r[1] }
```

# Conclusion

`most_replied_to_users` was untested, so I introduced a test for the logic, and have confirmed that it passes on both the new query **AND** the old query. 

Thank you @danielwaterworth for the debugging assistance.
2024-04-03 14:20:54 -06:00
..
fabricators DEV: Make all admins TL4 in tests (#25435) 2024-03-26 11:41:12 +08:00
fixtures DEV: Support translations for property labels in objects schema editor (#26362) 2024-03-28 10:53:51 +08:00
generator DEV: Silence the output of migration specs (#26365) 2024-03-26 11:32:44 +01:00
helpers FIX: print view wasn't working (#26433) 2024-04-02 08:27:16 +11:00
import_export DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00
initializers DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00
integration DEV: Add a test to ensure that our SMTP settings are correct (#26410) 2024-03-28 10:18:19 +08:00
integrity Enable Embroider/Webpack code spliting for Wizard (#24919) 2023-12-20 13:15:06 +00:00
jobs DEV: Add skip_email_bulk_invites hidden site setting (#26430) 2024-03-29 13:22:00 -04:00
lib DEV: add logo URL and locale details to the Discover stats. (#26320) 2024-04-04 00:22:28 +05:30
mailers DEV: Update rubocop-discourse to latest version 2024-03-04 15:08:35 +01:00
migrations DEV: Silence the output of migration specs (#26365) 2024-03-26 11:32:44 +01:00
models PERF: Improve performance of most_replied_to_users (#26373) 2024-04-03 14:20:54 -06:00
multisite DEV: Add S3 upload system specs using minio (#22975) 2023-08-23 11:18:33 +10:00
requests DEV: add logo URL and locale details to the Discover stats. (#26320) 2024-04-04 00:22:28 +05:30
script/import_scripts DEV: Allow fab! without block (#24314) 2023-11-09 16:47:59 -06:00
serializers DEV: Support translations for property labels in objects schema editor (#26362) 2024-03-28 10:53:51 +08:00
services UX: Improvements to user tips (#26480) 2024-04-03 11:43:56 -04:00
support DEV: Move non scheduled problem checks to classes (#26122) 2024-03-14 10:55:01 +08:00
system UX: Improvements to user tips (#26480) 2024-04-03 11:43:56 -04:00
tasks DEV: Introduce rake task to validate discourse-compatibility file (#26158) 2024-03-13 13:57:41 +00:00
views FEATURE: Simplify crawler content for non-canonical post URLs (#26324) 2024-03-26 15:18:46 +00:00
rails_helper.rb DEV: Use caller for plugin_file_from_fixtures (#26387) 2024-03-27 14:12:51 +11:00
regenerate_swagger_docs DEV: Add API docs for uploads and API doc watcher (#15387) 2021-12-23 08:40:15 +10:00
swagger_helper.rb DEV: Bump rswag-specs from 2.11.0 to 2.13.0 (#24654) 2023-12-07 08:16:47 +08:00