Re: Performance of UNION vs IN

От: Torsten Förtsch
Тема: Re: Performance of UNION vs IN
Дата: ,
Msg-id: 532B21D6.2030309@gmx.net
(см: обсуждение, исходный текст)
Ответ на: Performance of UNION vs IN  (Jean-Max Reymond)
Ответы: Re: Performance of UNION vs IN  (Jean-Max Reymond)
Список: 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



В списке pgsql-performance по дате сообщения:

От: Torsten Förtsch
Дата:
Сообщение: Re: Performance of UNION vs IN
От: Stefan Amshey
Дата:
Сообщение: slow join not using index properly