Re: Speeding up subselect ?

From: Joe Conway <mail(at)joeconway(dot)com>
To: Peter Alberer <h9351252(at)obelix(dot)wu-wien(dot)ac(dot)at>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Speeding up subselect ?
Date: 2002-07-09 04:07:59
Message-ID: 3D2A619F.80000@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Peter Alberer wrote:
> Hi there,
>
> i have the following query in a pl/pgsql procedure:
>
> update lr_object_usage
> set status = (case status
> when ''OPEN_SUCC'' then ''CLOSED_SUCC''
> when ''OPEN_FAIL'' then ''CLOSED_FAIL'' end)
> where lr_object_usage_id in (select lr_object_usage_id from
> lr_locked_objects where context = p_exam_usage_id);
>
> the problem is the result of the subselect is obviously calculated for
> every row of lr_object_usage (30K rows) -> the update takes very long.
> Here is the explain output with p_exam_usage_id being 38191. Two simple
> selects with the result of the first one being used in the second one
> vs. the subselect. Divided into two selects
>
> Can I somehow tell the planer not to requery the subselect for every row

Try to recast the subselect as a FROM clause subselect. E.g. will this work?

update lr_object_usage
set status = (case status
when ''OPEN_SUCC'' then ''CLOSED_SUCC''
when ''OPEN_FAIL'' then ''CLOSED_FAIL'' end)
from (select lr_object_usage_id
from lr_locked_objects
where context = p_exam_usage_id) as t1
where lr_object_usage.lr_object_usage_id = t1.lr_object_usage_id;

HTH,

Joe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-07-09 04:11:10 Re: Frontend/backend protocol authentication
Previous Message Tom Lane 2002-07-09 03:45:36 Re: Odd new symptom - database locking up on a query