Re: execute same query only one time?

From: Vitaly Burovoy <vitaly(dot)burovoy(at)gmail(dot)com>
To: Johannes <jotpe(at)posteo(dot)de>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Subject: Re: execute same query only one time?
Date: 2016-02-08 20:17:12
Message-ID: CAKOSWNkYNGBMX4inzRuDZGEyygV6U4hpufcVSgUJJiGg5peiKw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2/8/16, Johannes <jotpe(at)posteo(dot)de> wrote:
> Am 08.02.2016 um 20:15 schrieb David G. Johnston:
>> On Mon, Feb 8, 2016 at 12:05 PM, Johannes <jotpe(at)posteo(dot)de> wrote:
>>
>>> Hi,
>>>
>>> is there a best practice to share data between two select statements?
>>>
>>> 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;
>>
>>
>> Please confirm:​
>>
>> ​You want the​ result of "SELECT max(id) FROM t0" to be used in the
>> second
>> query without having to recompute it?
>
> Yes.
>
>> What client are you using to execute these statements?
>
> JDBC. I execute both statements at once and iterate through the resultsets.
>
> Johannes

Hmm. Could you clarify why you don't want to pass id from the first
query to the second one:

select col1 from t1 where t0_id = value_id_from_the_first_query

--
Best regards,
Vitaly Burovoy

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Johannes 2016-02-08 20:21:32 Re: execute same query only one time?
Previous Message Johannes 2016-02-08 20:09:43 Re: execute same query only one time?