A while ago I posted a message about optimizing a query in MySQL. I have since ported the data and query to PostgreSQL, but now PostgreSQL has the same problem. The solution in MySQL was to force the optimizer to not optimize using STRAIGHT_JOIN. PostgreSQL offers no such option.
Update Revised
I have isolated the part of the query that fixes the problem (d.month_ref_id = 1
):
select
d.*
from
daily d
join month_ref m on m.id = d.month_ref_id
join year_ref y on y.id = m.year_ref_id
where
m.category_id = '001' and
d.month_ref_id = 1
However, I can't hard-code a month reference to 1
. The query that produces a full table scan is:
select
d.*
from
daily d
join month_ref m on m.id = d.month_ref_id
join year_ref y on y.id = m.year_ref_id
where
m.category_id = '001'
The index on daily.month_ref_id
is:
CREATE INDEX daily_month_ref_idx
ON climate.daily
USING btree
(month_ref_id);
Why is the query performing a full table scan and what can be done to avoid it?
Thank you!