Skip to content

Commit ba75d71

Browse files
committed
Fix #3010: Optimize pgr_separateTouching performance
1 parent 4912a28 commit ba75d71

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

docqueries/utilities/separateTouching.result

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,28 @@ FROM pgr_separateTouching('SELECT id, geom FROM edges', dryrun => true);
2222
NOTICE:
2323
WITH
2424
edges_table AS (
25-
SELECT id, geom FROM edges
25+
SELECT id, geom,
26+
ST_StartPoint(geom) AS startpoint,
27+
ST_EndPoint(geom) AS endpoint
28+
FROM (SELECT id, geom FROM edges) _edges
2629
),
2730

2831
get_touching AS (
29-
SELECT e1.id id1, e2.id id2, ST_Snap(e1.geom, e2.geom, 0.01) AS geom, e1.geom AS g1, e2.geom AS g2
32+
SELECT e1.id id1, e2.id id2, ST_Snap(e1.geom, e2.geom, 0.01) AS geom, e1.geom AS g1, e2.geom AS g2,
33+
e1.startpoint AS sp1, e1.endpoint AS ep1
3034
FROM edges_table e1, edges_table e2
3135
WHERE e1.id != e2.id AND ST_DWithin(e1.geom, e2.geom, 0.01) AND NOT(
32-
ST_StartPoint(e1.geom) = ST_StartPoint(e2.geom) OR ST_StartPoint(e1.geom) = ST_EndPoint(e2.geom)
33-
OR ST_EndPoint(e1.geom) = ST_StartPoint(e2.geom) OR ST_EndPoint(e1.geom) = ST_EndPoint(e2.geom))
36+
e1.startpoint = e2.startpoint OR e1.startpoint = e2.endpoint
37+
OR e1.endpoint = e2.startpoint OR e1.endpoint = e2.endpoint)
3438
),
3539

3640
touchings AS (
3741
SELECT id1, g1, g2, st_intersection(geom, g2) AS point
3842
FROM get_touching
3943
WHERE NOT (geom = g1) OR
4044
(ST_touches(g1, g2) AND NOT
41-
(ST_Intersection(geom, g2) = ST_StartPoint(g1)
42-
OR ST_Intersection(geom, g2) = ST_EndPoint(g1)))
45+
(ST_Intersection(geom, g2) = sp1
46+
OR ST_Intersection(geom, g2) = ep1))
4347
),
4448

4549
blades AS (

sql/utilities/separateTouching.sql

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,28 @@ BEGIN
5959
the_query := format($$
6060
WITH
6161
edges_table AS (
62-
%s
62+
SELECT id, geom,
63+
ST_StartPoint(geom) AS startpoint,
64+
ST_EndPoint(geom) AS endpoint
65+
FROM (%s) _edges
6366
),
6467

6568
get_touching AS (
66-
SELECT e1.id id1, e2.id id2, ST_Snap(e1.geom, e2.geom, %2$s) AS geom, e1.geom AS g1, e2.geom AS g2
69+
SELECT e1.id id1, e2.id id2, ST_Snap(e1.geom, e2.geom, %2$s) AS geom, e1.geom AS g1, e2.geom AS g2,
70+
e1.startpoint AS sp1, e1.endpoint AS ep1
6771
FROM edges_table e1, edges_table e2
6872
WHERE e1.id != e2.id AND ST_DWithin(e1.geom, e2.geom, %2$s) AND NOT(
69-
ST_StartPoint(e1.geom) = ST_StartPoint(e2.geom) OR ST_StartPoint(e1.geom) = ST_EndPoint(e2.geom)
70-
OR ST_EndPoint(e1.geom) = ST_StartPoint(e2.geom) OR ST_EndPoint(e1.geom) = ST_EndPoint(e2.geom))
73+
e1.startpoint = e2.startpoint OR e1.startpoint = e2.endpoint
74+
OR e1.endpoint = e2.startpoint OR e1.endpoint = e2.endpoint)
7175
),
7276

7377
touchings AS (
7478
SELECT id1, g1, g2, st_intersection(geom, g2) AS point
7579
FROM get_touching
7680
WHERE NOT (geom = g1) OR
7781
(ST_touches(g1, g2) AND NOT
78-
(ST_Intersection(geom, g2) = ST_StartPoint(g1)
79-
OR ST_Intersection(geom, g2) = ST_EndPoint(g1)))
82+
(ST_Intersection(geom, g2) = sp1
83+
OR ST_Intersection(geom, g2) = ep1))
8084
),
8185

8286
blades AS (

0 commit comments

Comments
 (0)