The pgRouting
pgr_drivingDistance
function returns only ids of road network vertices, but without ids of road edges in specific drive time.
To find this road edges I created the query:
SELECT all_roads.id, the_geom
FROM
(
SELECT e.id, e.the_geom
FROM tmp_ddist dd,
tmp_edge e
WHERE
e.target = dd.vertex
UNION
SELECT e.id, e.the_geom
FROM tmp_ddist dd,
tmp_edge e
WHERE
e.source = dd.vertex
) all_roads
GROUP BY all_roads.id, the_geom
HAVING COUNT(all_roads.id) = 2
Basically it finds road edges which have source and target vertices in set generated by pgr_drivingDistance
function and returns only those edges which have both source and target in this set.
Is there any way to optimize this query?
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
CREATE INDEX tmp_ddist_idx_vertex ON "tmp_ddist" ("vertex");
CREATE INDEX tmp_edge_idx_target ON "tmp_edge" ("target");
CREATE INDEX tmp_edge_idx_source ON "tmp_edge" ("source");
SELECT
all_roads.id,
all_roads.the_geom
FROM
(SELECT
e.id,
e.the_geom
FROM
tmp_ddist dd,
tmp_edge e
WHERE
e.target = dd.vertex
HAVING
(
COUNT(e.id) = 2
)
UNION
SELECT
e.id,
e.the_geom
FROM
tmp_ddist dd,
tmp_edge e
WHERE
e.source = dd.vertex
HAVING
(
COUNT(e.id) = 2
)
) all_roads
GROUP BY
all_roads.id,
all_roads.the_geom
HAVING
COUNT(all_roads.id) = 2