Skip to content

Commit 29ff7ee

Browse files
committed
Move DISTINCT from queries generating list to when we read the data
We had some problems with the query planner getting confused with those queries. Lets make them as simple as possible to not confuse it unnecessarily. This might be vodoo, but it doesn't cost much, just a little bit of temporary space in the database, because when reading we do the ORDER BY anyway, so the extra DISTINCT isn't more cost.
1 parent bdd6444 commit 29ff7ee

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/middle-pgsql.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ static void load_id_list(pg_conn_t const &db_connection,
7373
std::string const &table,
7474
osmium::index::IdSetSmall<osmid_t> *ids)
7575
{
76-
auto const res =
77-
db_connection.exec(fmt::format("SELECT id FROM {} ORDER BY id", table));
76+
auto const res = db_connection.exec(
77+
fmt::format("SELECT DISTINCT id FROM {} ORDER BY id", table));
7878
for (int n = 0; n < res.num_tuples(); ++n) {
7979
ids->set(osmium::string_to_object_id(res.get_value(n, 0)));
8080
}
@@ -836,7 +836,7 @@ WITH changed_buckets AS (
836836
FROM osm2pgsql_changed_nodes GROUP BY id >> {way_node_index_id_shift}
837837
)
838838
INSERT INTO osm2pgsql_changed_ways
839-
SELECT DISTINCT w.id
839+
SELECT w.id
840840
FROM {schema}"{prefix}_ways" w, changed_buckets b
841841
WHERE w.nodes && b.node_ids
842842
AND {schema}"{prefix}_index_bucket"(w.nodes)
@@ -845,7 +845,7 @@ INSERT INTO osm2pgsql_changed_ways
845845
} else {
846846
queries.emplace_back(R"(
847847
INSERT INTO osm2pgsql_changed_ways
848-
SELECT DISTINCT w.id
848+
SELECT w.id
849849
FROM {schema}"{prefix}_ways" w, osm2pgsql_changed_nodes n
850850
WHERE w.nodes && ARRAY[n.id]
851851
)");
@@ -854,15 +854,15 @@ INSERT INTO osm2pgsql_changed_ways
854854
if (m_options->middle_database_format == 1) {
855855
queries.emplace_back(R"(
856856
INSERT INTO osm2pgsql_changed_relations
857-
SELECT DISTINCT r.id
857+
SELECT r.id
858858
FROM {schema}"{prefix}_rels" r, osm2pgsql_changed_nodes n
859859
WHERE r.parts && ARRAY[n.id]
860860
AND r.parts[1:way_off] && ARRAY[n.id]
861861
)");
862862
} else {
863863
queries.emplace_back(R"(
864864
INSERT INTO osm2pgsql_changed_relations
865-
SELECT DISTINCT r.id
865+
SELECT r.id
866866
FROM {schema}"{prefix}_rels" r, osm2pgsql_changed_nodes c
867867
WHERE {schema}"{prefix}_member_ids"(r.members, 'N'::char) && ARRAY[c.id];
868868
)");

0 commit comments

Comments
 (0)