Using union to avoid creating temp table

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: pgsql-sql(at)postgresql(dot)org
Subject: Using union to avoid creating temp table
Date: 2002-07-01 16:31:40
Message-ID: 20020701163140.GA22475@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2002-07-01 17:17:30 Re: Using union to avoid creating temp table
Previous Message Simeone, Mario 2002-07-01 16:28:11 unsubscribe