<div dir="ltr">exists() is optimized in a manner similar to join, so there's usually no penalty using it, when it's contextually a right metaphor. </div><br><div class="gmail_quote"><div dir="ltr">ср, 31 янв. 2018 г. в 14:51, Oliver Morris <<a href="mailto:Oliver.Morris@halliburton.com">Oliver.Morris@halliburton.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Darafei,<br>
<br>
Thank you so much, I have tested and now using EXISTS it runs in less than a second.<br>
<br>
I had avoided using exists because of the recommendation on:<br>
<a href="https://www.techonthenet.com/postgresql/exists.php" rel="noreferrer" target="_blank">https://www.techonthenet.com/postgresql/exists.php</a><br>
'SQL statements that use the EXISTS condition in PostgreSQL are very inefficient since the sub-query is RE-RUN for EVERY row in the outer query's table. There are more efficient ways to write most queries, that do not use the EXISTS condition.'<br>
<br>
However, this seems to work appropriately for me.<br>
<br>
Thank you again,<br>
<br>
Oliver<br>
<br>
-----Original Message-----<br>
From: postgis-users [mailto:<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">postgis-users-bounces@lists.osgeo.org</a>] On Behalf Of <a href="mailto:postgis-users-request@lists.osgeo.org" target="_blank">postgis-users-request@lists.osgeo.org</a><br>
Sent: 29 January 2018 20:00<br>
To: <a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
Subject: [EXTERNAL] postgis-users Digest, Vol 191, Issue 7<br>
<br>
External Sender: Use caution with links/attachments.<br>
<br>
<br>
<br>
Send postgis-users mailing list submissions to<br>
        <a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
        <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.osgeo.org_mailman_listinfo_postgis-2Dusers&d=DwIGaQ&c=PskvixtEUDK7wuWU-tIg6oKuGYBRbrMXk2FZvF0UfTo&r=A8IiePeQXxNn87piHJsFIGj9NGqzzZxbQ6jIL0nH8HQ&m=BQ81R13zHy3RXBQgSEYB6ibDq8qY68opCsE6EvC6ctM&s=fdGcanVIfllgOEeIyzfZ5_ST_0uAH8vBN7t9IdR0NFE&e=" rel="noreferrer" target="_blank">https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.osgeo.org_mailman_listinfo_postgis-2Dusers&d=DwIGaQ&c=PskvixtEUDK7wuWU-tIg6oKuGYBRbrMXk2FZvF0UfTo&r=A8IiePeQXxNn87piHJsFIGj9NGqzzZxbQ6jIL0nH8HQ&m=BQ81R13zHy3RXBQgSEYB6ibDq8qY68opCsE6EvC6ctM&s=fdGcanVIfllgOEeIyzfZ5_ST_0uAH8vBN7t9IdR0NFE&e=</a><br>
or, via email, send a message with subject or body 'help' to<br>
        <a href="mailto:postgis-users-request@lists.osgeo.org" target="_blank">postgis-users-request@lists.osgeo.org</a><br>
<br>
You can reach the person managing the list at<br>
        <a href="mailto:postgis-users-owner@lists.osgeo.org" target="_blank">postgis-users-owner@lists.osgeo.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific than "Re: Contents of postgis-users digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
   1. Spatial Intersect if condition is met then go to  next step<br>
      (Oliver Morris)<br>
   2. Re: Spatial Intersect if condition is met then go to next<br>
      step (Darafei Komяpa Praliaskouski)<br>
<br>
<br>
----------------------------------------------------------------------<br>
<br>
Message: 1<br>
Date: Mon, 29 Jan 2018 10:28:57 +0000<br>
From: Oliver Morris <<a href="mailto:Oliver.Morris@halliburton.com" target="_blank">Oliver.Morris@halliburton.com</a>><br>
To: "<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a>" <<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a>><br>
Subject: [postgis-users] Spatial Intersect if condition is met then go<br>
        to      next step<br>
Message-ID:<br>
        <<a href="mailto:93FB8A2740EFB64D82CE13D909334CC0036C54DE@EP1EXMB001.corp.halliburton.com" target="_blank">93FB8A2740EFB64D82CE13D909334CC0036C54DE@EP1EXMB001.corp.halliburton.com</a>><br>
<br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Hi,<br>
First post so apologies in advance if this is the wrong place for this question - hopefully I found the right place.<br>
<br>
I have a bounding box geometry and I would like to find out if any rows in a selection of tables are intersected, each table contains a field (seq) which contains a property that I need to group the results by (for this demo lets day it is days of the week). I don't need to know how many rows but simply require a true/false result for each table and seq. Ideally the script as soon as it has found an intersection would move onto the next table and not spend any further time running intersections, I was hoping the limit 1 clause would do this. I was also hoping that the lateral join would help me to do the grouping but I think it still processes all the rows.<br>
<br>
Currently my script (for two example tables) looks like:<br>
<br>
CREATE OR REPLACE FUNCTION tst.f_tst_report( topleftx double precision, toplefty double precision, bottomrightx double precision, bottomrighty double precision, groupidarray text)<br>
<br>
RETURNS TABLE(seq character)<br>
LANGUAGE 'plpgsql'<br>
COST 100<br>
VOLATILE<br>
ROWS 1000<br>
<br>
AS $BODY$<br>
DECLARE<br>
envelope geometry;<br>
BEGIN<br>
envelope := ST_MakeEnvelope(topleftx,toplefty,bottomrightx,bottomrighty,4326);<br>
RETURN QUERY<br>
<br>
WITH<br>
cte_gde_polygons as (<br>
select distinct sq.seq as seq1 from tst.seq sq join lateral ( select ss.seq from present_mapnonclipped.gde_polygons ss where ss.seq = sq.seq and ST_Intersects(ss.geom,envelope)<br>
)<br>
limit 1<br>
) p on true),<br>
<br>
cte_gde_fault as (<br>
select distinct sq.seq as seq1 from tst.seq sq join lateral ( select ss.seq from present_mapnonclipped.gde_fault ss where ss.seq = sq.seq and ST_Intersects(ss.geom,envelope)<br>
)<br>
limit 1<br>
) p on true)<br>
<br>
select * from cte_gde_polygons<br>
union<br>
select * from cte_gde_fault<br>
<br>
END<br>
$BODY$;<br>
<br>
An example output across these tables for when Monday, Wednesday and Thursday intersects with the bounding box would return:<br>
<br>
Seq<br>
Monday<br>
Wednesday<br>
Thursday<br>
<br>
In short - if there is no way to stop intersecting after the first match my alternative is to dissolve all the features by seq in each table. The only issue would be that this would need updating on any data change.<br>
<br>
Many Thanks for your help in advance,<br>
<br>
Oliver<br>
<br>
<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.osgeo.org_pipermail_postgis-2Dusers_attachments_20180129_e90e90d6_attachment-2D0001.html&d=DwIGaQ&c=PskvixtEUDK7wuWU-tIg6oKuGYBRbrMXk2FZvF0UfTo&r=A8IiePeQXxNn87piHJsFIGj9NGqzzZxbQ6jIL0nH8HQ&m=BQ81R13zHy3RXBQgSEYB6ibDq8qY68opCsE6EvC6ctM&s=ymaSU574LN_ghVEbW__bogW3SV96NQ45RCQTK3mlBWs&e=" rel="noreferrer" target="_blank">https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.osgeo.org_pipermail_postgis-2Dusers_attachments_20180129_e90e90d6_attachment-2D0001.html&d=DwIGaQ&c=PskvixtEUDK7wuWU-tIg6oKuGYBRbrMXk2FZvF0UfTo&r=A8IiePeQXxNn87piHJsFIGj9NGqzzZxbQ6jIL0nH8HQ&m=BQ81R13zHy3RXBQgSEYB6ibDq8qY68opCsE6EvC6ctM&s=ymaSU574LN_ghVEbW__bogW3SV96NQ45RCQTK3mlBWs&e=</a>><br>
<br>
------------------------------<br>
<br>
Message: 2<br>
Date: Mon, 29 Jan 2018 13:14:35 +0000<br>
From: Darafei "Komяpa" Praliaskouski <<a href="mailto:me@komzpa.net" target="_blank">me@komzpa.net</a>><br>
To: PostGIS Users Discussion <<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a>><br>
Subject: Re: [postgis-users] Spatial Intersect if condition is met<br>
        then go to next step<br>
Message-ID:<br>
        <CAC8Q8tKNDhOegFw=<a href="mailto:UBCQtG0iU2%2BKGgkQkiwbXDzjS3aane2u0w@mail.gmail.com" target="_blank">UBCQtG0iU2+KGgkQkiwbXDzjS3aane2u0w@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
hi,<br>
<br>
you may employ `where exists()`:<br>
<br>
select seq from tst sq<br>
 WHERE EXISTS(select from present_mapnonclipped.gde_polygons ss where ss.seq = sq.seq and ST_Intersects(ss.geom,envelope)) or exists(present_mapnonclipped.gde_polygons ss where ss.seq = sq.seq and ST_Intersects(ss.geom,envelope))<br>
<br>
You may also want to debug your query with EXPLAIN (ANALYZE, VERBOSE,<br>
BUFFERS) before putting it into stored procedure.<br>
<br>
пн, 29 янв. 2018 г. в 14:04, Oliver Morris <<a href="mailto:Oliver.Morris@halliburton.com" target="_blank">Oliver.Morris@halliburton.com</a>>:<br>
<br>
> Hi,<br>
><br>
> First post so apologies in advance if this is the wrong place for this<br>
> question – hopefully I found the right place.<br>
><br>
><br>
><br>
> I have a bounding box geometry and I would like to find out if any<br>
> rows in a selection of tables are intersected, each table contains a<br>
> field (seq) which contains a property that I need to group the results<br>
> by (for this demo lets day it is days of the week). I don’t need to<br>
> know how many rows but simply require a true/false result for each<br>
> table and seq. Ideally the script as soon as it has found an<br>
> intersection would move onto the next table and not spend any further<br>
> time running intersections, I was hoping the limit 1 clause would do<br>
> this. I was also hoping that the lateral join would help me to do the<br>
> grouping but I think it still processes all the rows.<br>
><br>
><br>
><br>
> Currently my script (for two example tables) looks like:<br>
><br>
><br>
><br>
> CREATE OR REPLACE FUNCTION tst.f_tst_report(<br>
><br>
> topleftx double precision,<br>
><br>
> toplefty double precision,<br>
><br>
> bottomrightx double precision,<br>
><br>
> bottomrighty double precision,<br>
><br>
> groupidarray text)<br>
><br>
><br>
><br>
> RETURNS TABLE(seq character)<br>
><br>
> LANGUAGE 'plpgsql'<br>
><br>
> COST 100<br>
><br>
> VOLATILE<br>
><br>
> ROWS 1000<br>
><br>
><br>
><br>
> AS $BODY$<br>
><br>
> DECLARE<br>
><br>
> envelope geometry;<br>
><br>
> BEGIN<br>
><br>
> envelope :=<br>
> ST_MakeEnvelope(topleftx,toplefty,bottomrightx,bottomrighty,4326);<br>
><br>
> RETURN QUERY<br>
><br>
><br>
><br>
> WITH<br>
><br>
> cte_gde_polygons as (<br>
><br>
> select distinct sq.seq as seq1 from tst.seq sq<br>
><br>
> join lateral (<br>
><br>
> select ss.seq from present_mapnonclipped.gde_polygons ss<br>
><br>
> where ss.seq = sq.seq and ST_Intersects(ss.geom,envelope)<br>
><br>
> )<br>
><br>
> limit 1<br>
><br>
> ) p on true),<br>
><br>
><br>
><br>
> cte_gde_fault as (<br>
><br>
> select distinct sq.seq as seq1 from tst.seq sq<br>
><br>
> join lateral (<br>
><br>
> select ss.seq from present_mapnonclipped.gde_fault ss<br>
><br>
> where ss.seq = sq.seq and ST_Intersects(ss.geom,envelope)<br>
><br>
> )<br>
><br>
> limit 1<br>
><br>
> ) p on true)<br>
><br>
><br>
><br>
> select * from cte_gde_polygons<br>
><br>
> union<br>
><br>
> select * from cte_gde_fault<br>
><br>
><br>
><br>
> END<br>
><br>
> $BODY$;<br>
><br>
><br>
><br>
> An example output across these tables for when Monday, Wednesday and<br>
> Thursday intersects with the bounding box would return:<br>
><br>
><br>
><br>
> Seq<br>
><br>
> Monday<br>
><br>
> Wednesday<br>
><br>
> Thursday<br>
><br>
><br>
><br>
> In short - if there is no way to stop intersecting after the first<br>
> match my alternative is to dissolve all the features by seq in each<br>
> table. The only issue would be that this would need updating on any data change.<br>
><br>
><br>
><br>
> Many Thanks for your help in advance,<br>
><br>
><br>
><br>
> Oliver<br>
><br>
><br>
><br>
><br>
> _______________________________________________<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="https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.osgeo.org_m" rel="noreferrer" target="_blank">https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.osgeo.org_m</a><br>
> ailman_listinfo_postgis-2Dusers&d=DwIGaQ&c=PskvixtEUDK7wuWU-tIg6oKuGYB<br>
> RbrMXk2FZvF0UfTo&r=A8IiePeQXxNn87piHJsFIGj9NGqzzZxbQ6jIL0nH8HQ&m=BQ81R<br>
> 13zHy3RXBQgSEYB6ibDq8qY68opCsE6EvC6ctM&s=fdGcanVIfllgOEeIyzfZ5_ST_0uAH<br>
> 8vBN7t9IdR0NFE&e=<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.osgeo.org_pipermail_postgis-2Dusers_attachments_20180129_31e89949_attachment-2D0001.html&d=DwIGaQ&c=PskvixtEUDK7wuWU-tIg6oKuGYBRbrMXk2FZvF0UfTo&r=A8IiePeQXxNn87piHJsFIGj9NGqzzZxbQ6jIL0nH8HQ&m=BQ81R13zHy3RXBQgSEYB6ibDq8qY68opCsE6EvC6ctM&s=_I0bEfa8-BvZCkQEBafghfsqbu7ffyi3RfuXRTQ6uXk&e=" rel="noreferrer" target="_blank">https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.osgeo.org_pipermail_postgis-2Dusers_attachments_20180129_31e89949_attachment-2D0001.html&d=DwIGaQ&c=PskvixtEUDK7wuWU-tIg6oKuGYBRbrMXk2FZvF0UfTo&r=A8IiePeQXxNn87piHJsFIGj9NGqzzZxbQ6jIL0nH8HQ&m=BQ81R13zHy3RXBQgSEYB6ibDq8qY68opCsE6EvC6ctM&s=_I0bEfa8-BvZCkQEBafghfsqbu7ffyi3RfuXRTQ6uXk&e=</a>><br>
<br>
------------------------------<br>
<br>
Subject: Digest Footer<br>
<br>
_______________________________________________<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="https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.osgeo.org_mailman_listinfo_postgis-2Dusers&d=DwIGaQ&c=PskvixtEUDK7wuWU-tIg6oKuGYBRbrMXk2FZvF0UfTo&r=A8IiePeQXxNn87piHJsFIGj9NGqzzZxbQ6jIL0nH8HQ&m=BQ81R13zHy3RXBQgSEYB6ibDq8qY68opCsE6EvC6ctM&s=fdGcanVIfllgOEeIyzfZ5_ST_0uAH8vBN7t9IdR0NFE&e=" rel="noreferrer" target="_blank">https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.osgeo.org_mailman_listinfo_postgis-2Dusers&d=DwIGaQ&c=PskvixtEUDK7wuWU-tIg6oKuGYBRbrMXk2FZvF0UfTo&r=A8IiePeQXxNn87piHJsFIGj9NGqzzZxbQ6jIL0nH8HQ&m=BQ81R13zHy3RXBQgSEYB6ibDq8qY68opCsE6EvC6ctM&s=fdGcanVIfllgOEeIyzfZ5_ST_0uAH8vBN7t9IdR0NFE&e=</a><br>
<br>
------------------------------<br>
<br>
End of postgis-users Digest, Vol 191, Issue 7<br>
*********************************************<br>
_______________________________________________<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="https://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a></blockquote></div>