giggls / mapnik-german-l10n

OSM map l10n functions
Other
28 stars 40 forks source link

Improve osml10n_get_country() perf by 20% #47

Closed nyurik closed 4 years ago

nyurik commented 4 years ago

Use SQL instead of PLPGSQL to gain 20% of performance. Measured with profile-pg-func tool. To test locally. Must have docker, make, and git.

-- File get-country.sql
CREATE or REPLACE FUNCTION osml10n_get_country_o(feature geometry) RETURNS TEXT AS $$
 DECLARE
  country text;
 BEGIN
   SELECT country_code into country
   from country_osm_grid
   where st_contains(geometry, st_centroid(st_transform(feature,4326)))
   order by area limit 1;
   return country;
 END;
$$ LANGUAGE 'plpgsql' STABLE;

CREATE or REPLACE FUNCTION osml10n_get_country_n(feature geometry) RETURNS TEXT AS $$
 SELECT country_code
 from country_osm_grid
 where st_contains(geometry, st_centroid(st_transform(feature,4326)))
 order by area
 limit 1;
$$ LANGUAGE SQL STABLE;

Results

Importing get_country.sql...
Running 2 functions x 10 runs x 10,000 each...
...
% slower    Function                                     AVG of 8 runs    MIN             MAX                  STDEV
----------  -------------------------------------------  ---------------  --------------  --------------  ----------
            2) osml10n_get_country_n({random_geopoint})  0:00:01.751501   0:00:01.741989  0:00:01.759559  0.00634216
21.0%       1) osml10n_get_country_o({random_geopoint})  0:00:02.118865   0:00:02.101815  0:00:02.138070  0.0137719