From: | Jimmy A <jimmypsql(at)gmail(dot)com> |
---|---|
To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Cc: | pgsql-performance(at)lists(dot)postgresql(dot)org |
Subject: | Re: EXISTS by itself vs SELECT EXISTS much slower in query. |
Date: | 2021-11-09 01:08:50 |
Message-ID: | CANU97yu8=6-ctO-H95x-dNJNiPGVWU6Q03vAs95Myyt8NBzNwQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-performance |
I see, I never knew that.
Indeed there is a hashed subplan for the EXISTS by itself. So that explains
it.
Thanks Tom.
On Mon, Nov 8, 2021 at 12:35 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Jimmy A <jimmypsql(at)gmail(dot)com> writes:
> > I have two equivalent queries, one with an EXISTS clause by itself and
> one
> > wrapped in a (SELECT EXISTS) and the "naked" exists is much slower.
> > I would expect both to be the same speed / have same execution plan.
>
> That is a dangerous assumption. In general, wrapping (SELECT ...) around
> something has a significant performance impact, because it pushes Postgres
> to try to decouple the sub-select's execution from the outer query.
> As an example,
>
> postgres=# select x, random() from generate_series(1,3) x;
> x | random
> ---+---------------------
> 1 | 0.08595356832524814
> 2 | 0.6444265043474005
> 3 | 0.6878852071694332
> (3 rows)
>
> postgres=# select x, (select random()) from generate_series(1,3) x;
> x | random
> ---+--------------------
> 1 | 0.7028987801136708
> 2 | 0.7028987801136708
> 3 | 0.7028987801136708
> (3 rows)
>
> That's not a bug: it's expected that the second query will evaluate
> random() only once.
>
> In the case at hand, I suspect you're getting a "hashed subplan"
> in one query and not the other. The depesz.com display doesn't
> really show that, but EXPLAIN VERBOSE would.
>
> regards, tom lane
>
From | Date | Subject | |
---|---|---|---|
Next Message | Jiří Fejfar | 2021-11-11 19:20:57 | performance of analytical query |
Previous Message | Tom Lane | 2021-11-08 20:35:12 | Re: EXISTS by itself vs SELECT EXISTS much slower in query. |