<div dir="ltr">On a more high level: <div>I'm guessing you are using the recursive cte to walk trough a graph.</div><div>There are ways to accelerate this, but at the cost of increase of memory usage and duplication of data.</div>
<div>It depends a lot on you graph properties (has it a lots of nodes, connectivity, centralness, etc)</div><div><br></div><div>(of course you have an index on "gid" or it is a primary key (implicit index) ).<br>
<br>If perf is really an issue, you could turn to more specialize grpah db (look for spar-ql)</div><div><br></div><div>Cheers,</div><div>Rémi-C</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/12/10 tommaso <span dir="ltr"><<a href="mailto:tommasodb@googlemail.com" target="_blank">tommasodb@googlemail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
Why do you do ST_AsText(e.geom)::geometry? This is equivalent to
e.geom. Change the query so:<br>
SELECT e.gid, e.geom FROM electric_line e, fuses f WHERE
ST_DWithin(e.geom, f.geom 0.01) AND f.gid=$1 .....<br>
<br>
No sure if it does help, but you you can also try so:<br>
SELECT e.gid, e.geom FROM electric_line e JOIN fuses f ON
ST_DWithin(e.geom, f.geom, 0.01) WHERE f.gid=$1<br>
<br>
Also<br>
WHERE ST_DWithin(ST_EndPoint(f.geom),ST_StartPoint(n.geom),0.01)<br>
can be changed to:<br>
WHERE ST_Distance(ST_EndPoint(f.geom),ST_StartPoint(n.geom)) <=
0.01<br>
which should be faster.<br>
<br>
Obviously you need a GIST index on booth geometry columns.<br>
<br>
If it all does not help please attach some test data.<br>
<br>
Cheers, Tommaso<div><div class="h5"><br>
<br>
<br>
<br>
<div>On 12/10/2013 03:01 AM, Jonatan Malaver
wrote:<br>
</div>
<blockquote type="cite">
<div>
<p class="MsoNormal"><span style="color:#1f497d">Another
question, what can I do to improve performance on the query?
The query can take 2 seconds or more.</span><span style="color:#1f497d"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d"><u></u> <u></u></span></p>
<div>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext">
<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">postgis-users-bounces@lists.osgeo.org</a>
[<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">mailto:postgis-users-bounces@lists.osgeo.org</a>]
<b>On Behalf Of </b>Jonatan Malaver<br>
<b>Sent:</b> Monday, December 09, 2013 8:52 PM<br>
<b>To:</b> PostGIS Users Discussion<br>
<b>Subject:</b> Re: [postgis-users] functions<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="color:#1f497d">Thank you!!!<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:#1f497d"><u></u> <u></u></span></p>
<div>
<p class="MsoNormal"><span style="color:#1f497d">Jon<u></u><u></u></span></p>
</div>
<p class="MsoNormal"><span style="color:#1f497d"><u></u> <u></u></span></p>
<div>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:windowtext">
<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">postgis-users-bounces@lists.osgeo.org</a>
[<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">mailto:postgis-users-bounces@lists.osgeo.org</a>]
<b>On Behalf Of </b>tommaso<br>
<b>Sent:</b> Monday, December 09, 2013 12:57 PM<br>
<b>To:</b> PostGIS Users Discussion<br>
<b>Subject:</b> Re: [postgis-users] functions<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">This should work:<br>
<br>
create or replace function flow(integer) RETURNS TABLE (gid
int, geom geometry, geojson text) AS<br>
$$ <br>
<br>
WITH RECURSIVE flow(gid, geom) AS (<br>
<br>
SELECT e.gid, e.geom FROM electric_line e, fuses f WHERE
ST_DWithin(ST_AsText(e.geom)::geometry,
ST_AsText(f.geom)::geometry, 0.01) AND f.gid=$1<br>
<br>
UNION ALL<br>
<br>
SELECT n.gid, n.geom<br>
<br>
FROM electric_line n, flow f<br>
<br>
WHERE
ST_DWithin(ST_EndPoint(f.geom),ST_StartPoint(n.geom),0.01)<br>
<br>
)<br>
<br>
SELECT *, ST_ASGeoJSON(ST_Transform(geom,4326),6) AS geojson<br>
<br>
FROM flow;<br>
<br>
$$<br>
LANGUAGE SQL;<br>
<br>
<br>
SELECT * FROM flow(1);<br>
<br>
<br>
On 12/09/2013 03:57 PM, Jonatan Malaver wrote:<u></u><u></u></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal">Hello,<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> I’m new to postgis/postgresql. I was
wondering if anyone could tell me how to make a function out
of this sql query:<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">WITH RECURSIVE flow(gid, geom) AS (<u></u><u></u></p>
<p class="MsoNormal"> SELECT e.gid, e.geom FROM
electric_line e, fuses f WHERE
ST_DWithin(ST_AsText(e.geom)::geometry,
ST_AsText(f.geom)::geometry, 0.01) AND f.gid=' .
$_GET['fuse'] . '<u></u><u></u></p>
<p class="MsoNormal"> UNION ALL<u></u><u></u></p>
<p class="MsoNormal"> SELECT n.gid, n.geom<u></u><u></u></p>
<p class="MsoNormal"> FROM electric_line n, flow f<u></u><u></u></p>
<p class="MsoNormal"> WHERE
ST_DWithin(ST_EndPoint(f.geom),ST_StartPoint(n.geom),0.01)<u></u><u></u></p>
<p class="MsoNormal"> )<u></u><u></u></p>
<p class="MsoNormal">SELECT *,
ST_ASGeoJSON(ST_Transform(geom,4326),6) AS geojson<u></u><u></u></p>
<p class="MsoNormal">FROM flow;<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> I’d like to make a function flow(gid)<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Jon<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span><br>
<br>
<u></u><u></u></span></p>
<pre>_______________________________________________<u></u><u></u></pre>
<pre>postgis-users mailing list<u></u><u></u></pre>
<pre><a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><u></u><u></u></pre>
<pre><a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a><u></u><u></u></pre>
</blockquote>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
postgis-users mailing list
<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a>
<a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a></pre>
</blockquote>
<br>
</div></div></div>
<br>_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org">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-bin/mailman/listinfo/postgis-users</a><br></blockquote></div><br></div>