]> cat aescling's git repositories - mastodon.git/commitdiff
Further improve the media attached status query for accounts (#16106)
authorabcang <abcang1015@gmail.com>
Mon, 26 Apr 2021 16:57:46 +0000 (01:57 +0900)
committerGitHub <noreply@github.com>
Mon, 26 Apr 2021 16:57:46 +0000 (18:57 +0200)
app/controllers/accounts_controller.rb
app/controllers/admin/statuses_controller.rb
app/controllers/api/v1/accounts/statuses_controller.rb
db/migrate/20210425135952_add_index_on_media_attachments_account_id_status_id.rb [new file with mode: 0644]
db/schema.rb

index f159615527d04e7b9f4e4f4dca38be1a084672fa..8210918d869c1083315a72a38cf13dde4204f67c 100644 (file)
@@ -77,7 +77,7 @@ class AccountsController < ApplicationController
   end
 
   def only_media_scope
-    Status.joins(:media_attachments).group(:id)
+    Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
   end
 
   def no_replies_scope
index df1bd1bc75e8da2dc8862b7fcc2742e7e0e7fa82..ef279509d8759b765814140e7f15fd6f4013fb38 100644 (file)
@@ -14,7 +14,7 @@ module Admin
       @statuses = @account.statuses.where(visibility: [:public, :unlisted])
 
       if params[:media]
-        @statuses.merge!(Status.joins(:media_attachments).group(:id))
+        @statuses.merge!(Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id))
       end
 
       @statuses = @statuses.preload(:media_attachments, :mentions).page(params[:page]).per(PER_PAGE)
index b1e478f8d75860aff3c80bd5bfebaa11dc80dc5e..92ccb80615da26ad94dbebf6edccefe7c7c14fa0 100644 (file)
@@ -42,7 +42,7 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
   end
 
   def only_media_scope
-    Status.joins(:media_attachments).group(:id)
+    Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
   end
 
   def pinned_scope
diff --git a/db/migrate/20210425135952_add_index_on_media_attachments_account_id_status_id.rb b/db/migrate/20210425135952_add_index_on_media_attachments_account_id_status_id.rb
new file mode 100644 (file)
index 0000000..5ef2d3c
--- /dev/null
@@ -0,0 +1,13 @@
+class AddIndexOnMediaAttachmentsAccountIdStatusId < ActiveRecord::Migration[5.2]
+  disable_ddl_transaction!
+
+  def up
+    add_index :media_attachments, [:account_id, :status_id], order: { status_id: :desc }, algorithm: :concurrently
+    remove_index :media_attachments, :account_id, algorithm: :concurrently
+  end
+
+  def down
+    add_index :media_attachments, :account_id, algorithm: :concurrently
+    remove_index :media_attachments, [:account_id, :status_id], order: { status_id: :desc }, algorithm: :concurrently
+  end
+end
index 8dc0661fc92376236713a3e9be5df922662b39c8..0d951ee957d86f2761ae56da07b27cdf552af078 100644 (file)
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2021_04_21_121431) do
+ActiveRecord::Schema.define(version: 2021_04_25_135952) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -541,7 +541,7 @@ ActiveRecord::Schema.define(version: 2021_04_21_121431) do
     t.integer "thumbnail_file_size"
     t.datetime "thumbnail_updated_at"
     t.string "thumbnail_remote_url"
-    t.index ["account_id"], name: "index_media_attachments_on_account_id"
+    t.index ["account_id", "status_id"], name: "index_media_attachments_on_account_id_and_status_id", order: { status_id: :desc }
     t.index ["scheduled_status_id"], name: "index_media_attachments_on_scheduled_status_id"
     t.index ["shortcode"], name: "index_media_attachments_on_shortcode", unique: true
     t.index ["status_id"], name: "index_media_attachments_on_status_id"