Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

 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 = AND parcela_rel.area_id = areas.gid AND parcela_rel.parcela_id =
share|improve this question
up vote 4 down vote accepted


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

share|improve this answer
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
@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
Nice solution. That solves the problem. Thanks. – Pablo May 9 '11 at 12:17

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

share|improve this answer

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

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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