Re: same plan, different time - Mailing list pgsql-performance

From Tom Lane
Subject Re: same plan, different time
Date
Msg-id 1580.1090039384@sss.pgh.pa.us
Whole thread Raw
In response to same plan, different time  (Litao Wu <litaowu@yahoo.com>)
List pgsql-performance
Litao Wu <litaowu@yahoo.com> writes:
> SELECT *
> FROM user U LEFT JOIN user_timestamps T USING
> (user_id), user_alias A
> WHERE U.user_id = A.user_id  AND A.domain_id=7551070;

Ick.  Try changing the join order, perhaps

SELECT *
FROM (user U JOIN user_alias A ON (U.user_id = A.user_id))
     LEFT JOIN user_timestamps T USING (user_id)
WHERE A.domain_id=7551070;

As you have it, the entire LEFT JOIN has to be formed first,
and the useful restriction clause only gets applied later.

The fact that the case with 7551070 finishes quickly is just
blind luck --- the slow case is much more representative.

            regards, tom lane

pgsql-performance by date:

Previous
From: "Fred Moyer"
Date:
Subject: Scaling with lazy index updates
Next
From: Josh Berkus
Date:
Subject: Re: Scaling with lazy index updates