Further improve the media attached status query for accounts (#16106)

shrike
abcang 2021-04-27 01:57:46 +09:00 committed by GitHub
parent f78cbc0c32
commit d0fc69d721
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 5 deletions

View File

@ -77,7 +77,7 @@ class AccountsController < ApplicationController
end end
def only_media_scope def only_media_scope
Status.joins(:media_attachments).group(:id) Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
end end
def no_replies_scope def no_replies_scope

View File

@ -14,7 +14,7 @@ module Admin
@statuses = @account.statuses.where(visibility: [:public, :unlisted]) @statuses = @account.statuses.where(visibility: [:public, :unlisted])
if params[:media] 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 end
@statuses = @statuses.preload(:media_attachments, :mentions).page(params[:page]).per(PER_PAGE) @statuses = @statuses.preload(:media_attachments, :mentions).page(params[:page]).per(PER_PAGE)

View File

@ -42,7 +42,7 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
end end
def only_media_scope def only_media_scope
Status.joins(:media_attachments).group(:id) Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
end end
def pinned_scope def pinned_scope

View File

@ -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

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -541,7 +541,7 @@ ActiveRecord::Schema.define(version: 2021_04_21_121431) do
t.integer "thumbnail_file_size" t.integer "thumbnail_file_size"
t.datetime "thumbnail_updated_at" t.datetime "thumbnail_updated_at"
t.string "thumbnail_remote_url" 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 ["scheduled_status_id"], name: "index_media_attachments_on_scheduled_status_id"
t.index ["shortcode"], name: "index_media_attachments_on_shortcode", unique: true t.index ["shortcode"], name: "index_media_attachments_on_shortcode", unique: true
t.index ["status_id"], name: "index_media_attachments_on_status_id" t.index ["status_id"], name: "index_media_attachments_on_status_id"