From: Craig Ringer <craig@postnewspapers.com.au>
To: Gurjeet Singh <singh.gurjeet@gmail.com>
Cc: Joe <dev@freedomcircle.net>
Cc: Gavin 'Beau' Baumanis <gavinb@eclinic.com.au>
Cc: pgsql-sql@postgresql.org
Subject: Re: Select into
Date: Thu, 20 Mar 2008 22:44:34 +0900
Message-ID: <47E26A42.5050106@postnewspapers.com.au> (raw)
In-Reply-To: <65937bea0803200613k4593da12hd62233f8a745309@mail.gmail.com>
References: <94F93C0F-DA95-4435-B253-57C90961DF34@eclinic.com.au>
<20080320104922.GB18935@a-kretschmer.de>
<65937bea0803200358i290bdc87hdeca777b593b5df1@mail.gmail.com>
<786D2B38-CE0A-41BF-9E70-4DEB22F8D9EF@eclinic.com.au>
<47E25447.6040109@freedomcircle.net>
<65937bea0803200613k4593da12hd62233f8a745309@mail.gmail.com>
Gurjeet Singh wrote:
> Except that it doesn't work... Did you try to execute that query; I am
> assuming not.
>
It does, or at least a query written to work the same way works fine for
me. Not only that, but at least in the presence of a unique index the
query planner optimises it to the same query plan as the one I proposed.
From my earlier test data:
craig=# update x set val = x2.val from x as x2 where x.id = 1000 and
x2.id = 1024;
UPDATE 1
craig=# select * from x where id in (1000,1024);
id | val
------+------
1024 | 1021
1000 | 1021
(2 rows)
craig=# explain update x set val = x2.val from x as x2 where x.id = 1000
and x2.id = 1024;
QUERY PLAN
---------------------------------------------------------------------------
Nested Loop (cost=0.00..16.55 rows=1 width=14)
-> Index Scan using x_id_idx on x (cost=0.00..8.27 rows=1 width=10)
Index Cond: (id = 1000)
-> Index Scan using x_id_idx on x x2 (cost=0.00..8.27 rows=1 width=4)
Index Cond: (x2.id = 1024)
(5 rows)
The above query actually executes slightly faster, presumably because
the query planner has to do less work to reach the same point than it
does with the subquery-based one I proposed. You should probably use
this one instead of the subquery one.
--
Craig Ringer
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: craig@postnewspapers.com.au, singh.gurjeet@gmail.com, dev@freedomcircle.net, gavinb@eclinic.com.au
Subject: Re: Select into
In-Reply-To: <47E26A42.5050106@postnewspapers.com.au>
* 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