我正在尝试优化这个 SQL 查询:
select topics.id from "topics"
left join "articles_topics" on "topics"."id" = "articles_topics"."topic_id"
left join "articles" on "articles_topics"."article_id" = "articles"."id"
where not "topics"."type" = 'sport' and "articles"."image" is not null
group by "topics"."id"
having COUNT(articles.id) > 10
Run Code Online (Sandbox Code Playgroud)
这是完整的查询成本(我使用过EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS)
)
Finalize HashAggregate (cost=12881.12..12974.90 rows=2501 width=8) (actual time=209.037..210.463 rows=1381 loops=1)
Output: topics.id
Group Key: topics.id
Filter: (count(articles.id) > 10)
Rows Removed by Filter: 5672
Buffers: shared hit=8624
-> Gather (cost=12018.39..12843.61 rows=7502 width=16) (actual time=198.146..205.348 rows=10376 loops=1) …
Run Code Online (Sandbox Code Playgroud)