Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id CC9F5632E31 for ; Tue, 24 Nov 2009 08:37:06 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 62348-08 for ; Tue, 24 Nov 2009 12:36:49 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og115.obsmtp.com (exprod7og115.obsmtp.com [64.18.2.217]) by mail.postgresql.org (Postfix) with SMTP id 84A76632D20 for ; Tue, 24 Nov 2009 08:36:55 -0400 (AST) Received: from source ([209.85.219.213]) by exprod7ob115.postini.com ([64.18.6.12]) with SMTP ID DSNKSwvTZTi+PIIGX7fzg4RbTQO4q3ArlmOQ@postini.com; Tue, 24 Nov 2009 04:36:55 PST Received: by ewy5 with SMTP id 5so3439853ewy.10 for ; Tue, 24 Nov 2009 04:36:53 -0800 (PST) Received: by 10.213.110.17 with SMTP id l17mr5099099ebp.18.1259066199170; Tue, 24 Nov 2009 04:36:39 -0800 (PST) Received: from ?192.168.1.118? ([88.195.103.165]) by mx.google.com with ESMTPS id 28sm2074456eye.1.2009.11.24.04.36.37 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 24 Nov 2009 04:36:38 -0800 (PST) Message-ID: <4B0BD389.5000509@enterprisedb.com> Date: Tue, 24 Nov 2009 14:37:29 +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> <4B0B8C30.2080400@enterprisedb.com> <4B0BC854.30705@sigaev.ru> In-Reply-To: <4B0BC854.30705@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=-2.508 tagged_above=-10 required=5 tests=AWL=0.091, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200911/1610 X-Sequence-Number: 150309 Teodor Sigaev wrote: >>> 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? > New algorithm works much more with memory for allocation/free to manage > lists and it's a single reason of performance loss. Choosing of > algorithm could not be done by consistent function, it should be done at > least in amrescan method or even earlier - in planner. Ok, that sounds good. The bottom line is that you can use the same on-disk tree with both algorithms. No need for a separate indexam in that case. > One idea: > SELECT p FROM pt WHERE p << '5.0,5.0'::point ORDER BY (p <-> > '5.0,5.0'::point) DESC LIMIT 10; > And add <-> to opclass (but for now any indexable operation should > return boolean type). You really shouldn't need to have a WHERE clause. > Of course, KNNGiST should be modified to support > not only k-nearest search but k-"farest" search and NULLS LAST/FIRST. Well, as long as the planner knows the capabilities of the indexam, it can just fall back to a seqscan+sort if the query can't be sped up with the index. > And now you can specify p >< 'one point' AND p >< 'another > point', but it's impossible to do that by ORDER BY clause. Huh, what does that mean? Is it like "ORDER BY (min( p >< 'one point', p >< 'another point')" ? > Second idea with non-standard syntax. > SELECT ... ORDER BY PROXIMITY OF expression[, expression [..]] TO > expression[, expression [..]] USING [operator [, operator [..]] > and operator is distance operator, i.e. it's not a member of btree > opclass, but returns non-negative float8 value. > > Without index it will be essentially the same as > ORDER BY expression operator expression[ + ..] DESC NULLS LAST We already have the syntax to represent the query, using ORDER BY. IMHO we just need to teach the planner that when it sees a query like that, it can use a GiST index to speed it up. A number of indexam and operator class API changes are probably required, but it should be invisible to the user. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com