[Qgis-developer] Way to improve attribute search

Martin Dobias wonder.sk at gmail.com
Tue Feb 9 11:25:25 EST 2010


On Tue, Feb 9, 2010 at 9:09 AM,  <luca_manganelli at comune.trento.it> wrote:
>
> Hi,
>
> in my python plugins I always use a simple procedure to search features by
> value. I get every single feature in a loop, get the n-th field value, I
> compare it with a number or a substring.
> The problem is that the performance is poor (with shapefiles / postgis
> layers with over 10.000 features).
> But I don't know if there is another way to improve the performance. And,
> sadly, it doesn't use the power of Postgres query to search features with
> given in a boost of time.

Can you give a code sample of such search?
What is the average time it takes and what time do you need?

I believe any simpler searches could be implemented in c++ e.g. by
creating a new static method:
QList<QgsFeature>
QgsSearchString::findMatchingFeatures(QgsVectorLayer* layer, QString
searchString);

So, given a vector layer and a condition ("ROAD_TYPE = 3 OR ROAD_TYPE
= 5") it will return a list of features that match. Since the whole
search would be done within c++ code, that would avoid performance
penalty of python code with lots of iterations.

In case you need really fast searching, you could create an index -
especially if the queries are for the same field (or same set of
fields). Another choice would be to cache the whole features if there
aren't millions of them.

Martin


More information about the Qgis-developer mailing list