Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id AF5BB9FBDF0 for ; Wed, 14 Feb 2007 18:32:53 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 08235-03 for ; Wed, 14 Feb 2007 18:32:46 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from paul-ramseys-computer.local (mail.refractions.net [24.68.236.214]) by postgresql.org (Postfix) with ESMTP id 0928E9FBD49 for ; Wed, 14 Feb 2007 18:32:45 -0400 (AST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by paul-ramseys-computer.local (Postfix) with ESMTP id 7389723372D; Tue, 13 Feb 2007 19:01:35 -0800 (PST) In-Reply-To: <1d4e0c10702130909g3be0a10fpf2594574c84866c9@mail.gmail.com> References: <1d4e0c10702130909g3be0a10fpf2594574c84866c9@mail.gmail.com> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <0BD9E842-2DA7-4F90-A732-AFD60C1F50C7@refractions.net> Cc: "Pg Performance list" Content-Transfer-Encoding: 7bit From: Paul Ramsey Subject: Re: Proximity query with GIST and row estimation Date: Tue, 13 Feb 2007 19:01:34 -0800 To: "Guillaume Smet" X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/162 X-Sequence-Number: 23216 You'll find that PostGIS does a pretty good job of selectivity estimation. P On 13-Feb-07, at 9:09 AM, Guillaume Smet wrote: > Hi all, > > Following the work on Mark Stosberg on this list (thanks Mark!), I > optimized our slow proximity queries by using cube, earthdistance > (shipped with contrib) and a gist index. The result is globally very > interesting apart for a specific query and we'd like to be able to fix > it too to be more consistent (it's currently faster with a basic > distance calculation based on acos, cos and so on but it's slow > anyway). > > The problem is that we have sometimes very few places near a given > location (small city) and sometimes a lot of them (in Paris, Bruxelles > and so on - it's the case we have here). The gist index I created > doesn't estimate the number of rows in the area very well. > > Table: lieu (100k rows) with wgslat and wgslon as numeric > Table: lieugelieu (200k rows, 1k with codegelieu = 'PKG') > Index: "idx_lieu_earth" gist (ll_to_earth(wgslat::double precision, > wgslon::double precision)) > > The simplified query is: > SELECT DISTINCT l.numlieu, l.nomlieu, ROUND > (earth_distance(ll_to_earth(48.85957600, 2.34860800), > ll_to_earth(l.wgslat, l.wgslon))) as dist > FROM lieu l, lieugelieu lgl > WHERE lgl.codegelieu = 'PKG' AND earth_box(ll_to_earth(48.85957600, > 2.34860800), 1750) @ ll_to_earth(l.wgslat, l.wgslon) AND lgl.numlieu = > l.numlieu ORDER BY dist ASC LIMIT 2; > It's used to find the nearest car parks from a given location. > > The plan is attached plan_earthdistance_nestedloop.txt. It uses a > nested loop because the row estimate is pretty bad: (cost=0.00..3.38 > rows=106 width=0) (actual time=30.229..30.229 rows=5864 loops=1). > > If I disable the nested loop, the plan is different and faster (see > plan_earthdistance_hash.txt attached). > > Is there any way to improve this estimation? I tried to set the > statistics of wgslat and wgslon higher but it doesn't change anything > (I don't know if the operator is designed to use the statistics). > > Any other idea to optimize this query is very welcome too. > > -- > Guillaume > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 6: explain analyze is your friend