我有以下查询:
select *
from radio_ratings rr
inner join radio_groups rg ON rg.id = rr.radio_group_id
where rg.owner_id = 3;
当运行EXPLAIN ANALYZE时,我看到:
Nested Loop (cost=0.84..79.45 rows=33 width=145) (actual time=0.023..36.320 rows=44122 loops=1)
-> Index Scan using radio_groups_owner_id_fkey on radio_groups rg (cost=0.42..8.94 rows=3 width=81) (actual time=0.009..0.011 rows=4 loops=1)
Index Cond: (owner_id = 3)
-> Index Scan using radio_ratings_radio_group_id_fkey on radio_ratings rr (cost=0.43..22.95 rows=55 width=64) (actual time=0.010..4.288 rows=11030 loops=4)
Index Cond: (radio_group_id = rg.id)
Planning time: 0.247 ms
Execution time: 43.154 ms
我不知道如何更改查询,以便PostgreSQL在第二次索引扫描时不会有错误的估计。看起来像一个简单的查询,带有经典的外键索引,但是PostgreSQL却搞砸了估计。
我该如何改善呢?