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 E969ED1B4AC for ; Fri, 30 Jan 2004 09:57:35 +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 42137-04 for ; Fri, 30 Jan 2004 05:57:37 -0400 (AST) Received: from smtp.istop.com (dci.doncaster.on.ca [66.11.168.194]) by svr1.postgresql.org (Postfix) with ESMTP id 2733CD1B47D for ; Fri, 30 Jan 2004 05:57:35 -0400 (AST) Received: from stark.xeocode.com (gsstark.mtl.istop.com [66.11.160.162]) by smtp.istop.com (Postfix) with ESMTP id 7CBAB36DE4; Fri, 30 Jan 2004 04:56:22 -0500 (EST) Received: from localhost ([127.0.0.1] helo=stark.xeocode.com) by stark.xeocode.com with smtp (Exim 3.36 #1 (Debian)) id 1AmVNu-0005Jn-00; Fri, 30 Jan 2004 04:56:22 -0500 To: Cc: , "Bruno Wolff III" , Subject: Re: limit 1 and functional indexes: SOLVED References: In-Reply-To: From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 30 Jan 2004 04:56:22 -0500 Message-ID: <87smhxyd2x.fsf@stark.xeocode.com> Lines: 51 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/283 X-Sequence-Number: 16676 "Alexandra Birch" writes: > It works perfectly - thanks a million! > Strangely the offset 0 does not seem to make any difference. > Gotta read up more about subqueries :) > > explain analyze > select code,order_date > from ( > select code, order_date > from transactions > where UPPER(pop) = UPPER('c892eb2f877e3a28ddc8e196cd5a8aad') > limit 1 > ) as foo > order by order_date DESC; I think what you're trying to do here is get the last order? Then you'll want the limit to be on the outer query where it's ordered by order_date: select code,order_date from ( select code, order_date from transactions where UPPER(pop) = UPPER('c892eb2f877e3a28ddc8e196cd5a8aad') offset 0 ) as foo order by order_date DESC; limit 1 Note that in theory postgres should be able to find the same plan for this query as yours since it's equivalent. It really ought to use the order_date index since it thinks it would be more efficient. However it's unable to because postgres doesn't try every possible index, only the ones that look like they'll be useful for a where clause or an order by. And the order by on the outer query isn't considered when it's looking at the subquery. It normally handles this case by merging the subquery into the outer query, but it can't do that if there's a limit or offset. So an "offset 0" is convenient for fooling it into thinking the subquery can't be pulled up without actually changing the output. You could do "order by upper(pop)" instead which might be clearer for someone reading the query in that it makes it look like you're trying to encourage it to use the index on upper(pop). In theory "order by"s on subqueries are useless and postgres could ignore them, but it doesn't. -- greg