Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve account media query #10121

Merged
merged 1 commit into from Feb 26, 2019

Conversation

Projects
None yet
4 participants
@abcang
Copy link
Collaborator

abcang commented Feb 26, 2019

By narrowing down by statuses.account_id, index_statuses_20180106 can be used and it becomes faster.

before:

SELECT  DISTINCT "statuses"."id" FROM "statuses" INNER JOIN "media_attachments" ON "media_attachments"."status_id" = "statuses"."id" WHERE "media_attachments"."account_id" = xxx AND ("statuses"."visibility" IN (0, 1) OR "statuses"."id" IN (SELECT "mentions"."status_id" FROM "mentions" WHERE "mentions"."account_id" = yyy)) AND (statuses.created_at <= '2019-02-26 09:21:23.846620') ORDER BY "statuses"."id" DESC LIMIT 20
Limit  (cost=3609.37..29375.14 rows=20 width=8)
  ->  Unique  (cost=3609.37..75266716.19 rows=58421 width=8)
        ->  Merge Join  (cost=3609.37..75266570.13 rows=58421 width=8)
              Merge Cond: (statuses.id = media_attachments.status_id)
              ->  Index Scan Backward using statuses_pkey on statuses  (cost=3374.35..74945413.91 rows=121241046 width=8)
                    Filter: ((created_at <= '2019-02-26 09:21:23.84662'::timestamp without time zone) AND ((visibility = ANY ('{0,1}'::integer[])) OR (hashed SubPlan 1)))
                    SubPlan 1
                      ->  Index Only Scan using index_mentions_on_account_id_and_status_id on mentions  (cost=0.56..3370.75 rows=1211 width=8)
                            Index Cond: (account_id = yyy)
              ->  Index Only Scan Backward using index_media_attachments_on_account_id_and_status_id on media_attachments  (cost=0.56..17317.88 rows=60723 width=8)
                    Index Cond: (account_id = xxx)

after:

SELECT  DISTINCT "statuses"."id" FROM "statuses" INNER JOIN "media_attachments" ON "media_attachments"."status_id" = "statuses"."id" WHERE "statuses"."account_id" = xxx AND "media_attachments"."account_id" = xxx AND ("statuses"."visibility" IN (0, 1) OR "statuses"."id" IN (SELECT "mentions"."status_id" FROM "mentions" WHERE "mentions"."account_id" = yyy)) AND (statuses.created_at <= '2019-02-26 09:21:23.846620') ORDER BY "statuses"."id" DESC LIMIT 20
Limit  (cost=3609.37..42304.73 rows=4 width=8)
  ->  Unique  (cost=3609.37..42304.73 rows=4 width=8)
        ->  Merge Join  (cost=3609.37..42304.72 rows=4 width=8)
              Merge Cond: (statuses.id = media_attachments.status_id)
              ->  Index Scan using index_statuses_20180106 on statuses  (cost=3374.35..24816.73 rows=7419 width=8)
                    Index Cond: (account_id = xxx)
                    Filter: ((created_at <= '2019-02-26 09:21:23.84662'::timestamp without time zone) AND ((visibility = ANY ('{0,1}'::integer[])) OR (hashed SubPlan 1)))
                    SubPlan 1
                      ->  Index Only Scan using index_mentions_on_account_id_and_status_id on mentions  (cost=0.56..3370.75 rows=1211 width=8)
                            Index Cond: (account_id = yyy)
              ->  Index Only Scan Backward using index_media_attachments_on_account_id_and_status_id on media_attachments  (cost=0.56..17317.88 rows=60723 width=8)
                    Index Cond: (account_id = xxx)

@abcang abcang added the performance label Feb 26, 2019

@ykzts

ykzts approved these changes Feb 26, 2019

@ThibG

ThibG approved these changes Feb 26, 2019

Copy link
Collaborator

ThibG left a comment

LGTM!

Hm, looking at the query, I'm wondering where does that created_at query come from? I don't know how expensive it is, but it doesn't seem relevent.

@abcang

This comment has been minimized.

Copy link
Collaborator Author

abcang commented Feb 26, 2019

Oh, I'm sorry. Please ignore created_at because it is due to extension of Pawoo. It does not affect query improvement.

@Gargron Gargron merged commit a5e7ada into master Feb 26, 2019

11 checks passed

ci/circleci: build Your tests passed on CircleCI!
Details
ci/circleci: check-i18n Your tests passed on CircleCI!
Details
ci/circleci: install Your tests passed on CircleCI!
Details
ci/circleci: install-ruby2.4 Your tests passed on CircleCI!
Details
ci/circleci: install-ruby2.5 Your tests passed on CircleCI!
Details
ci/circleci: install-ruby2.6 Your tests passed on CircleCI!
Details
ci/circleci: test-ruby2.4 Your tests passed on CircleCI!
Details
ci/circleci: test-ruby2.5 Your tests passed on CircleCI!
Details
ci/circleci: test-ruby2.6 Your tests passed on CircleCI!
Details
ci/circleci: test-webui Your tests passed on CircleCI!
Details
codeclimate All good!
Details

@ykzts ykzts deleted the abcang-patch-1 branch Feb 26, 2019

Gargron added a commit that referenced this pull request Mar 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.