Using union to avoid creating temp table
Using union to avoid creating temp table
От:
Bruno Wolff III <bruno@wolff.to>
Дата:
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.
Re: Using union to avoid creating temp table
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Bruno Wolff III 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