X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id F3424D1D200 for ; Thu, 29 Jan 2004 12:12:22 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 92997-05 for ; Thu, 29 Jan 2004 08:12:00 -0400 (AST) Received: from gizmo05ps.bigpond.com (gizmo05ps.bigpond.com [144.140.71.15]) by svr1.postgresql.org (Postfix) with SMTP id 1BF14D1D201 for ; Thu, 29 Jan 2004 08:11:50 -0400 (AST) Received: (qmail 16235 invoked from network); 29 Jan 2004 12:03:48 -0000 Received: from unknown (HELO psmam03.bigpond.com) (144.135.25.75) by gizmo05ps.bigpond.com with SMTP; 29 Jan 2004 12:03:48 -0000 Received: from cpe-203-45-149-167.qld.bigpond.net.au ([203.45.149.167]) by psmam03.bigpond.com(MAM REL_3_4_2 89/11636266) with SMTP id 11636266; Thu, 29 Jan 2004 22:11:57 +1000 Received: from [192.168.1.203] (helo=orion.mackay.local) by centauri.darrenmackay.com with esmtp (Exim 4.24) id 1AmB1c-0002n1-G6 for pgsql-sql@postgresql.org; Thu, 29 Jan 2004 22:12:00 +1000 Received: from orion.mackay.local (unverified) by orion.mackay.local (Content Technologies SMTPRS 4.2.5) with ESMTP id for ; Thu, 29 Jan 2004 22:18:08 +1000 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: query not using index for descending records? X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 Date: Thu, 29 Jan 2004 22:18:08 +1000 Content-class: urn:content-classes:message Message-ID: <035C9F7CE28601428BBB5B051C9F77F2017898@orion> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: query not using index for descending records? Thread-Index: AcODcHOY6tfixTn+QQ+mYkt5tzbVLxi7maEQ From: "email lists" To: X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/250 X-Sequence-Number: 16643 Hi All, I have this table; id - Serial datetime - timestamp without timezone with the index as index idx_trafficlogs_datetime_id on trafficlogs using btree (datetime,id); When performing the following query: explain select datetime,id from trafficlogs order by datetime,id limit 20; QUERY PLAN ------------------------------------------------------------------------ -------------------------------------------- Limit (cost=3D0.00..2.31 rows=3D20 width=3D12) -> Index Scan using idx_trafficlogs_datetime_id on trafficlogs (cost=3D0.00..1057.89 rows=3D9172 width=3D12) (2 rows) however, I am wanting to return the last 20 records. I have been using: explain select datetime,id from trafficlogs order by datetime,id desc limit 20; QUERY PLAN ------------------------------------------------------------------------ --------- Limit (cost=3D926.38..926.43 rows=3D20 width=3D12) -> Sort (cost=3D926.38..949.31 rows=3D9172 width=3D12) Sort Key: datetime, id -> Seq Scan on trafficlogs (cost=3D0.00..322.72 rows=3D9172 width=3D12) as you can see, a sequential scan is performed. How do I get pg to use an index scan for this query. The table in a production environment grows by approx 150,000 records daily, hence long term performance is a major factor here - for each additional day of data, the above query takes an additional 6-8 secs to run. Tia, Darren