X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id D01419DD55A for ; Thu, 1 Dec 2005 00:58:03 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 73402-10 for ; Thu, 1 Dec 2005 00:58:00 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id B5DE19DCD5E for ; Thu, 1 Dec 2005 00:58:00 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id jB14w0e7006649; Wed, 30 Nov 2005 23:58:00 -0500 (EST) To: Ralph Mason cc: pgsql-performance@postgresql.org Subject: Re: Query is 800 times slower when running in function! In-reply-to: <438E4F12.7060205@telogis.com> References: <438E4F12.7060205@telogis.com> Comments: In-reply-to Ralph Mason message dated "Thu, 01 Dec 2005 14:17:06 +1300" Date: Wed, 30 Nov 2005 23:58:00 -0500 Message-ID: <6648.1133413080@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200512/1 X-Sequence-Number: 15822 Ralph Mason writes: > I have a simple query that is running inside a plpgsql function. > SELECT INTO _point_id id FROM ot2.point WHERE unit_id = _unit_id AND > time > _last_status ORDER BY time LIMIT 1; It would probably help significantly to make that be "ORDER BY unit_id, time". This'd eliminate the need for the separate sort step and encourage the planner to use the index, even when it does not know whether the "time > x" condition is selective or not. regards, tom lane