Replace constants in subquery

From: Dominik Moritz <domoritz(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Replace constants in subquery
Date: 2014-05-02 06:22:12
Message-ID: 8D041634-CD4F-499E-9FEF-D8700687DE86@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

We were looking into optimizing a very long running query and very very surprised by the query plan for a query. To illustrate the question, I created a sqlfiddle [0].

(1) The query we are running looks like this one:

SELECT *
FROM Foo o
WHERE
o.b = (SELECT b
FROM Foo i
WHERE i.a = o.a
ORDER BY b
LIMIT 1)
AND o.a = 2

(2) We made a minor change to this query by pushing the constant value (2) into the subquery:

SELECT *
FROM Foo o
WHERE
o.b = (SELECT b
FROM Foo i
WHERE i.a = 2
ORDER BY b
LIMIT 1)
AND o.a = 2

The difference in execution time was 5 orders of magnitude. For the former (1) query, Postgres executes the subquery for every row, while for the latter query (2) Postgres executes the subquery once and then uses its result.

(3) Finally, we made another modification and removed the subquery. Also, we have an index on Foo(a,b).

SELECT *
FROM Foo o
WHERE
o.b = 2
AND o.a = 2

Is about 2x faster than (2) because it will run as an index scan whereas we couldn't make (2) use the index -- it looks up the value of b once, then does a bitmap heap scan followed by a sort.

You might not be able to easily reproduce the difference between (2) and (3) because the decision whether to use an index or not depends on the data. However, we would be very interested in learning why (2) is so much faster than (1) even though they seem to be equivalent. Are we missing a corder case in which (1) and (2) have different semantics?

Thank you,
Dominik

[0] http://sqlfiddle.com/#!15/dec73/1

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Albe Laurenz 2014-05-02 09:58:14 Re: Replace constants in subquery
Previous Message David G Johnston 2014-05-02 01:39:30 Re: Insert image into bytea question