Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Hackers > Re: Internal de...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 9285 of 10966
Post > Topic >>

Re: Internal design of MERGE, with Rules

by simon@[EMAIL PROTECTED] (Simon Riggs) May 8, 2008 at 02:38 PM

On Wed, 2008-04-30 at 16:58 +0100, Simon Riggs wrote:
> The main query will then look like this
> 
> select   target.ctid
>         ,case when-not-matched (as above)
>         ,case when-matched (as above)
>         ,(all other columns required for side queries)  
> from <source-query> left outer join <target> on <join-condition>
> where (<when-matched-condition-0>
> or <when-matched-condition-1>
> ...
> or <when-matched-condition-N>)
> or (<when-not-matched-condition-0>
> or <when-not-matched-condition-1>
> ...
> or <when-not-matched-condition-N>)
> 
> The WHERE clause is likely required in case we get queries like this
> 
> MERGE target t
> USING (select * from source) s
> ON (s.pkey = t.pkey)
> WHEN MATCHED AND s.pkey = $1
>         UPDATE SET col = $2;
> 
> which would be perfectly valid, even if we might hope that they had
> coded like this
> 
> MERGE target
> USING (select * from source WHERE index-column = $1)
> ON (join-condition)
> WHEN MATCHED 
>         UPDATE SET col = $2; 

Peter has just jogged my memory about double evaluation of volatile
functions, so the above transformation isn't correct.

We would not be able to fully optimise a MERGE statement like this 

MERGE target t
USING (select * from source) s
 ON (s.pkey = t.pkey)
 WHEN MATCHED AND s.key = $1
        UPDATE SET col = $2;

since we won't be able to pass the clause "s.pkey = $1" down into the s
query so it would use an index. The following statement will be faster,
but will in all cases give an identical result:

MERGE target t
USING (select * from source WHERE key = $1) s
 ON (s.pkey = t.pkey)
 WHEN MATCHED
        UPDATE SET col = $2;

I don't think its too im****tant, since the latter is the way people
would have used MERGE in SQL:2003 anyway.

-- 
  Simon Riggs
  2ndQuadrant  http://www.2ndQuadrant.com


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
 




 4 Posts in Topic:
Internal design of MERGE, with Rules
simon@[EMAIL PROTECTED]   2008-04-30 16:58:52 
Re: Internal design of MERGE, with Rules
sam@[EMAIL PROTECTED] (S  2008-05-01 00:26:11 
Re: Internal design of MERGE, with Rules
simon@[EMAIL PROTECTED]   2008-05-01 17:02:32 
Re: Internal design of MERGE, with Rules
simon@[EMAIL PROTECTED]   2008-05-08 14:38:30 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Dec 1 13:47:16 CST 2008.