<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>

<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7638.1">
<TITLE>Re: [postgis-users] Re: GiST index seems to be ignored?</TITLE>
</HEAD>
<BODY>
<DIV id=idOWAReplyText11921 dir=ltr>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2>Interesting.&nbsp; So was it 
not using the fips index at all? Just curious can you try this 
query</FONT></DIV>
<DIV dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT size=2>SELECT p.*<BR>FROM (SELECT 
buffer(kc_block_groups.the_geom, 1320) as thebuffer FROM kc_block_groups AND 
kc_block_groups.fips = 530330001001)&nbsp;&nbsp;b INNER JOIN parcels_06&nbsp; 
p&nbsp;&nbsp;&nbsp;ON b.thebuffer &amp;&amp; 
centroid(p.the_geom)&nbsp;&nbsp;</FONT></DIV>
<DIV dir=ltr><FONT size=2>WHERE within(centroid(p.the_geom), 
b.thebuffer)<BR></DIV></FONT></DIV>
<DIV dir=ltr><BR>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> 
postgis-users-bounces@postgis.refractions.net on behalf of Josh 
Livni<BR><B>Sent:</B> Fri 3/9/2007 1:29 PM<BR><B>To:</B> PostGIS Users 
Discussion<BR><B>Subject:</B> Re: [postgis-users] Re: GiST index seems to be 
ignored?<BR></FONT><BR></DIV>
<DIV>
<P><FONT size=2>Regina,<BR><BR>Thanks a lot for your detailed response (and to 
Reid too).<BR><BR>First, the 90 was a typo in my message, sorry -- the query was 
always<BR>using just 1320 with it's slowness.<BR><BR>Also, I did have both a 
btree index on fips, and a GiST index 
on<BR>centroid(parcels_06.the_geom)<BR><BR>I tried your query, and it too, took 
a couple minutes.<BR><BR>Moving on to some good news for me (and my not having 
to use my hack<BR>workaround of temp tables with pre-calculated 
buffers):<BR><BR>Turns out, Mark Cave-Ayland was exceedingly helpful on IRC and 
managed<BR>to solve this little issue, where it looks like forcing the 
planner's<BR>hand was the key.<BR><BR>He narrowed it down, so we noticed 
that:<BR>SELECT p.*<BR>FROM kc_block_groups b, parcels_06 
p&nbsp;&nbsp;&nbsp;<BR>WHERE buffer(b.the_geom,1320) &amp;&amp; 
centroid(p.the_geom)&nbsp;&nbsp;<BR>and b.fips = 530330001001;<BR><BR>took &gt; 
110,000 ms to run.<BR><BR><BR>whereas:<BR>SELECT *<BR>FROM parcels_06 AS 
p<BR>WHERE&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; (SELECT buffer(the_geom, 
1320)&nbsp;&nbsp;&nbsp; AS 
the_geomx&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
FROM kc_block_groups&nbsp;&nbsp;&nbsp;&nbsp; WHERE fips = 
530330001001)&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; 
centroid(p.the_geom)<BR>AND within(centroid(p.the_geom),<BR>&nbsp;&nbsp;&nbsp; 
(SELECT buffer(the_geom, 1320)&nbsp; AS 
the_geomx<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
FROM kc_block_groups&nbsp; WHERE fips = 530330001001) );<BR><BR>took ~300ms to 
run.<BR><BR>Nice!<BR><BR>I put this back in my orig query, and it took about 
1500ms:<BR>SELECT<BR>&nbsp;&nbsp; count(j."lfc_code") as count,<BR>&nbsp;&nbsp; 
sum(J."gross_sqft") as Sum_Gross_Sqft,<BR>&nbsp;&nbsp; sum(j."units") as 
Sum_Units,<BR>&nbsp;&nbsp; j."lfc_code"<BR>&nbsp;&nbsp; FROM kc_block_groups b, 
parcels_06 p inner join lfc_comm j on<BR>p.pin_key = j.pin_key<BR>&nbsp;&nbsp; 
WHERE<BR>&nbsp;&nbsp; (SELECT buffer(the_geom, 1320)<BR>&nbsp;&nbsp; AS 
the_geomx<BR>&nbsp;&nbsp; FROM kc_block_groups<BR>&nbsp;&nbsp;&nbsp; WHERE fips 
= 530330001001)&nbsp;&nbsp;&nbsp; &amp;&amp; 
centroid(p.the_geom)<BR>&nbsp;&nbsp; and within(centroid(p.the_geom), (SELECT 
buffer(the_geom, 1320)<BR>&nbsp;&nbsp; AS the_geomx<BR>&nbsp;&nbsp; FROM 
kc_block_groups<BR>&nbsp;&nbsp;&nbsp; WHERE fips = 530330001001) 
)<BR>&nbsp;&nbsp; and b.fips = 530330001001<BR>&nbsp;&nbsp; GROUP BY 
j."lfc_code" ;<BR><BR>However, this compared to a ~350ms return time for the 
query on my temp<BR>table of pre-buffered polygons:<BR>SELECT<BR>&nbsp;&nbsp; 
count(j."lfc_code") as count,<BR>&nbsp;&nbsp; sum(J."gross_sqft") as 
Sum_Gross_Sqft,<BR>&nbsp;&nbsp; sum(j."units") as Sum_Units,<BR>&nbsp;&nbsp; 
j."lfc_code"<BR>&nbsp;&nbsp; FROM kc_bg_qm_buffer b, parcels_06 p inner join 
lfc_comm j on<BR>p.pin_key = j.pin_key<BR>&nbsp;&nbsp; WHERE b.the_geom 
&amp;&amp; centroid(p.the_geom)<BR>&nbsp;&nbsp; and within(centroid(p.the_geom), 
b.the_geom)<BR>&nbsp;&nbsp; and b.fips = 530330001001<BR>&nbsp;&nbsp; GROUP BY 
j."lfc_code" ;<BR><BR>As I think Mark noted, since my queries are pretty much 
spatial, further<BR>subselects to ensure the spatial stuff is done first might 
help.&nbsp; This<BR>is my next step, but meanwhile here is a really significant 
improvement<BR>- I hope the notes above might aid someone in a similar 
situation.&nbsp;&nbsp;<BR>Thanks Mark!<BR><BR>Cheers,<BR><BR><BR>&nbsp; 
-Josh<BR><BR>Obe, Regina wrote:<BR>&gt; Do you have a gist index on your 
p.the_geom.<BR>&gt;&nbsp;<BR>&gt; I think expand is much more efficient than 
buffer for your &amp;&amp; and I<BR>&gt; think your centroid &amp;&amp; instead 
of just using p.the_geom is totally<BR>&gt; wasteful.&nbsp; So I would change to 
do the falling<BR>&gt;&nbsp;<BR>&gt; 1) Add a btree index on fips on your block 
groups&nbsp; - a attribute index<BR>&gt; is almost always more selective than a 
spatial index.<BR>&gt; 2) I think you can drop that buffer index you have going, 
but if you<BR>&gt; do need to keep it change it to expand(b.the_geom, 90) - for 
this<BR>&gt; particular query, it probably won't be used anyway if you have 
an<BR>&gt; index on fips.<BR>&gt; 2) Make sure you have an index on 
p.the_geom<BR>&gt; 3) Change your query to<BR>&gt;&nbsp;<BR>&gt;&nbsp; 
SELECT<BR>&gt;&nbsp;&nbsp;&nbsp; count(j."lfc_code") as 
count,<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; sum(J."gross_sqft") as 
Sum_Gross_Sqft,<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; sum(j."units") as 
Sum_Units,<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; 
j."lfc_code"<BR>&gt;&nbsp;&nbsp;&nbsp; FROM kc_block_groups b, parcels_06 p 
inner join lfc_comm j on<BR>&gt;&nbsp; p.pin_key = 
j.pin_key<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; WHERE expand(b.the_geom,90) &amp;&amp; 
p.the_geom<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; and within(centroid(p.the_geom), 
buffer(b.the_geom,1320))<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; and b.fips = 
530330001001<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; GROUP BY 
j."lfc_code"<BR>&gt;<BR>&gt; 
------------------------------------------------------------------------<BR>&gt; 
*From:* postgis-users-bounces@postgis.refractions.net on behalf of<BR>&gt; Reid 
Priedhorsky<BR>&gt; *Sent:* Fri 3/9/2007 12:40 PM<BR>&gt; *To:* PostGIS Users 
Discussion<BR>&gt; *Subject:* [postgis-users] Re: GiST index seems to be 
ignored?<BR>&gt;<BR>&gt; Josh Livni wrote:<BR>&gt;<BR>&gt; &gt; I am basically 
trying to find points within a buffered polygon.&nbsp; I've<BR>&gt; &gt; created 
a GiST index on my polygons:<BR>&gt; &gt; CREATE INDEX 
kc_block_groups_qm_buffer_the_geom_gist<BR>&gt; &gt;&nbsp; ON 
kc_block_groups<BR>&gt; &gt;&nbsp; USING gist<BR>&gt; &gt;&nbsp; 
(buffer(the_geom, 1320::double precision));<BR>&gt;<BR>&gt; &gt; SELECT<BR>&gt; 
&gt;&nbsp;&nbsp;&nbsp; count(j."lfc_code") as count,<BR>&gt; 
&gt;&nbsp;&nbsp;&nbsp; sum(J."gross_sqft") as Sum_Gross_Sqft,<BR>&gt; 
&gt;&nbsp;&nbsp;&nbsp; sum(j."units") as Sum_Units,<BR>&gt; 
&gt;&nbsp;&nbsp;&nbsp; j."lfc_code"<BR>&gt; &gt;&nbsp;&nbsp;&nbsp; FROM 
kc_block_groups b, parcels_06 p inner join lfc_comm j on<BR>&gt; &gt; p.pin_key 
= j.pin_key<BR>&gt; &gt;&nbsp;&nbsp;&nbsp; WHERE buffer(b.the_geom,90) 
&amp;&amp; centroid(p.the_geom)<BR>&gt; &gt;&nbsp;&nbsp;&nbsp; and 
within(centroid(p.the_geom), buffer(b.the_geom,1320))<BR>&gt; 
&gt;&nbsp;&nbsp;&nbsp; and b.fips = 530330001001<BR>&gt; &gt;&nbsp;&nbsp;&nbsp; 
GROUP BY j."lfc_code"<BR>&gt;<BR>&gt; within() won't take advantage of the 
index, and with the operator that<BR>&gt; does (&amp;&amp;), your buffer() call 
uses a different buffer distance. What<BR>&gt; happens if you create the index 
on buffer(the_geom, 90)?<BR>&gt;<BR>&gt; My other guess is that you use a 
typecast in the index creation but not<BR>&gt; the query.<BR>&gt;<BR>&gt; Good 
luck,<BR>&gt;<BR>&gt; Reid<BR>&gt; 
_______________________________________________<BR>&gt; postgis-users mailing 
list<BR>&gt; postgis-users@postgis.refractions.net<BR>&gt; <A 
href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</A><BR>&gt;<BR>&gt; 
------------------------------------------------------------------------<BR>&gt;<BR>&gt; 
*<BR>&gt; The substance of this message, including any attachments, may 
be<BR>&gt; confidential, legally privileged and/or exempt from 
disclosure<BR>&gt; pursuant to Massachusetts law. It is intended solely for 
the<BR>&gt; addressee. If you received this in error, please contact the 
sender<BR>&gt; and delete the material from any computer.<BR>&gt; 
*<BR>&gt;<BR>&gt; 
------------------------------------------------------------------------<BR>&gt;<BR>&gt; 
_______________________________________________<BR>&gt; postgis-users mailing 
list<BR>&gt; postgis-users@postgis.refractions.net<BR>&gt; <A 
href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</A><BR>&gt;&nbsp;&nbsp;<BR><BR><BR>_______________________________________________<BR>postgis-users 
mailing list<BR>postgis-users@postgis.refractions.net<BR><A 
href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</A><BR></FONT></P></DIV>

</BODY>
</HTML>