pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Brett Henderson <brett@bretth.com>
To: Віталій Тимчишин <tivv00@gmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Prepared Statement Query Planning
Date: Sat, 29 Aug 2009 17:51:00 +1000
Message-ID: <4A98DDE4.4060509@bretth.com> (raw)
In-Reply-To: <331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.com>
References: <4A9892E5.4010509@bretth.com>
	<331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.com>

Віталій Тимчишин wrote:
> It the subselect is only for switching out, it can be rewritten to:
>
> SELECT e.id <http://e.id/;, e.version, e.timestamp, e.visible, 
> u.data_public,
> u.id <http://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 <http://e.id/;
> INNER JOIN changesets c ON e.changeset_id = c.id <http://c.id/;
> INNER JOIN users u ON c.user_id = u.id <http://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 <http://e.id/;, e.version, e.timestamp, e.visible, 
> u.data_public,
> u.id <http://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 <http://c.id/;
> INNER JOIN users u ON c.user_id = u.id <http://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.

view thread (9+ messages)  latest in thread

Message-ID: <4A98DDE4.4060509@bretth.com>
Permalink:  ../4A98DDE4.4060509@bretth.com/
Also on:    postgresql.org/message-id/4A98DDE4.4060509@bretth.com

 · 

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-jdbc@postgresql.org
  Cc: brett@bretth.com, tivv00@gmail.com
  Subject: Re: Prepared Statement Query Planning
  In-Reply-To: <4A98DDE4.4060509@bretth.com>
  References: <4A9892E5.4010509@bretth.com> <331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.com> <4A98DDE4.4060509@bretth.com>

* 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