Skip to content

Instantly share code, notes, and snippets.

@pnorman
Last active December 19, 2015 05:49
Show Gist options
  • Save pnorman/5907352 to your computer and use it in GitHub Desktop.
Save pnorman/5907352 to your computer and use it in GitHub Desktop.
Two options for backfilling nodes from ways in cgimap.
-- Both queries produce the same results.
-- tmp_nodes and tmp_ways populated by geometries intersecting ST_SetSRID(ST_MakeBox2D(ST_Point(-123.4387,49.3308),ST_Point(-123.3034,49.4248)),4326)
-- ~5k nodes, ~400 ways, ~600 backfilled nodes
-- 150k set from ST_SetSRID(ST_MakeBox2D(ST_Point(-123.2748,49.1929),ST_Point(-122.9850,49.3058)),4326)
-- subselect variant
EXPLAIN ANALYZE INSERT INTO tmp_nodes
SELECT n.id,n.version,n.user_id,n.tstamp,n.changeset_id,n.tags,
ST_X(n.geom) * 10000000, ST_Y(n.geom) * 10000000
FROM (SELECT DISTINCT unnest(nodes) AS node_id FROM tmp_ways) AS wn
JOIN nodes n ON (wn.node_id = n.id)
WHERE node_id NOT IN (SELECT id FROM tmp_nodes);
-- Resulting 5k query plan is http://explain.depesz.com/s/wvTz
-- Resulting 150k query plan is http://explain.depesz.com/s/BIq
-- left join variant
EXPLAIN ANALYZE INSERT INTO tmp_nodes
SELECT n.id,n.version,n.user_id,n.tstamp,n.changeset_id,n.tags,
ST_X(n.geom) * 10000000, ST_Y(n.geom) * 10000000
FROM (SELECT DISTINCT unnest(nodes) AS node_id FROM tmp_ways) AS wn
LEFT JOIN tmp_nodes t ON (wn.node_id = t.id)
JOIN nodes n ON (wn.node_id = n.id)
WHERE t.id IS NULL;
-- Resulting 5k query plan is http://explain.depesz.com/s/W9eo
-- Resulting 150k query plan is http://explain.depesz.com/s/zK8
-- left join with CTE forcing seq scan
EXPLAIN ANALYZE WITH t AS (SELECT * FROM tmp_nodes)
INSERT INTO tmp_nodes
SELECT n.id,n.version,n.user_id,n.tstamp,n.changeset_id,n.tags,
ST_X(n.geom) * 10000000, ST_Y(n.geom) * 10000000
FROM (SELECT DISTINCT unnest(nodes) AS node_id FROM tmp_ways) AS wn
LEFT JOIN t ON (wn.node_id = t.id)
JOIN nodes n ON (wn.node_id = n.id)
WHERE t.id IS NULL;
-- Resulting 5k query plan is http://explain.depesz.com/s/XFJ
-- Resulting 150k query plan is http://explain.depesz.com/s/4jC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment