<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body 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<br>
<br>
<br>
<br>
<div class="moz-cite-prefix">On 12/10/2013 03:01 AM, Jonatan Malaver
wrote:<br>
</div>
<blockquote
cite="mid:58E13A9FA1D4994EB29384A8025A97B216BE86C8@mbx027-e1-nj-6.exch027.domain.local"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta name="Generator" content="Microsoft Word 14 (filtered
medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
color:black;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
pre
{mso-style-priority:99;
mso-style-link:"HTML Preformatted Char";
margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";
color:black;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
{mso-style-priority:99;
mso-style-link:"Balloon Text Char";
margin:0in;
margin-bottom:.0001pt;
font-size:8.0pt;
font-family:"Tahoma","sans-serif";
color:black;}
span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:Consolas;
color:black;}
span.EmailStyle19
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:windowtext;}
span.EmailStyle20
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:#1F497D;}
span.EmailStyle21
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
span.BalloonTextChar
{mso-style-name:"Balloon Text Char";
mso-style-priority:99;
mso-style-link:"Balloon Text";
font-family:"Tahoma","sans-serif";
color:black;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<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"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></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 class="moz-txt-link-abbreviated" href="mailto:postgis-users-bounces@lists.osgeo.org">postgis-users-bounces@lists.osgeo.org</a>
[<a class="moz-txt-link-freetext" href="mailto:postgis-users-bounces@lists.osgeo.org">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<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="color:#1F497D">Thank you!!!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal"><span style="color:#1F497D">Jon<o:p></o:p></span></p>
</div>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></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 class="moz-txt-link-abbreviated" href="mailto:postgis-users-bounces@lists.osgeo.org">postgis-users-bounces@lists.osgeo.org</a>
[<a class="moz-txt-link-freetext" href="mailto:postgis-users-bounces@lists.osgeo.org">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<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></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:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal">Hello,<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></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:<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">WITH RECURSIVE flow(gid, geom) AS (<o:p></o:p></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'] . '<o:p></o:p></p>
<p class="MsoNormal"> UNION ALL<o:p></o:p></p>
<p class="MsoNormal"> SELECT n.gid, n.geom<o:p></o:p></p>
<p class="MsoNormal"> FROM electric_line n, flow f<o:p></o:p></p>
<p class="MsoNormal"> WHERE
ST_DWithin(ST_EndPoint(f.geom),ST_StartPoint(n.geom),0.01)<o:p></o:p></p>
<p class="MsoNormal"> )<o:p></o:p></p>
<p class="MsoNormal">SELECT *,
ST_ASGeoJSON(ST_Transform(geom,4326),6) AS geojson<o:p></o:p></p>
<p class="MsoNormal">FROM flow;<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal"> I’d like to make a function flow(gid)<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">Jon<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span
style="font-size:12.0pt;font-family:"Times New
Roman","serif""><br>
<br>
<o:p></o:p></span></p>
<pre>_______________________________________________<o:p></o:p></pre>
<pre>postgis-users mailing list<o:p></o:p></pre>
<pre><a moz-do-not-send="true" href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><o:p></o:p></pre>
<pre><a moz-do-not-send="true" href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a><o:p></o:p></pre>
</blockquote>
<p class="MsoNormal"><span
style="font-size:12.0pt;font-family:"Times New
Roman","serif""><o:p> </o:p></span></p>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
postgis-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a></pre>
</blockquote>
<br>
</body>
</html>