pg.ddx.io pgsql-performance@postgresql.org mailing list archive
help / color / mirror / Atom feedPerformance of UNION vs IN
3+ messages / 2 participants
[nested] [flat]
* Performance of UNION vs IN
@ 2014-03-20 16:57 Jean-Max Reymond <jmreymond@free.fr>
0 siblings, 1 reply; 3+ messages in thread
From: Jean-Max Reymond @ 2014-03-20 16:57 UTC (permalink / raw)
To: pgsql-performance
I have a very complex view zinfoexp and running the view as:
SELECT * FROM zinfoexp WHERE idmembre in (1,84)
take 2700 ms
So, I try another syntax:
SELECT * FROM zinfoexp WHERE idmembre = 1
union
SELECT * FROM zinfoexp WHERE idmembre = 84
and for me, two calls to my view takes a lot of time (may be x2) and it
takes 134 ms !
How is it possible that the optimizer cannot optimize the IN as UNION ?
I have a database postgresql 9.3 freshly vacuumed
--
Jean-Max Reymond
CKR Solutions Open Source http://www.ckr-solutions.com
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Performance of UNION vs IN
@ 2014-03-20 17:13 Torsten Förtsch <torsten.foertsch@gmx.net>
parent: Jean-Max Reymond <jmreymond@free.fr>
0 siblings, 1 reply; 3+ messages in thread
From: Torsten Förtsch @ 2014-03-20 17:13 UTC (permalink / raw)
To: Jean-Max Reymond <jmreymond@free.fr>; pgsql-performance
On 20/03/14 17:57, Jean-Max Reymond wrote:
> I have a very complex view zinfoexp and running the view as:
> SELECT * FROM zinfoexp WHERE idmembre in (1,84)
> take 2700 ms
>
> So, I try another syntax:
> SELECT * FROM zinfoexp WHERE idmembre = 1
> union
> SELECT * FROM zinfoexp WHERE idmembre = 84
>
> and for me, two calls to my view takes a lot of time (may be x2) and it
> takes 134 ms !
try
SELECT * FROM zinfoexp WHERE idmembre=1 OR idmembre=84
This will probably be even faster.
Also, the 2 statements of your's are not semantically equal. UNION
implies DISTINCT, see:
select * from (values (1), (1), (2)) t(i) UNION select 19;
i
----
19
1
2
(3 rows)
What you want is UNION ALL:
select * from (values (1), (1), (2)) t(i) UNION ALL select 19;
i
----
1
1
2
19
(4 rows)
Torsten
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Performance of UNION vs IN
@ 2014-03-21 05:33 Jean-Max Reymond <jmreymond@free.fr>
parent: Torsten Förtsch <torsten.foertsch@gmx.net>
0 siblings, 0 replies; 3+ messages in thread
From: Jean-Max Reymond @ 2014-03-21 05:33 UTC (permalink / raw)
To: pgsql-performance
Le 20/03/2014 18:13, Torsten Förtsch a écrit :
> On 20/03/14 17:57, Jean-Max Reymond wrote:
>> I have a very complex view zinfoexp and running the view as:
>> SELECT * FROM zinfoexp WHERE idmembre in (1,84)
>> take 2700 ms
>>
>> So, I try another syntax:
>> SELECT * FROM zinfoexp WHERE idmembre = 1
>> union
>> SELECT * FROM zinfoexp WHERE idmembre = 84
>>
>> and for me, two calls to my view takes a lot of time (may be x2) and it
>> takes 134 ms !
>
> try
>
> SELECT * FROM zinfoexp WHERE idmembre=1 OR idmembre=84
>
> This will probably be even faster.
>
> Also, the 2 statements of your's are not semantically equal. UNION
> implies DISTINCT, see:
>
> select * from (values (1), (1), (2)) t(i) UNION select 19;
> i
> ----
> 19
> 1
> 2
> (3 rows)
>
> What you want is UNION ALL:
>
> select * from (values (1), (1), (2)) t(i) UNION ALL select 19;
> i
> ----
> 1
> 1
> 2
> 19
> (4 rows)
>
>
> Torsten
>
same numbers with DISTINCT and UNION ALL (construction of VIEW does an
implicit DISTINCT).
--
Jean-Max Reymond
Éruption de l'Etna: http://jmreymond.free.fr/Etna2002
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2014-03-21 05:33 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-03-20 16:57 Performance of UNION vs IN Jean-Max Reymond <jmreymond@free.fr>
2014-03-20 17:13 ` Torsten Förtsch <torsten.foertsch@gmx.net>
2014-03-21 05:33 ` Jean-Max Reymond <jmreymond@free.fr>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox