<div dir="ltr"><div>Thanks a lot Nyall, setting the NoGeometry flags and subset of fields does improve the speed by factor 3 for my test layer ( 300k features and 80 (!) fields )</div><div><br></div><div>Regards</div><div>Michaël<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le lun. 25 févr. 2019 à 23:48, Nyall Dawson <<a href="mailto:nyall.dawson@gmail.com">nyall.dawson@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Tue, 26 Feb 2019 at 03:31, kimaidou <<a href="mailto:kimaidou@gmail.com" target="_blank">kimaidou@gmail.com</a>> wrote:<br>
><br>
> Hi all,<br>
><br>
> I needed to gather basic stats for a layer containing multiple fields. I created the following gist to compute the number of features per each unique value for a list of given fields<br>
> <a href="https://gist.github.com/mdouchin/a234efb7e67ebd8dae3a04cb26cf5e72" rel="noreferrer" target="_blank">https://gist.github.com/mdouchin/a234efb7e67ebd8dae3a04cb26cf5e72</a><br>
<br>
Checkout trap #3 from<br>
<a href="http://nyalldawson.net/2016/10/speeding-up-your-pyqgis-scripts/" rel="noreferrer" target="_blank">http://nyalldawson.net/2016/10/speeding-up-your-pyqgis-scripts/</a>.<br>
Specifically, since you aren’t doing anything with the geometry, and<br>
are only using a single attribute from the layer, by calling setFlags(<br>
QgsFeatureRequest.NoGeometry ) and setSubsetOfAttributes() you can<br>
tell QGIS that you don’t need the geometry, and only require a single<br>
attribute’s value in the feature request. This will give a huge speed<br>
boost to the feature fetching.<br>
<br>
> I know I could use PostgreSQL to do it (see <a href="https://twitter.com/kimaidou/status/1100053546978983936" rel="noreferrer" target="_blank">https://twitter.com/kimaidou/status/1100053546978983936</a> conversation), wich is much faster, but I needed a quick way to do it with Python.<br>
> I tested virtual layers too, but my source layer has many fields (with some heavy data) and the copy/pasting into spatialite was not efficient here.<br>
><br>
> The method uniqueValues(fieldIndex) of QgsVectorLayer is pretty fast compared to my iteration through the layer features. Any hint how to improve the speed of my calculation ?<br>
<br>
Potentially this could be a new method in QgsVectorDataProvider which<br>
sends a native query to be executed on the backend (like uniqueValues<br>
does -- that's why it's so fast). But that would require an addition<br>
to the c++ QgsVectorDataProvider class, and implementations in the<br>
popular vector data providers.<br>
<br>
Nyall<br>
<br>
><br>
> Regards,<br>
> Michaël<br>
> _______________________________________________<br>
> QGIS-Developer mailing list<br>
> <a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
> List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
> Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div>