Re: execute same query only one time?

From: Vitaly Burovoy <vitaly(dot)burovoy(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: Johannes <jotpe(at)posteo(dot)de>, pgsql-general(at)postgresql(dot)org
Subject: Re: execute same query only one time?
Date: 2016-02-08 19:32:17
Message-ID: CAKOSWN=B7pR6DsHJHsBLN4ZjXiovJvhmd1+pWWPoE2UKteWbvQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2/8/16, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com> wrote:
> On 02/08/2016 11:05 AM, Johannes wrote:
>> Imaging following situation: I want to receive two result sets from two
>> tables, referring to a specific id from table t0 AND I try not to query
>> for that specific id a second time.
>
>> Table t0 returns 1 row and table t1 returns multiple rows.
>>
>> begin;
>> select id, col1, col2, ... from t0 where id = (select max(id) from t0
>> where col1 = value1 and col2 = value2 and ...);
>> select col1 from t1 where t0_id = (select max(id) from t0 where col1 =
>> value1 and col2 = value2 and ...);
>> commit;
>>
>> Best regards Johannes
>
> 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.

>
> --
> Adrian Klaver
> adrian(dot)klaver(at)aklaver(dot)com

--
Best regards,
Vitaly Burovoy

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Vitaly Burovoy 2016-02-08 19:40:34 Re: execute same query only one time?
Previous Message Chris Travers 2016-02-08 19:26:11 Re: Let's Do the CoC Right