Hi again,
/*Hedayat Vatankhah*/ wrote on Sun, 31 Jan 2016 01:20:53 +0330:
Hi,
/*David Rowley*/ wrote on Sun, 31 Jan 2016 04:57:04 +1300:
On 31 January 2016 at 01:30, Hedayat Vatankhah
<hedayat.fwd@xxxxxxxxx> wrote:
Personally, I expect both queries below to perform exactly the same:
SELECT
t1.id, *
FROM
t1
INNER JOIN
t2 ON t1.id = t2.id
where t1.id > -9223372036513411363;
And:
SELECT
t1.id, *
FROM
t1
INNER JOIN
t2 ON t1.id = t2.id
where t1.id > -9223372036513411363 and t2.id >
-9223372036513411363;
Unfortunately, they do not. PostgreSQL creates different plans for
these
queries, which results in very poor performance for the first one
compared
to the second (What I'm testing against is a DB with around 350 million
rows in t1, and slightly less in t2).
EXPLAIN output:
First query: http://explain.depesz.com/s/uauk
Second query: link: http://explain.depesz.com/s/uQd
Yes, unfortunately you've done about the only thing that you can do,
and that's just include both conditions in the query. Is there some
special reason why you can't just write the t2.id > ... condition in
the query too? or is the query generated dynamically by some software
that you have no control over?
I just found another issue with using a query like the second one (using
LEFT JOINs instead of INNER JOINs): referencing id columns of joined
tables explicitly disables PostgreSQL join removal optimization when you
only select column(s) from t1! :(
I should forget about creating views on top of JOIN queries, and build
appropriate JOIN queries with referenced table and appropriate
conditions manually, so the whole data model should be exposed to the
application.
If I'm not wrong, PostgreSQL should understand that ANY condition on t2
doesn't change the LEFT JOIN output when t2 columns are not SELECTed.
Regards,
Hedayat
--
Sent via pgsql-performance mailing list (pgsql-performance@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance