In postgis, is the ST_GeomFromText call very expensive? I ask mostly because I have a frequently called query that attempts to find the point that is nearest another point that matches some criteria, and which is also within a certain distance of that other point, and the way I currently wrote it, it’s doing the same ST_GeomFromText twice:
$findNearIDMatchStmt = $postconn->prepare( 'SELECT internalid ' . 'FROM waypoint ' . 'WHERE id = ? AND ' . ' category = ? AND '. ' (b.category in (1, 3) OR type like ?) AND '. ' ST_DWithin(point, ST_GeomFromText(?,' . SRID . ' ),'. SMALL_EPSILON . ') ' . ' ORDER BY ST_Distance(point, ST_GeomFromText(?,', SRID . ' )) ' . ' LIMIT 1');
Is there a better way to re-write this?
Slightly OT: In the preview screen, all my underscores are being rendered as & # 9 5 ; – I hope that’s not going to show up that way in the post.
I don’t believe
ST_GeomFromText()is particularly expensive, although in the past I’ve optimizedPostGISqueries by creating a function, declaring a variable and then assigning the result ofST_GeomFromTextto the variable.Have you tried checking the execution plan for you query with a variety of different parameters because that should give you a definite idea of which bits of the query are taking the time?
I’m guessing most of the execution time will be in the calls to
ST_DWithin()andST_Distance(), although if the id and category columns aren’t indexed then it might be doing some interesting table scanning.