]> git.openstreetmap.org Git - nominatim.git/blob - lib-sql/indices.sql
Merge pull request #3926 from lonvia/rework-postcode-handling
[nominatim.git] / lib-sql / indices.sql
1 -- SPDX-License-Identifier: GPL-2.0-only
2 --
3 -- This file is part of Nominatim. (https://nominatim.org)
4 --
5 -- Copyright (C) 2025 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
7
8 -- Indices used only during search and update.
9 -- These indices are created only after the indexing process is done.
10
11 CREATE INDEX IF NOT EXISTS idx_place_addressline_address_place_id
12   ON place_addressline USING BTREE (address_place_id) {{db.tablespace.search_index}};
13 ---
14 CREATE INDEX IF NOT EXISTS idx_placex_rank_search
15   ON placex USING BTREE (rank_search) {{db.tablespace.search_index}};
16 ---
17 CREATE INDEX IF NOT EXISTS idx_placex_rank_address
18   ON placex USING BTREE (rank_address) {{db.tablespace.search_index}};
19 ---
20 CREATE INDEX IF NOT EXISTS idx_placex_parent_place_id
21   ON placex USING BTREE (parent_place_id) {{db.tablespace.search_index}}
22   WHERE parent_place_id IS NOT NULL;
23 ---
24 CREATE INDEX IF NOT EXISTS idx_placex_geometry ON placex
25   USING GIST (geometry) {{db.tablespace.search_index}};
26 ---
27 -- Index is needed during import but can be dropped as soon as a full
28 -- geometry index is in place. The partial index is almost as big as the full
29 -- index.
30 DROP INDEX IF EXISTS idx_placex_geometry_lower_rank_ways;
31 ---
32 CREATE INDEX IF NOT EXISTS idx_placex_geometry_reverse_lookupPolygon
33   ON placex USING gist (geometry) {{db.tablespace.search_index}}
34   WHERE St_GeometryType(geometry) in ('ST_Polygon', 'ST_MultiPolygon')
35     AND rank_address between 4 and 25
36     AND name is not null AND indexed_status = 0 AND linked_place_id is null;
37 ---
38 -- used in reverse large area lookup
39 CREATE INDEX IF NOT EXISTS idx_placex_geometry_reverse_lookupPlaceNode
40   ON placex USING gist (ST_Buffer(geometry, reverse_place_diameter(rank_search)))
41   {{db.tablespace.search_index}}
42   WHERE rank_address between 4 and 25
43     AND name is not null AND linked_place_id is null AND osm_type = 'N';
44 ---
45 CREATE INDEX IF NOT EXISTS idx_osmline_parent_place_id
46   ON location_property_osmline USING BTREE (parent_place_id) {{db.tablespace.search_index}}
47   WHERE parent_place_id is not null;
48 ---
49 CREATE INDEX IF NOT EXISTS idx_osmline_parent_osm_id
50   ON location_property_osmline USING BTREE (osm_id) {{db.tablespace.search_index}};
51
52 {% if drop %}
53 ---
54   DROP INDEX IF EXISTS idx_placex_geometry_address_area_candidates;
55   DROP INDEX IF EXISTS idx_placex_geometry_buildings;
56   DROP INDEX IF EXISTS idx_placex_wikidata;
57   DROP INDEX IF EXISTS idx_placex_rank_address_sector;
58   DROP INDEX IF EXISTS idx_placex_rank_boundaries_sector;
59 {% else %}
60 -- Indices only needed for updating.
61 ---
62   CREATE INDEX IF NOT EXISTS idx_location_area_country_place_id
63     ON location_area_country USING BTREE (place_id) {{db.tablespace.address_index}};
64 ---
65   CREATE UNIQUE INDEX IF NOT EXISTS idx_place_osm_unique
66     ON place USING btree(osm_id, osm_type, class, type) {{db.tablespace.address_index}};
67 ---
68 -- Table needed for running updates with osm2pgsql on place.
69   CREATE TABLE IF NOT EXISTS place_to_be_deleted (
70     osm_type CHAR(1),
71     osm_id BIGINT,
72     class TEXT,
73     type TEXT,
74     deferred BOOLEAN
75    );
76 ---
77   CREATE INDEX IF NOT EXISTS idx_location_postcodes_parent_place_id
78     ON location_postcodes USING BTREE (parent_place_id) {{db.tablespace.address_index}};
79 {% endif %}
80
81 -- Indices only needed for search.
82 {% if 'search_name' in db.tables %}
83 ---
84   CREATE INDEX IF NOT EXISTS idx_search_name_nameaddress_vector
85     ON search_name USING GIN (nameaddress_vector) WITH (fastupdate = off) {{db.tablespace.search_index}};
86 ---
87   CREATE INDEX IF NOT EXISTS idx_search_name_name_vector
88     ON search_name USING GIN (name_vector) WITH (fastupdate = off) {{db.tablespace.search_index}};
89 ---
90   CREATE INDEX IF NOT EXISTS idx_search_name_centroid
91     ON search_name USING GIST (centroid) {{db.tablespace.search_index}};
92 ---
93   CREATE INDEX IF NOT EXISTS idx_placex_housenumber
94     ON placex USING btree (parent_place_id)
95     INCLUDE (housenumber) {{db.tablespace.search_index}}
96     WHERE housenumber is not null;
97 ---
98   CREATE INDEX IF NOT EXISTS idx_osmline_parent_osm_id_with_hnr
99     ON location_property_osmline USING btree(parent_place_id)
100     INCLUDE (startnumber, endnumber) {{db.tablespace.search_index}}
101     WHERE startnumber is not null;
102 {% endif %}