Nicolas Seinlet <nicolas@xxxxxxxxxxx> writes: > Many thanks for the fast response. The full query plan is below, and the Dalibo link points to it as well. You're right, it's a merge join. One table doesn't have up-to-date statistics, because the table is filled during the transaction involving this query. Sure looks like incorporating an ANALYZE into the transaction would help matters. This misestimation: > -> Seq Scan on account_invoice_line il (cost=0.00..9885.48 rows=752 width=22) (actual time=0.049..121.238 rows=150448 loops=1) > Filter: (display_type IS NULL) looks like it's really hurting you, and "ANALYZE account_invoice_line" after loading that table ought to be enough to fix that. regards, tom lane