pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedUsing union to avoid creating temp table
2+ messages / 2 participants
[nested] [flat]
* Using union to avoid creating temp table
@ 2002-07-01 16:31 Bruno Wolff III <bruno@wolff.to>
2002-07-01 17:17 ` Re: Using union to avoid creating temp table Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 2+ messages in thread
From: Bruno Wolff III @ 2002-07-01 16:31 UTC (permalink / raw)
To: pgsql-sql
I don't know if this is a well known trick or not. I wanted to check a
list of values inputted to a script against some existing tables. I was
trying to think of alternatives to using a temp table and copy since
this would take two requests instead of one.
What I ended up doing is building the input values into sepearte
select statements unioned together. So if the input values were
'abc', 'def' and 'ghi', I would end up doing a select like:
select add.id from (select 'abc' union select 'def' union select 'ghi') add (id)where not exists (select from role where add.id = role.id)
I don't know whether or not this is faster in general, but it probably is
under at least some circumstances.
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Using union to avoid creating temp table
2002-07-01 16:31 Using union to avoid creating temp table Bruno Wolff III <bruno@wolff.to>
@ 2002-07-01 17:17 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2002-07-01 17:17 UTC (permalink / raw)
To: Bruno Wolff III <bruno@wolff.to>; +Cc: pgsql-sql
Bruno Wolff III <bruno@wolff.to> writes:
> What I ended up doing is building the input values into sepearte
> select statements unioned together. So if the input values were
> 'abc', 'def' and 'ghi', I would end up doing a select like:
> select add.id from (select 'abc' union select 'def' union select 'ghi') add (id)where not exists (select from role where add.id = role.id)
Might be worth writing "union all" not "union". This suppresses union's
check for duplicated rows, which you presumably don't need.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2002-07-01 17:17 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-07-01 16:31 Using union to avoid creating temp table Bruno Wolff III <bruno@wolff.to>
2002-07-01 17:17 ` Tom Lane <tgl@sss.pgh.pa.us>
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