Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 0CD5A63239E for ; Tue, 24 Nov 2009 03:32:27 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 33463-08 for ; Tue, 24 Nov 2009 07:32:20 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og119.obsmtp.com (exprod7og119.obsmtp.com [64.18.2.16]) by mail.postgresql.org (Postfix) with SMTP id F3119632225 for ; Tue, 24 Nov 2009 03:32:17 -0400 (AST) Received: from source ([209.85.219.210]) by exprod7ob119.postini.com ([64.18.6.12]) with SMTP ID DSNKSwuMAKAVaqhrGaXo/ridkqgqQbqHMRel@postini.com; Mon, 23 Nov 2009 23:32:18 PST Received: by ewy2 with SMTP id 2so2486033ewy.35 for ; Mon, 23 Nov 2009 23:32:16 -0800 (PST) Received: by 10.213.2.75 with SMTP id 11mr463803ebi.9.1259047935953; Mon, 23 Nov 2009 23:32:15 -0800 (PST) Received: from ?192.168.1.118? ([88.195.103.165]) by mx.google.com with ESMTPS id 5sm2166167eyh.32.2009.11.23.23.32.13 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 23 Nov 2009 23:32:14 -0800 (PST) Message-ID: <4B0B8C30.2080400@enterprisedb.com> Date: Tue, 24 Nov 2009 09:33:04 +0200 From: Heikki Linnakangas Organization: EnterpriseDB User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706) MIME-Version: 1.0 To: Teodor Sigaev CC: Pgsql Hackers Subject: Re: KNNGiST for knn-search References: <4B0AC9E1.5050509@sigaev.ru> In-Reply-To: <4B0AC9E1.5050509@sigaev.ru> Content-Type: text/plain; charset=KOI8-R Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-6.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599, RCVD_IN_DNSWL_MED=-4 X-Spam-Level: X-Archive-Number: 200911/1589 X-Sequence-Number: 150288 Teodor Sigaev wrote: > we'd like to present contrib module for CVS HEAD, which contains > implementation of knn (k nearest neighbourhood) search in PostgreSQL, > see README.knngist for > details. Cool! > Old way: > SELECT coordinates, (coordinates <-> '5.0,5.0'::point) AS dist FROM spots > order by dist asc LIMIT 10; > > Time: 1024.242 ms > > knn-search: > SELECT coordinates, (coordinates <-> '5.0,5.0'::point) AS dist FROM spots > WHERE coordinates >< '5.0,5.0'::point LIMIT 10; > > Time: 3.158 ms I think you'll need to work on that. A WHERE qual shouldn't imply a sort order. You'll have to teach the planner how to use the index to speed up a query in the first form. > We didn't patch existing implementation of GiST for several reasons: > > 1. KNNGiST is about 5% slower than GiST on non-knn search queries, like > contains or contained by, because of some overhead of new algorithm of > tree traversal Is it possible to use the regular GiST traversal algorithm on a KNNGiST-tree, when performing regular GiST searches that don't require a particular order? > 2. KNNGiST can't be used in bitmap index scan, which destroys order of > results, > We don't know the way to forbid bitmap index scan only for knn queries. > Current version of KNNGiST doesn't distinguish knn-search and usual > search > and postgres doesn't know about ordered output from KNNGiST. Yeah, you really need to modify the planner to understand the ordering and plan accordingly. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com