GeoDjango endpoint is extremely slow
I'm trying to work with a list of 1.100 rows loaded in a PostgreSQL with PostGIS extension. Each row has the following fields:
- code (integer)
- name (char)
- mpoly (multipolygon)
The idea is to have an endpoint that will be called from a VueJS app to print each geo data in a Leaflet managed map.
The query is extremely slow when serializing it. How can I optimize it?
I thought about having a geojson file, but each row has a manytomany table associated that indicate some groups that it belongs to.
in the past i have similar problem, so i can share some things i do, like:
- if its possible to select only necessary fields, that you truly need. You can fetchin unnecessary data can impact performance. Django only() method is good for this.
- also you can simplify geometrics with simplify() method in GeoDjango - that reduces the number of vertices and can speed up rendering
- as you mentioon GeoJSON, instead of serializing each row individually, you can generate a GeoJSON feature collection containing all your geometries. Serialize the entire collection once and serve it as a single response to your VueJS app.
- do you cache the results? can you implement caching to avoid repeated queries? Can store serialized GeoJSON data in a cache.
- I think you already did this, but can you optimize the DB?
Hope this helps.