Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id B4D021337B7E for ; Thu, 17 Feb 2011 15:17:25 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 86896-07 for ; Thu, 17 Feb 2011 19:17:18 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id C79E41337B79 for ; Thu, 17 Feb 2011 15:17:17 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p1HJHH6U013094; Thu, 17 Feb 2011 14:17:17 -0500 (EST) To: Mark Stosberg cc: pgsql-performance@postgresql.org Subject: Re: application of KNN code to US zipcode searches? In-reply-to: References: <4D5CE118020000250003AB83@gw.wicourts.gov> <20110217152445.GV4116@tamriel.snowman.net> Comments: In-reply-to Mark Stosberg message dated "Thu, 17 Feb 2011 11:41:51 -0500" Date: Thu, 17 Feb 2011 14:17:17 -0500 Message-ID: <13093.1297970237@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/434 X-Sequence-Number: 42572 Mark Stosberg writes: > - The query planner didn't like it when the "ORDER BY" referred to a > column value instead of a static value, even when I believe it should > know that the column value never changes. See this pseudo-query where > we look-up the coordinates for 90210 once: > EXPLAIN ANALYZE > SELECT pets.pet_id, > zipcodes.lon_lat <-> center.lon_lat AS radius > FROM (SELECT lon_lat FROM zipcodes WHERE zipcode = '90210') AS > center, pets > JOIN shelters USING (shelter_id) > JOIN zipcodes USING (zipcode) > ORDER BY postal_codes.lon_lat <-> center.lon_lat limit 1000; As phrased, that's a join condition, so there's no way that an index on a single table can possibly satisfy it. You could probably convert it to a sub-select though: ORDER BY postal_codes.lon_lat <-> (SELECT lon_lat FROM zipcodes WHERE zipcode = '90210') limit 1000; regards, tom lane