pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jens Schipkowski <jens.schipkowski@apus.co.at>
To: Arnaud Lesauvage <thewild@freesurf.fr>
Cc: pgsql-performance@postgresql.org
Subject: Re: Slow update with simple query
Date: Wed, 13 Dec 2006 14:20:40 +0100
Message-ID: <op.tkh10qgw81rjf6@xjens.apus.local> (raw)
In-Reply-To: <457FF0CD.4050606@freesurf.fr>
References: <457FDB1E.6040801@freesurf.fr>
	<op.tkhy4jpg81rjf6@xjens.apus.local>
	<457FF0CD.4050606@freesurf.fr>

On Wed, 13 Dec 2006 13:23:41 +0100, Arnaud Lesauvage <thewild@freesurf.fr>  
wrote:

> Jens Schipkowski a écrit :
>> the problem is a combination of bad formed SQL and maybe missing  
>> indexes.
>> try this:
>> UPDATE t1
>> SET booleanfield = foo.bar
>>  FROM (SELECT uid,(field IN ('some','other') AND field2 = 'Y') AS bar  
>> FROM  t2) AS foo
>> WHERE t1.uid=foo.uid;
>
>
> Hi Jens,
> Why is this query better than the other one ? Because it runs the  
> "(field IN ('some','other') AND field2 = 'Y')" once and then executes  
> the join with the resulting set ?
True. The Subselect in FROM clause will be executed once and will be  
joined using the condition at where clause. So your condition at t2 is not  
executed for each row in t1(2mio records) but for each row in t2(1k  
records). And the boolean value is already set during update.

regards,
Jens

>
>> and index t1.uid, t2.uid, t2.field, t2.field2
>
> t1.field can only take 3 or 4 values (don't remember exactly), and  
> field2 only 2 ('Y' or 'N'). So this fields have a very low cardinality.
> Won't the planner chose to do a table scan in such a case ?
>
> Thanks for your advices !
>
> --
> Arnaud



-- 
**
APUS Software GmbH



view thread (21+ messages)  latest in thread

Message-ID: <op.tkh10qgw81rjf6@xjens.apus.local>
Permalink:  ../op.tkh10qgw81rjf6@xjens.apus.local/
Also on:    postgresql.org/message-id/op.tkh10qgw81rjf6@xjens.apus.local

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: jens.schipkowski@apus.co.at, thewild@freesurf.fr
  Subject: Re: Slow update with simple query
  In-Reply-To: <op.tkh10qgw81rjf6@xjens.apus.local>
  References: <457FDB1E.6040801@freesurf.fr> <op.tkhy4jpg81rjf6@xjens.apus.local> <457FF0CD.4050606@freesurf.fr> <op.tkh10qgw81rjf6@xjens.apus.local>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox