Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 844EE6332CD for ; Sat, 29 Aug 2009 04:58:22 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 30669-06 for ; Sat, 29 Aug 2009 07:58:06 +0000 (UTC) X-Greylist: delayed 00:06:52.841166 by SQLgrey-1.7.6 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.251]) by mail.postgresql.org (Postfix) with ESMTP id 4DFA0632533 for ; Sat, 29 Aug 2009 04:58:10 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c3so1149731ana.28 for ; Sat, 29 Aug 2009 00:58:09 -0700 (PDT) Received: by 10.101.85.10 with SMTP id n10mr2213270anl.156.1251532275759; Sat, 29 Aug 2009 00:51:15 -0700 (PDT) Received: from ?192.168.1.113? ([203.123.90.2]) by mx.google.com with ESMTPS id 23sm1220193yxe.12.2009.08.29.00.51.09 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sat, 29 Aug 2009 00:51:14 -0700 (PDT) Message-ID: <4A98DDE4.4060509@bretth.com> Date: Sat, 29 Aug 2009 17:51:00 +1000 From: Brett Henderson User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 To: =?UTF-8?B?0JLRltGC0LDQu9GW0Lkg0KLQuNC80YfQuNGI0LjQvQ==?= CC: pgsql-jdbc@postgresql.org Subject: Re: Prepared Statement Query Planning References: <4A9892E5.4010509@bretth.com> <331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.com> In-Reply-To: <331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.com> Content-Type: multipart/alternative; boundary="------------010600040302010709070401" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.598 tagged_above=-10 required=5 tests=BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200908/81 X-Sequence-Number: 19930 This is a multi-part message in MIME format. --------------010600040302010709070401 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Віталій Тимчишин wrote: > It the subselect is only for switching out, it can be rewritten to: > > SELECT e.id , e.version, e.timestamp, e.visible, > u.data_public, > u.id AS user_id, u.display_name, e.changeset_id, > e.latitude, e.longitude > FROM nodes e > INNER JOIN ( > values (?,?) > ) t(from, to) ON timestamp > from AND timestamp <= to > INNER JOIN changesets c ON e.changeset_id = c.id > INNER JOIN users u ON c.user_id = u.id > > Still don't know if this will make things better or worse. > Another (better) option could be not to add a join, but replace > original "from nodes e" in this case: > SELECT e.id , e.version, e.timestamp, e.visible, > u.data_public, > u.id AS user_id, u.display_name, e.changeset_id, > e.latitude, e.longitude > FROM (select * from nodes where timestamp > ? AND timestamp <= ?) e > INNER JOIN changesets c ON e.changeset_id = c.id > INNER JOIN users u ON c.user_id = u.id Thanks for the tips. I haven't see the first style before, and hadn't considered the second. However I don't think it will work in my case. I currently have three different ways of selecting records, 1. by timestamp range (as in my initial example), 2. by records in a temp table, and 3. unrestricted. The unrestricted example isn't an issue because a full table scan is appropriate in that case. However the temp table one is a different matter. In that case the query looks like this: SELECT e.id, e.version, e.timestamp, e.visible, u.data_public, u.id AS user_id, u.display_name, e.changeset_id, e.latitude, e.longitude FROM nodes e INNER JOIN tmp_nodes t ON e.id = t.id AND e.version = t.version INNER JOIN changesets c ON e.changeset_id = c.id INNER JOIN users u ON c.user_id = u.id The tmp_nodes table contains only two columns id and version which have been built up by previous queries. Most of the query remains identical, but instead of doing a "SELECT id, version FROM nodes WHERE timestamp > ? AND timestamp <= ?", I join to tmp_nodes which contains only the records I'm interested in. Originally I was creating a temp table in the timestamp range case as well but moved away in an attempt to get better performance, I suspect I was encountering bad query plans in that case as well. --------------010600040302010709070401 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit Віталій Тимчишин wrote:
It the subselect is only for switching out, it can be rewritten to:

SELECT e.id, e.version, e.timestamp, e.visible, u.data_public,
u.id AS user_id, u.display_name, e.changeset_id, e.latitude, e.longitude
FROM nodes e
INNER JOIN (
values (?,?)
) t(from, to) ON timestamp > from AND timestamp <= to
INNER JOIN changesets c ON e.changeset_id = c.id
INNER JOIN users u ON c.user_id = u.id

Still don't know if this will make things better or worse.
Another (better) option could be not to add a join, but replace original "from nodes e" in this case:
SELECT e.id, e.version, e.timestamp, e.visible, u.data_public,
u.id AS user_id, u.display_name, e.changeset_id, e.latitude, e.longitude
FROM (select * from nodes where timestamp > ? AND timestamp <= ?) e
INNER JOIN changesets c ON e.changeset_id = c.id
INNER JOIN users u ON c.user_id = u.id
Thanks for the tips.  I haven't see the first style before, and hadn't considered the second.

However I don't think it will work in my case.  I currently have three different ways of selecting records, 1. by timestamp range (as in my initial example), 2. by records in a temp table, and 3. unrestricted.  The unrestricted example isn't an issue because a full table scan is appropriate in that case.  However the temp table one is a different matter.  In that case the query looks like this:

SELECT e.id, e.version, e.timestamp, e.visible, u.data_public,
u.id AS user_id, u.display_name, e.changeset_id, e.latitude, e.longitude
FROM nodes e
INNER JOIN
tmp_nodes
t ON e.id = t.id AND e.version = t.version
INNER JOIN changesets c ON e.changeset_id = c.id
INNER JOIN users u ON c.user_id = u.id

The tmp_nodes table contains only two columns id and version which have been built up by previous queries.  Most of the query remains identical, but instead of doing a "SELECT id, version FROM nodes WHERE timestamp > ? AND timestamp <= ?", I join to tmp_nodes which contains only the records I'm interested in.  Originally I was creating a temp table in the timestamp range case as well but moved away in an attempt to get better performance, I suspect I was encountering bad query plans in that case as well.

--------------010600040302010709070401--