【发布时间】:2019-11-25 23:56:31
【问题描述】:
问题:我需要找到带有指定 Hashtag 的帖子。 many:many 连接表称为 PostHashtag。但是,看起来内部连接太慢了:
EXPLAIN ANALYZE SELECT
"Post"."id",
"Post"."createdAt",
"Post"."photo",
"Post"."thumbnail",
"hashtags->PostHashtag"."id" AS "hashtags.PostHashtag.id",
"hashtags->PostHashtag"."hashtagId" AS "hashtags.PostHashtag.hashtagId",
"hashtags->PostHashtag"."postId" AS "hashtags.PostHashtag.postId",
"hashtags->PostHashtag"."createdAt" AS "hashtags.PostHashtag.createdAt",
"hashtags->PostHashtag"."updatedAt" AS "hashtags.PostHashtag.updatedAt"
FROM
"Posts" AS "Post"
INNER JOIN (
"PostHashtags" AS "hashtags->PostHashtag"
INNER JOIN "Hashtags" AS "hashtags" ON "hashtags"."id" = "hashtags->PostHashtag"."hashtagId"
) ON "Post"."id" = "hashtags->PostHashtag"."postId"
AND "hashtags"."name" = 'pumpupfam'
WHERE
"Post"."photo" IS NOT NULL
AND "Post"."private" IS NULL
ORDER BY
"Post"."createdAt" DESC
LIMIT 90;
产量
Limit (cost=2651.58..2651.60 rows=7 width=212) (actual time=349.987..350.048 rows=90 loops=1)
-> Sort (cost=2651.58..2651.60 rows=7 width=212) (actual time=349.985..350.013 rows=90 loops=1)
Sort Key: "Post"."createdAt"
Sort Method: top-N heapsort Memory: 48kB
-> Nested Loop (cost=18.80..2651.49 rows=7 width=212) (actual time=9.125..336.449 rows=30092 loops=1)
-> Nested Loop (cost=18.37..2623.52 rows=19 width=28) (actual time=9.096..93.508 rows=31495 loops=1)
-> Index Scan using hashtags_name_unique_index on "Hashtags" hashtags (cost=0.42..8.44 rows=1 width=4) (actual time=0.034..0.036 rows=1 loops=1)
Index Cond: ((name)::text = 'pumpupfam'::text)
-> Bitmap Heap Scan on "PostHashtags" "hashtags->PostHashtag" (cost=17.94..2607.97 rows=711 width=28) (actual time=9.059..79.563 rows=31495 loops=1)
Recheck Cond: ("hashtagId" = hashtags.id)
Heap Blocks: exact=21507
-> Bitmap Index Scan on posthashtags_hashtagid_fk_index (cost=0.00..17.77 rows=711 width=0) (actual time=5.393..5.393 rows=31496 loops=1)
Index Cond: ("hashtagId" = hashtags.id)
-> Index Scan using "Posts_pkey" on "Posts" "Post" (cost=0.43..1.46 rows=1 width=184) (actual time=0.006..0.007 rows=1 loops=31495)
Index Cond: (id = "hashtags->PostHashtag"."postId")
Filter: ((photo IS NOT NULL) AND (private IS NULL))
Rows Removed by Filter: 0
Planning time: 2.865 ms
Execution time: 350.289 ms
如何加快速度?
谢谢
【问题讨论】:
-
子查询必须有别名。
-
@wildplasser 那里没有子查询。这是一个带括号的连接,不需要别名。
标签: sql postgresql performance sequelize.js