Mapnik 显示优化

缘起

日常填坑记录,你值得拥有!

数据库优化

参考资料:

  1. https://wiki.openstreetmap.org/wiki/Osm2pgsql/benchmarks
  2. https://www.cnblogs.com/shanyou/p/3495935.html
  3. https://www.percona.com/blog/2018/08/31/tuning-postgresql-database-parameters-to-optimize-performance/
  4. https://stackify.com/postgresql-performance-tutorial/

Mapnik XML配置优化

  1. Datasource那块的table参数使用查询而不是表名,不要将所有的列都查询出来
    这种方式会查询所有的列

    <Parameter name="table">planet_osm_point</Parameter>
    

    查询需要的列,减少查询列的数量

    (select way,place from planet_osm_point where place is not null) as foo
    
  2. 控制要遍历查询行的数量,可以设置过滤条件

     <Parameter name="table">(select * from planet_osm_point where place is not null) as foo</Parameter>
    

    如果你指向渲染rivers, canals, drains, and streams

    (SELECT * from planet_osm_line where waterway in ('river','canal','drain','stream')) as foo
    

    如果你指向渲染road tunnels

    (SELECT * from planet_osm_line where highway is not null and tunnel in ('yes','true','1')) as foo
    
  3. 将Filtering过滤条件移动到PostGIS的where语句中

  4. 简化过滤条件,优化SQL

  5. 创建索引
    创建索引可以使查询更快

     CREATE INDEX idx_buildings_the_geom ON buildings USING gist(the_geom);
    

    如果过滤或者排序指定的字段,可以堆这个字段配置索引

     CREATE INDEX idx_buildings_code ON buildings USING btree(code);
    
  6. Use asynchronous PostGIS datasource

  7. 一般的Postgresql保持数据库优化。
    你应该打开autovacuum。如果您有正在更新的数据源(例如OpenStreetMap数据),则需要定期对数据库进行ANALYZEREINDEX。您应该不时运行此SQL命令维护

    ANALYZE; REINDEX;
    

    根据您的需要,您可能还希望定期对数据进行CLUSTER

  8. 使用extent参数
    如果没有设置extent参数,mapnik执行查询的sql大概如下:

    SELECT ST_XMin(ext),ST_YMin(ext),ST_XMax(ext),ST_YMax(ext)
    FROM (SELECT ST_Extent(geom) as ext from planet_osm_line) as tmp
    

    这回导致每次查询都会去数据库中从整个结果中遍历查询。有三个参数可以避免整个问题:
    extent_from_subquery

    <Parameter name="extent_from_subquery">true</Parameter>
    

    优点:准确估计范围
    缺点:性能仅在小结果集上获得
    先决条件:

    • table参数使用子查询(而不是只是用一个表名)
    • extent参数没有设置
    • estimate_extent参数没有设置成false

    extent

    <Parameter name="extent">-20037508,-19929239,20037508,19929239</Parameter>
    

    优点:没有数据库开销
    缺点:

    • 如果源数据的更改影响范围,则需要更新XML
    • 精度较低 - 不好因为[我不知道 - 与裁剪计算有关吗?]

    覆盖如下参数:

    • extent_from_subquery
    • estimate_extent

    estimate_extent

    <Parameter name="estimate_extent">true</Parameter>
    

    优点:比不设置任何范围参数更快;对于大型结果集显着
    缺点:对于PostgreSQL> = 8.0.0统计数据由VACUUM ANALYZE收集,结果范围将是实际数据的95%
    覆盖如下参数:extent_from_subquery

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章