I have a postgis layer in Qgis that is a result of a complex query. Every screen update, Qgis runs the Query again and that's slow. Any suggestion on how to speed up?

EDIT - Here is the query:

 parcela.id AS id, 
 st_union(areas.the_geom) AS geom,
  (((((('p'::text || round(avg(to_number(dado.valor -> 'pequena'::text, '99'::text)) * 10::numeric, 2)) || ',m'::text) || round(avg(to_number(dado.valor -> 'media'::text, '99'::text)) * 10::numeric, 2)) || ',g'::text) || round(avg(to_number(dado.valor -> 'grande'::text, '99'::text)) * 10::numeric, 2)) || ',mo'::text) || round(avg(to_number(dado.valor -> 'morta'::text, '99'::text)) * 10::numeric, 2) AS "legenda", 
  round(avg(to_number(dado.valor -> 'pequena'::text, '99'::text)) * 10::numeric, 2) AS pequenas, 
  round(avg(to_number(dado.valor -> 'media'::text, '99'::text)) * 10::numeric, 2) AS medias, 
  round(avg(to_number(dado.valor -> 'grande'::text, '99'::text)) * 10::numeric, 2) AS grandes, 
  round(avg(to_number(dado.valor -> 'morta'::text, '99'::text)) / 10::numeric, 2) AS mortas

  FROM petr.dado, petr.parcela, petr.parcela_rel, petr.areas
  WHERE dado.parcela_id = parcela.id AND parcela_rel.area_id = areas.gid AND parcela_rel.parcela_id = parcela.id
  GROUP BY parcela.id  


Is it the QGIS rendering that makes things slow or the PostGIS query. Is the query in the definition of a view or why does it need to med evaluated more than once?

If it is the PostGIS query that is the problem, why can't you just make a table out of the query to make it static and add a spatial index to it?

If you need it to be a dynamic query you need to share more information about the query if it might be optimized in some way.

Regards Nicklas

  • Hi, It is the Postgis query that is slow, it was defined with RT_SQL Layer, it needs to be dynamic, but doesn't need to be evaluated every single zoom in and out. Thanks. – Pablo May 5 '11 at 18:22
  • 1
    @Pablo why does it have to be dynamic. maybe you could have one table prepared for use in qgis updated with triggers when things change in the original table. – Nicklas Avén May 6 '11 at 20:51

Creating indices on the columns in your WHERE condition will speed things up considerably, if you haven't already done that.


QGIS does more work to load a view than it does to load a table. "Viewing" a view in any database viewer like pgAdmin is also slow - it's not a QGIS thing, it's a view thing.

My data doesn't change much - about once a year - so I only use tables. I just don't find I need the abstraction that a view provides. (Good discussion here https://stackoverflow.com/questions/4378068/when-to-use-a-view-instead-of-a-table)

Certainly indexing will speed things up for you. But so will just using a table :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.