Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id A92B063250C for ; Sat, 29 Aug 2009 03:28:10 -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 17458-09 for ; Sat, 29 Aug 2009 06:27:57 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f210.google.com (mail-fx0-f210.google.com [209.85.220.210]) by mail.postgresql.org (Postfix) with ESMTP id 61D0E632483 for ; Sat, 29 Aug 2009 03:27:59 -0300 (ADT) Received: by fxm6 with SMTP id 6so1916459fxm.19 for ; Fri, 28 Aug 2009 23:27:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=yBaMeWqzKpKSrfyNh1Zi4XmKvsrIZaXE3S8bUETiE9s=; b=SuEhXbiDI7oP3Gy+XukM7fbSuzXn6hvYrGyt6+5xXRmNEdWFq2rH5cT1MXYyCgXI2D n/cFAQauaK5gwiAWY3TpL3onf3aX+Si/MTZJJCfCu1lxoRo+KBI5ELAWcTn+A/MdAZsA 0HkaVEvpmgGWrRJtiP7q0ymBYpCuxBgVQUJ7Q= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=cKviDOOpPn4mrBnReWntJ6SOdTggkK3GPjr5vvlz9cLJWgkQqsZ06RcQ9hOa8/SMUP KG42hW93Jf0LCur0BGVXeFeCaKCh/+qCEWxy6pBEK8B8Vmxq6cROe3QHIhqtY4ME3DVp JQZsvIPhIHPDVno0wnbu8FMod8kP2p/Sb66Fw= MIME-Version: 1.0 Received: by 10.103.76.29 with SMTP id d29mr804812mul.50.1251527277017; Fri, 28 Aug 2009 23:27:57 -0700 (PDT) In-Reply-To: <4A9892E5.4010509@bretth.com> References: <4A9892E5.4010509@bretth.com> Date: Sat, 29 Aug 2009 09:27:56 +0300 Message-ID: <331e40660908282327y22c4e6d7wc4e6f69337d08a9f@mail.gmail.com> Subject: Re: Prepared Statement Query Planning From: =?KOI8-U?B?96bUwcymyiD0yc3eydvJzg==?= To: Brett Henderson Cc: pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary=0016e65aeed0c5fcbe047241e68f 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/80 X-Sequence-Number: 19929 --0016e65aeed0c5fcbe047241e68f Content-Type: text/plain; charset=ISO-8859-1 2009/8/29 Brett Henderson > > 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 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 --0016e65aeed0c5fcbe047241e68f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

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

This is the query:
SELECT e.id, e.version, e.tim= estamp, e.visible, u.data_public,
u.id AS user_id, u.display_na= me, e.changeset_id, e.latitude, e.longitude
FROM nodes e
INNER JOIN (
SELECT id, version FROM
nodes WHERE timestamp > ? AND timestamp <=3D ?
) t ON e.id =3D t.id AND e.version =3D t.version
INNER JOIN changesets c ON e.changeset_id =3D c.id
INNER JOIN users u ON c.user_id =3D 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 t= he lower end. =A0The server has 32 GB of RAM so can fit smaller tables in R= AM, but not the node table.

The query retrieves all rows within a timestamp range. =A0I realise the que= ry could be re-written without the sub-select, but it is implemented in thi= s way as a result of the way the query is dynamically constructed and allow= s the sub-select portion can be switched out for other row selection criter= ia.

It the subselect is only for switching out, it can b= e rewritten to:

SELECT e.= id, e.version, e.timestamp, e.visible, u.data_public,
u.id AS user_id, u.display_n= ame, e.changeset_id, e.latitude, e.longitude
FROM nodes e
INNER JOIN (
values (?,?)
) t(from, to) ON timestamp > = from AND timestamp <=3D to
INNER JOIN changesets c ON e.changeset_id =3D c.id
INNER JOIN users u ON c.user_id =3D 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.ti= mestamp, e.visible, u.data_public,
u.id AS user_id, u.display_n= ame, e.changeset_id, e.latitude, e.longitude
FROM (select * from nodes where timestamp > ? AND timestamp <=3D ?) e=
INNER JOIN changesets c ON e.changeset_id =3D c.id
INNER JOIN users u ON c.user_id =3D u.id

Best regards, Vitalii Tymchyshyn
--0016e65aeed0c5fcbe047241e68f--