I'm using pgrouting shortest_path_astar with a complicated cost function. It gives back the edges, vertices and costs. I would like to get the total distance of this shortest path (i.e., the sum of lengths of edges, not the sum of costs of edges).

For now, I use:

SELECT SUM(t2.length) FROM (
    SELECT edge_id FROM shortest_path_astar(..., TRUE, TRUE)
) as t1,
routing.edges as t2 where t1.edge_id = t2.edge_id

Is it the most efficient way to get the distance? I need to run this query many times and I want to be sure to do it in the fastest way.

  • You could try if a LEFT JOIN is faster, but I think the PostGIS query analyzer should do a good job and figure it out anyway.
    – underdark
    Aug 11, 2012 at 9:52
  • Don't use such an ancient version of pgRouting, especially if you want to run this many times. In that case you can use 1:n or even n:n shortest path queries: docs.pgrouting.org/latest/en/pgr_dijkstra.html#pgr-dijkstra
    – dkastl
    Jul 15, 2018 at 10:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.