Skip to content

Updating Tiler SQL queries to Materialized view #917

Closed

Description

@Rub21

From: #909

To improve the performance of SELECT queries in the database, I have decided to eliminate the use of functions such as ST_Simplify, JSONB, and Filters , etc.

These functionalities, such as geometry simplification, JSON selection, and filtering, will be moved to Imposm. It will handle geometry simplification and apply filters during insertion and updates. This way, the PostgreSQL functions and filters will only run once, reducing the load during SELECT queries .

How will this process work in Imposm?

For example, in the water_lines layer, in the previous workflow with Imposm, we only had 3 tables:

    - water_lines_gen0
    - water_lines_gen1
    - water_lines.

Now, for water_lines, we have tables structured according to the zoom levels:

    -  water_lines_zoom_8_9
    - water_lines_zoom_10_11
    - water_lines_zoom_12_14
    - water_lines_zoom_15_20
    - water_lines

To achieve this, it will use the generalized_tables functionality in the Imposm configuration. This feature allows us to create simplified (or “generalized”) versions of geometries with different levels of detail and specific filters.

Previous Structure:

The previous structure consisted of a limited set of tables.

{
  "tags": {
    "load_all": true,
    "exclude": ["created_by", "source", "source:datetime"]
  },
  "generalized_tables": {
    "water_lines_gen0": {
      "source": "water_lines_gen1",
      "sql_filter": null,
      "tolerance": 200
    },
    "water_lines_gen1": {
      "source": "water_lines",
      "sql_filter": null,
      "tolerance": 50.0
    }
  },
  "tables": {
    "water_lines": {
      "fields": [
        {
          "type": "id",
          "name": "osm_id",
          "key": null
        },
       ....
  }
}

New Structure:

In the new structure, I create additional tables with appropriate filters tailored for specific zoom levels.

{
  "tags": {
    "load_all": true,
    "exclude": [
      "created_by",
      "source",
      "source:datetime"
    ]
  },
  "generalized_tables": {
    "water_lines_zoom_8_9": {
      "source": "water_lines",
      "sql_filter": "type IN ('river', 'canal')",
      "tolerance": 1000
    },
    "water_lines_zoom_10_11": {
      "source": "water_lines",
      "sql_filter": "type IN ('river', 'canal', 'cliff', 'dam')",
      "tolerance": 100
    },
    "water_lines_zoom_12_14": {
      "source": "water_lines",
      "sql_filter": "type IN ('river', 'canal', 'cliff', 'dam', 'stream')",
      "tolerance": 10
    },
    "water_lines_zoom_15_20": {
      "source": "water_lines",
      "sql_filter": "type IN ('river', 'canal', 'cliff', 'dam', 'stream', 'ditch', 'drain')"
    }
  },
  "tables": {
    "water_lines": {
      "fields": [
        {
          "type": "id",
          "name": "osm_id",
          "key": null
        }
        ...

By moving these filters to the import stage with Imposm, We could ensure that each table is pre-optimized for a specific zoom range and removing complex queries from tiler-server.

Improvements for tiles SELECT queries

  • Instead of using the value of a JSONB key in queries, the new config will store those values in their own columns.

    tags->'bridge' AS bridge, 
    tags->'start_date' AS start_date, 
    tags->'end_date' AS end_date,

For the above example, it is going to create dedicated columns such as bridge, start_date, and end_date, and extract the corresponding data.

  • Removing filters in the query, since the data is already being filtered during the Imposm stage, we could simplify the SELECT queries. This eliminates unnecessary runtime filtering

    • previous
        type IN ('water', 'riverbank') 
        AND area > 100000000 
        AND geometry && !BBOX!
    • Updated:
    geometry && !BBOX!
  • Removing ST_Simplify fuction: Currently, we use this function to simplify objects and reduce tile sizes for display at specific zoom levels. However, we are moving the simplification functionality to Imposm by utilizing its tolerance parameter. apply the required simplification during the import stage an reduce y runtime simplification.

For now, I have moved the simplification and filtering process for a few layers to Imposm in Staging , such as water_areas, water_lines, and boundary_lines.I am still working on the other layers, but I hope this approach will help reduce latency issues in the tiler server by pre-processing the data more effectively.

Note:

  • Staging use just a few resources so it maybe slow the vtiler, it will better to test in production with large machine.

Happy to hear your thoughts on this new approach. Let me know if you have any suggestions or feedback.

cc. @danrademacher @batpad @1ec5 @jeffreyameyer

Activity

  1. 1ec5 commented on Nov 30, 2024

    @1ec5
    Member

    This makes plenty of sense to me. We probably need to configure the boundaries a bit differently in imposm for #800 anyhow.

  2. Rub21 commented on Dec 2, 2024

    @Rub21
    MemberAuthor

    yes, i think boundaries is the biggest table that we are handling, I am going to work on it

  3. Rub21 commented on Dec 12, 2024

    @Rub21
    MemberAuthor

    @1ec5 @danrademacher I have migrated four large tables to imposm: admin boundaries (including centroids), water lines, water areas (including centroids), and transport lines. All simplification processes will now occur in imposm, making the tiler response faster than before. This is because the queries no longer need to process large tables and perform on-the-fly simplifications. The tables are now smaller and contain only the exact types of objects needed for each zoom level

    E.g:
    Here’s the requested table in Markdown format without the source column:

    Zoom Level Filter Simplification
    transport_lines_z5 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction') AND tunnel = 0 AND bridge = 0 1000
    transport_lines_z6 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link') 200
    transport_lines_z7 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed') 150
    transport_lines_z8 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed') 100
    transport_lines_z9 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed') 50
    transport_lines_z10 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed') 20
    transport_lines_z11 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed', 'type', 'tram', 'funicular', 'monorail') 15
    transport_lines_z12 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed', 'tram', 'funicular', 'monorail', 'taxiway', 'runway', 'raceway', 'residential', 'service', 'unclassified') 10
    transport_lines_z13_15 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed', 'tram', 'funicular', 'monorail', 'taxiway', 'runway', 'raceway', 'residential', 'service', 'unclassified') 5
    transport_lines_z16_20 (type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed', 'tram', 'funicular', 'monorail', 'taxiway', 'runway', 'raceway', 'residential', 'service', 'unclassified', 'subway', 'track', 'living_street', 'footway', 'cycleway', 'path', 'pier', 'steps', 'pedestrian', 'ferry') OR class IN ('railway', 'route')) N/A

    So almost for each zoom level we have different simplification and filters. The other layers, like water lines, admin boundaries follows the same pattern.

    There are still pending other tables/layers to simplify, that we may work in the next weeks, what do you think?

  4. Rub21 commented on Dec 15, 2024

    @Rub21
    MemberAuthor

    The issue reported in #923 (comment) was very challenging to identify. In the changeset generated by FK27 had generated several roads, but while some objects (roads) from the changeset appeared in the tiler, one or two did not. I investigated where the error could be, but it took quite some time to figure this out.

    https://www.openhistoricalmap.org/way/200679682#
    image

    As part of our efforts to improve tiler performance, several adjustments have been made to simplify objects in various tables. However, the issue was related to the number of generalization tables being created in Imposm for transport lines layer, I tables generated for almost every zoom level, because of filter and simplification. (See in the previous comment #917 (comment) )

    Layer Source SQL Filter Simplification
    transport_lines_z5_7 transport_lines_z8_9 null(no filter from source) 150
    transport_lines_z8_9 transport_lines_z10_11 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed') 50
    transport_lines_z10_11 transport_lines_z12_13 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed', 'type', 'tram', 'funicular', 'monorail') 15
    transport_lines_z12_13 transport_lines_z14_20 type IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'construction', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'miniature', 'narrow_gauge', 'dismantled', 'abandoned', 'disused', 'razed', 'rail', 'light_rail', 'preserved', 'proposed', 'tram', 'funicular', 'monorail', 'taxiway', 'runway', 'raceway', 'residential', 'service', 'unclassified') OR class IN ('railway', 'route') 5
    transport_lines_z14_20 transport_lines null(Keep all the highway*) 0

    It appears that generating multiple generalized tables for each zoom level prevented the insertion of certain objects, even when similar objects from the same changeset were successfully inserted in the database table. After running several tests, I discovered that reducing the number of generalized tables to five, gruped in zoom levels. resolved the issue.

  5. Rub21 commented on Dec 16, 2024

    @Rub21
    MemberAuthor

    Since we are using a Hetzner server, the tiler server performs faster than before. However, for some reason, Imposm slows down, and when there are many edits, the tiler falls behind by hours in updating. The reason might be that it needs to update many generalized tables.or network.

    e.g in the current time-hours this is the imposm logs

    [2024-12-16T08:40:14Z] 10:56:42 [info] Importing #1676445 including changes till 2024-12-15 14:25:56 +0000 UTC (18h14m18s behind)
    [2024-12-16T08:40:14Z] 10:56:42 [step] Starting: Importing #1676445
    [2024-12-16T08:40:14Z] 10:56:42 [step] Starting: Parsing changes, updating cache and removing elements
    [2024-12-16T08:40:14Z] 10:56:42 [progress]     0s C:       0/s (0) N:       0/s (0) W:       0/s (0) R:      0/s (0)
    [2024-12-16T08:40:14Z] 10:56:42 [step] Finished: Parsing changes, updating cache and removing elements in 196.795µs
    [2024-12-16T08:40:14Z] 10:56:42 [step] Starting: Importing added/modified elements
    [2024-12-16T08:40:14Z] 10:56:42 [step] Starting: Updating generalized tables
    [2024-12-16T08:40:14Z] 10:56:42 [step] Finished: Updating generalized tables in 51.53µs
    [2024-12-16T08:40:14Z] 10:56:42 [step] Finished: Importing added/modified elements in 80.902µs
    [2024-12-16T08:40:14Z] 10:56:42 [progress]     0s C:       0/s (0) N:       0/s (0) W:       0/s (0) R:      0/s (0)
    

    But sequence of minute replication are 858.state.txt

    image image

    I am going to put this as a high priority, to work today.

    cc. @1ec5 @danrademacher @jeffreyameyer

  6. Rub21 commented on Dec 16, 2024

    @Rub21
    MemberAuthor

    checking more the logs, some replication files takes 9 minutes to complete update the tiler db

    Checking for expired files to upload... 2024-12-16_09-02-13
    [2024-12-16T09:02:24Z] 11:18:52 [step] Finished: Parsing changes, updating cache and removing elements in 9m46.313276348s
    [2024-12-16T09:02:24Z] 11:18:52 [step] Starting: Importing added/modified elements
    
  7. changed the title [-]Updating Tiler SQL queries[/-] [+]Updating Tiler SQL queries to Materialized view[/+] on Jan 6, 2025
  8. Rub21 commented on Jan 6, 2025

    @Rub21
    MemberAuthor

    Currently, I have migrated a couple of complex queries to Materialized Views, which I believe is the way to go. It provides good response times and improved performance, significantly enhancing the speed of SELECT queries in Tegola vector tiles.

    The materialized views are refreshing every 5 minutes with recent changes coming from Imposm. This approach does not interfere with the table updates performed by Imposm, unlike the previous process that relied on triggers and new tables.

    The following materialized views have been created, filtering data according to the zoom levels.

    • admin_boundaries_lines
    • admin_boundaries_centroids
    • admin_boundaries_maritime
    • amenity_areas.centroids
    • amenity_areas
    • amenity_points
    • buildings.centroids
    • buildings
    • land
    • landuse_areas.centroids
    • landuse_areas
    • landuse_lines
    • landuse_points
    • other_areas.centroids
    • other_areas
    • other_lines
    • other_points
    • place_areas
    • place_points
    • route_lines
    • transport_areas
    • transport_associated_streets
    • transport_lines
    • transport_points
    • water_areas_natural_earth
    • water_areas_centroids
    • water_areas
    • water_lines
  9. added theissue type on Mar 16, 2025
  10. Rub21 commented on Apr 22, 2025

    @Rub21
    MemberAuthor

    This has been completed for now. Later, we can add more materialized views for other tables, but so far we’ve added them to the heaviest ones!

  11. moved this from In Progress to Ready for Staging in OpenHistoricalMap Dev Planningon Apr 22, 2025
  12. moved this from Ready for Staging to In Production in OpenHistoricalMap Dev Planningon May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions