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

2009/8/29 Brett Henderson <brett@bretth.com>

>
> This is the query:
> 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 (
> SELECT id, version FROM
> nodes WHERE timestamp > ? AND timestamp <= ?
> ) 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 node table contains approx 500 million rows, the user table approx 100
> thousand rows, and the changeset table somewhere in between but closer to
> the lower end.  The server has 32 GB of RAM so can fit smaller tables in
> RAM, but not the node table.
>
> The query retrieves all rows within a timestamp range.  I realise the query
> could be re-written without the sub-select, but it is implemented in this
> way as a result of the way the query is dynamically constructed and allows
> the sub-select portion can be switched out for other row selection criteria.
>

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 <http://e.id/;
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

Best regards, Vitalii Tymchyshyn

view thread (9+ messages)  latest in thread

Message-ID: <331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.com>
Permalink:  ../331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.com/
Also on:    postgresql.org/message-id/331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.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: tivv00@gmail.com, brett@bretth.com
  Subject: Re: Prepared Statement Query Planning
  In-Reply-To: <331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.com>
  References: <4A9892E5.4010509@bretth.com> <331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.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