X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 3C4FB52AA6 for ; Mon, 15 Aug 2005 06:15:23 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 41534-03 for ; Mon, 15 Aug 2005 09:15:19 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 693DA52AA1 for ; Mon, 15 Aug 2005 06:15:18 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id DF9844087F8; Mon, 15 Aug 2005 10:15:14 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id CCBEE15EDA; Mon, 15 Aug 2005 10:13:37 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 26087-07; Mon, 15 Aug 2005 10:13:34 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 2C7F915ED9; Mon, 15 Aug 2005 10:13:34 +0100 (BST) Message-ID: <43005CBD.5070703@archonet.com> Date: Mon, 15 Aug 2005 10:13:33 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: =?ISO-8859-1?Q?Poul_M=F8ller_Hansen?= Cc: pgsql-general@postgresql.org Subject: Re: Optimizing query References: <43005482.7020105@pbnet.dk> In-Reply-To: <43005482.7020105@pbnet.dk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.057 required=5 tests=[AWL=0.007, FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200508/715 X-Sequence-Number: 82123 Poul M=F8ller Hansen wrote: > I have a problem creating a usable index for the following simple query= : > SELECT * FROM my.table WHERE node =3D '10' ORDER BY id DESC LIMIT 1 >=20 > id is a serial, so the query is to find the latest entry to a given nod= e=20 > and id is the primary key. You're not necessarily getting the latest entry, just the one with the=20 highest "id". Sequences guarantee uniqueness but if you have concurrent=20 inserts not necessarily ordering. > The table contains around 1 million records and the query takes around = 2=20 > seconds. Well, you don't say how many different values for "node" there are, nor=20 how many rows you would expect where node=3D'10'. > I have tried to make an index on node and also on both id & node, but i= s=20 > doesn't lower the query time. Difficult to say what's happening since you don't supply any EXPLAIN=20 ANALYSE output. However, if you have an index on (node,id) you might want to try: SELECT ... ORDER BY node DESC, id DESC LIMIT 1; That way the "ORDER BY" part clearly tells the planner that a=20 reverse-order on your index will be useful. -- Richard Huxton Archonet Ltd