Re: Wacky query plan, why?

From: Mike Mascari <mascarm(at)mascari(dot)com>
To: Maksim Likharev <mlikharev(at)aurigin(dot)com>
Cc: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Wacky query plan, why?
Date: 2003-07-26 00:25:13
Message-ID: 3F21CA69.7020004@mascari.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Maksim Likharev wrote:

> My be I too spoiled by MS SQL Server, but does'nt
> syntax:
>
> update prod.t_results set expdate=e.termdate from
> work.termdate e, prod.t_results r where e.docid=r.docid;
> or
> update prod.t_results set expdate=e.termdate from
> work.termdate e inner join prod.t_results r on e.docid=r.docid;
>
> is standard SQL-92 update FROM form?
> just trying to understand.

13.10 <update statement: searched>

Function

Update rows of a table.

Format

<update statement: searched> ::=
UPDATE <table name>
SET <set clause list>
[ WHERE <search condition> ]

So, for SQL92:

UPDATE prod.t_results
SET expdate = (
SELECT e.termdate
FROM work.termdate e
WHERE e.docid = prod.t_results.docid
);

If a 'termdate.docid' does not necessarily exist for every
't_results.docid' then you must further qualify the update to ensure
expdate won't be set to NULL (or die trying):

UPDATE prod.t_results
SET expdate = (
SELECT e.termdate
FROM work.termdate e
WHERE e.docid = prod.t_results.docid
)
WHERE EXISTS (
SELECT 1
FROM work.termdate e
WHERE e.docid = prod.t_results.docid
);

That's SQL92 and it's ugly. I prefer the PostgreSQL extended form:

UPDATE prod.t_results
SET expdate = work.termdate.termdate
WHERE prod.t_results.docid = work.termdate.docid;

Hope that helps,

Mike Mascari
mascarm(at)mascari(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2003-07-26 00:58:50 Re: Wacky query plan, why?
Previous Message Tom Lane 2003-07-25 23:55:06 Re: Using YY-MM-DD date input