Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 085C3632B1B for ; Tue, 24 Nov 2009 22:13:25 -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 83772-08 for ; Wed, 25 Nov 2009 02:13:17 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from outmail136082.authsmtp.co.uk (outmail136082.authsmtp.co.uk [62.13.136.82]) by mail.postgresql.org (Postfix) with ESMTP id E2F98632182 for ; Tue, 24 Nov 2009 22:13:16 -0400 (AST) Received: from mail-c187.authsmtp.com (mail-c187.authsmtp.com [62.13.128.33]) by punt5.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id nAP2DEJ4036159; Wed, 25 Nov 2009 02:13:14 GMT Received: from [172.16.1.107] ([61.194.37.73]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id nAP2DBB7055490; Wed, 25 Nov 2009 02:13:13 GMT Subject: Re: KNNGiST for knn-search From: Simon Riggs To: Teodor Sigaev Cc: Pgsql Hackers In-Reply-To: <4B0AC9E1.5050509@sigaev.ru> References: <4B0AC9E1.5050509@sigaev.ru> Content-Type: text/plain Date: Wed, 25 Nov 2009 02:13:10 +0000 Message-Id: <1259115190.27757.11194.camel@ebony> Mime-Version: 1.0 X-Mailer: Evolution 2.26.1 Content-Transfer-Encoding: 7bit X-Server-Quench: 1663f9fa-d968-11de-ab46-001185d377ca X-Report-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZOTQEd DAteCiN5VAwpPBRK HVkIKg5MJUcNSQVJ NksadBtFagBbY1ZH HGQLW11EUF97WGB/ ag4fawRDY0NKQQZt Tk9KXVdaExpoT0Jw ZhxqIGt1dwBDfHdw K0JrVnQVXUJ8dhB0 FB9JQWQAbXphaTRK TREKcwVJcANLfxdA PlN4UyBcLwdSbGoL PyYYHB0LBgAXCwJz bQA2ERocRlxDHTg6 AgsPFC8kHSUA X-Authentic-SMTP: 61633235383639.1000:706/Kp X-Virus-Status: No virus detected - but ensure you scan with your own anti-virus system. X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200911/1664 X-Sequence-Number: 150363 On Mon, 2009-11-23 at 20:44 +0300, Teodor Sigaev wrote: > 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 > > > 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 > 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. Sounds very cool. Seems like you should look at the way sorted_path works in query_planner(). If you have a query like this explain select col1 from s order by col1 limit 10; then we currently understand that we should use an IndexScan for that. We don't specifically exclude the bitmap scan, it's just that we know that the results from the index are ordered and therefore the cost of sorting the output need not be added. In the bitmap case the cost of the sort must be added and that's enough to ensure we almost never do that. I notice that a query like explain select col1 from s order by abs(col1 - 5) limit 10; is the one-dimensional equivalent of the type of query you're proposing and that doesn't work either until you put an index on abs(col1 - 5), then it just works, but only for k = 5. Maybe you should look at the above query and see if there are any usable similarities for the Knn index. Part of your problem appears to be that cost_sort does not include anything about the cost of the comparison operators for different datatypes. -- Simon Riggs www.2ndQuadrant.com