Yeah, the reindexing has finished. Sorting by topic creation date has the same issue. Here’s the EXPLAIN output for both queries:
Sorted by tagging date:
discourse=# explain SELECT "topics"."id", "topics"."title", "topics"."last_posted_at", "topics"."created_at", "topics"."updated_at", "topics"."views", "topics"."posts_count", "topics"."user_id", "topics"."last_post_user_id", "topics"."reply_count", "topics"."featured_user1_id", "topics"."featured_user2_id", "topics"."featured_user3_id", "topics"."deleted_at", "topics"."highest_post_number", "topics"."like_count", "topics"."incoming_link_count", "topics"."category_id", "topics"."visible", "topics"."moderator_posts_count", "topics"."closed", "topics"."archived", "topics"."bumped_at", "topics"."has_summary", "topics"."archetype", "topics"."featured_user4_id", "topics"."notify_moderators_count", "topics"."spam_count", "topics"."pinned_at", "topics"."score", "topics"."percent_rank", "topics"."subtype", "topics"."slug", "topics"."deleted_by_id", "topics"."participant_count", "topics"."word_count", "topics"."excerpt", "topics"."pinned_globally", "topics"."pinned_until", "topics"."fancy_title", "topics"."highest_staff_post_number", "topics"."featured_link", "topics"."reviewable_score", "topics"."image_upload_id"
discourse-# FROM "topics"
discourse-# INNER JOIN "topic_tags"
discourse-# ON "topic_tags"."topic_id" = "topics"."id"
discourse-# INNER JOIN "tags" ON "tags"."id" = "topic_tags"."tag_id"
discourse-# WHERE ("topics"."deleted_at" IS NULL)
discourse-# AND "topics"."visible" = TRUE
discourse-# AND (NOT topics.closed AND NOT topics.archived AND topics.deleted_at IS NULL)
discourse-# AND (topics.image_upload_id in (
discourse(# SELECT image_upload_id FROM topic_thumbnails
discourse(# ))
discourse-# AND (tags.id IN (1))
discourse-#
discourse-# ORDER BY (
discourse(# SELECT created_at FROM topic_tags
discourse(# WHERE topic_id = topics.id
discourse(# AND tag_id IN (1)
discourse(# LIMIT 1
discourse(# )
discourse-#
discourse-# DESC LIMIT 6;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------
------------
Limit (cost=2631719.94..2631719.95 rows=6 width=569)
-> Sort (cost=2631719.94..2631721.73 rows=717 width=569)
Sort Key: ((SubPlan 1)) DESC
-> Nested Loop (cost=0.43..2631707.09 rows=717 width=569)
-> Seq Scan on tags (cost=0.00..4.51 rows=1 width=4)
Filter: (id = 1)
-> Nested Loop (cost=0.43..2625737.13 rows=717 width=565)
-> Seq Scan on topic_tags (cost=0.00..860.33 rows=1539 width=8)
Filter: (tag_id = 1)
-> Index Scan using topics_pkey on topics (cost=0.43..1705.57 rows=1 width=561)
Index Cond: (id = topic_tags.topic_id)
Filter: ((deleted_at IS NULL) AND visible AND (NOT closed) AND (NOT archived) AND (deleted_at IS NULL) AND (
SubPlan 2))
SubPlan 2
-> Seq Scan on topic_thumbnails (cost=0.00..2966.64 rows=171664 width=8)
SubPlan 1
-> Limit (cost=0.29..8.31 rows=1 width=8)
-> Index Scan using index_topic_tags_on_topic_id_and_tag_id on topic_tags topic_tags_1 (cost=0.29..8.31 rows=1
width=8)
Index Cond: ((topic_id = topics.id) AND (tag_id = 1))
JIT:
Functions: 25
Options: Inlining true, Optimization true, Expressions true, Deforming true
(21 rows)
sorted by topic date:
discourse=# explain SELECT "topics"."id", "topics"."title", "topics"."last_posted_at", "topics"."created_at", "topics"."updated_at", "topics"."views", "topics"."posts_count", "topics"."user_id", "topics"."last_post_user_id", "topics"."reply_count", "topics"."featured_user1_id", "topics"."featured_user2_id", "topics"."featured_user3_id", "topics"."deleted_at", "topics"."highest_post_number", "topics"."like_count", "topics"."incoming_link_count", "topics"."category_id", "topics"."visible", "topics"."moderator_posts_count", "topics"."closed", "topics"."archived", "topics"."bumped_at", "topics"."has_summary", "topics"."archetype", "topics"."featured_user4_id", "topics"."notify_moderators_count", "topics"."spam_count", "topics"."pinned_at", "topics"."score", "topics"."percent_rank", "topics"."subtype", "topics"."slug", "topics"."deleted_by_id", "topics"."participant_count", "topics"."word_count", "topics"."excerpt", "topics"."pinned_globally", "topics"."pinned_until", "topics"."fancy_title", "topics"."highest_staff_post_number", "topics"."featured_link", "topics"."reviewable_score", "topics"."image_upload_id" FROM "topics" INNER JOIN "topic_tags" ON "topic_tags"."topic_id" = "topics"."id" INNER JOIN "tags" ON "tags"."id" = "topic_tags"."tag_id" WHERE ("topics"."deleted_at" IS NULL) AND "topics"."visible" = TRUE AND (NOT topics.closed AND NOT topics.archived AND topics.deleted_at IS NULL) AND (topics.image_upload_id in (
SELECT image_upload_id FROM topic_thumbnails
)) AND (tags.id IN (1)) ORDER BY (SELECT created_at FROM topic_tags
WHERE topic_id = topics.id
AND tag_id IN (1)
LIMIT 1)
DESC LIMIT 6;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------
------------
Limit (cost=2631719.94..2631719.95 rows=6 width=569)
-> Sort (cost=2631719.94..2631721.73 rows=717 width=569)
Sort Key: ((SubPlan 1)) DESC
-> Nested Loop (cost=0.43..2631707.09 rows=717 width=569)
-> Seq Scan on tags (cost=0.00..4.51 rows=1 width=4)
Filter: (id = 1)
-> Nested Loop (cost=0.43..2625737.13 rows=717 width=565)
-> Seq Scan on topic_tags (cost=0.00..860.33 rows=1539 width=8)
Filter: (tag_id = 1)
-> Index Scan using topics_pkey on topics (cost=0.43..1705.57 rows=1 width=561)
Index Cond: (id = topic_tags.topic_id)
Filter: ((deleted_at IS NULL) AND visible AND (NOT closed) AND (NOT archived) AND (deleted_at IS NULL) AND (
SubPlan 2))
SubPlan 2
-> Seq Scan on topic_thumbnails (cost=0.00..2966.64 rows=171664 width=8)
SubPlan 1
-> Limit (cost=0.29..8.31 rows=1 width=8)
-> Index Scan using index_topic_tags_on_topic_id_and_tag_id on topic_tags topic_tags_1 (cost=0.29..8.31 rows=1
width=8)
Index Cond: ((topic_id = topics.id) AND (tag_id = 1))
JIT:
Functions: 25
Options: Inlining true, Optimization true, Expressions true, Deforming true
(21 rows)