<div dir="ltr">Thanks Paul. <br><div><br></div><div>What if I want to say ST_Contains(A, B)? And will it matter if I say ST_Within(B, A) instead? I have a feeling it will, since </div><div>most likely the query planner will decide to process A row by row and find a "suitable B" each time. In this case, we need a GiST index on B, so that we could find the B that is contained in A right?</div><div><br></div><div>Thanks again,</div><div>Igor</div></div><br><div class="gmail_quote">On Tue, Mar 3, 2015 at 5:47 PM Paul Ramsey <<a href="mailto:pramsey@cleverelephant.ca">pramsey@cleverelephant.ca</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You want to express this as a JOIN,<br>
<br>
SELECT A.id, B.id<br>
FROM A JOIN B<br>
ON (<br>
ST_Intersects(A.shape, B.shape) AND<br>
ST_Length(ST_Intersection(A.<u></u>shape, B.shape)) / ST_Length(A.shape) >= 0.5<br>
)<br>
WHERE B.kind != 1<br>
<br>
As a general "join rule" I put any clause where both tables appear<br>
into the JOIN clause and any clause where only one table appears into<br>
the WHERE clause. The planner ends up rearranging things anyway it<br>
likes, but I find my rule brings some sense of order into my head.<br>
<br>
Order of operations is generally evaluated left-to-right, but the<br>
planner will rearrange things, particularly things with operators<br>
(like the && that hides inside ST_Intersects), so that the most<br>
selective clause evaluates first. In this case, since the<br>
ST_Intersects is hiding an && operator it doesn't matter where you put<br>
it, it will be promoted to wherever it is most effective. The only<br>
time the left-to-right thing is a place your fiddling will matter is<br>
when the functions are all scored as the same cost, and the planner<br>
has no idea what the most effective clause will be; in that case it'll<br>
apply them left to right.<br>
<br>
P.<br>
<br>
<br>
On Tue, Mar 3, 2015 at 2:14 AM, Igor Stassiy <<a href="mailto:istassiy@gmail.com" target="_blank">istassiy@gmail.com</a>> wrote:<br>
> Hello,<br>
><br>
> Lets say I have two tables<br>
><br>
> CREATE EXTENSION postgis;<br>
> DROP TABLE A;<br>
> DROP TABLE B;<br>
> CREATE TABLE A(shape Geometry, id INT);<br>
> CREATE TABLE B(shape Geometry, id INT, kind INT);<br>
> CREATE INDEX ON A USING GIST (shape);<br>
><br>
> I would like to select for each object A, the an object of B of a particular<br>
> kind, such<br>
> that the relative overlap between A and B is at least 0.5:<br>
><br>
> SELECT A.id, B.id FROM A, B<br>
> WHERE B.id = (SELECT B.id FROM B WHERE<br>
> ST_Intersects(A.shape, B.shape)<br>
> AND ST_Length(ST_Intersection(A.<u></u>shape, B.shape)) / ST_Length(A.shape) >=<br>
> 0.5<br>
> AND B.kind != 1 LIMIT 1)<br>
><br>
> The number of rows in table A orders of magnitude larger than that of table<br>
> B.<br>
><br>
> What are the indexes that should give optimal performance and what should be<br>
> the order of arguments<br>
> in the ST_Intersection? From the following page it seems that the order or<br>
> arguments can have dramatic effects:<br>
> <a href="http://support.esri.com/EN/knowledgebase/techarticles/detail/35498" target="_blank">http://support.esri.com/EN/<u></u>knowledgebase/techarticles/<u></u>detail/35498</a><br>
><br>
> Thanks,<br>
> Igor<br>
><br>
> ______________________________<u></u>_________________<br>
> postgis-users mailing list<br>
> <a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
> <a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-<u></u>bin/mailman/listinfo/postgis-<u></u>users</a><br>
______________________________<u></u>_________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-<u></u>bin/mailman/listinfo/postgis-<u></u>users</a><br>
</blockquote></div>