Re: execute same query only one time?

From: Johannes <jotpe(at)posteo(dot)de>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: execute same query only one time?
Date: 2016-02-08 20:46:05
Message-ID: 56B8FE8D.4090703@posteo.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Am 08.02.2016 um 21:33 schrieb Vitaly Burovoy:
> On 2/8/16, Johannes <jotpe(at)posteo(dot)de> wrote:
>>
>> Am 08.02.2016 um 20:32 schrieb Vitaly Burovoy:
>>> On 2/8/16, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com> wrote:
>>>> Based on rough guess of the above, without seeing actual table schemas:
>>>>
>>>> select id, t0.col1, t1.col1, col2, ... from t0 JOIN t1 ON t0.id =
>>>> t1.t0_id where id = (select max(id) from t0 where col1 = value1 and col2
>>>> = value2 and ...);
>>>
>>> I don't think it is a good solution because it leads to copying
>>> columns from the t0 which is wasting net traffic and increasing
>>> complexity at the client side. Moreover it works iff t0 returns only
>>> one row.
>>
>> I had same doubts.
>> CTE would be first class, if it was be reusable for other statements.
>>
>> Johannes
>
> CTEs are temporary tables for a _statement_ for using a single
> statement instead of several ones (create temp table, insert into,
> select from it, select from it, drop temp table).
>
> But it is not your case because CTEs are for a queries which return a
> single set of rows. Your case is returning two sets (one row with
> several columns from t0 and several rows with a single columns from
> t1).

Sure.
Thanks for the temporary table example!

Johannes

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2016-02-08 20:46:10 Re: execute same query only one time?
Previous Message Vitaly Burovoy 2016-02-08 20:33:36 Re: execute same query only one time?