So, I've a table in postgresql, that is joined to other spatial table.
If I've run a simple query in pgAdmin, let's say SELECT * FROM table1 WHERE "area" > '5000'
and it takes less, than 1s to output a result.
Completely different story is in QGIS.
I'm developing plugin, that needs to fetch some data by query. The code is simple, more less like this:
table1 = iface.activeLayer()
attribute = 'area'
size = 5000
q = "\"{}\" > '{}'".format(attribute, size)
expr = QgsExpression(q)
result = table1.getFeatures(QgsFeatureRequest(expr))
ids = [i.id() for i in result]
and it works just fine, but performance is the issue. It takes about 15-20s to run the same query, where in pgAdmin took less than 1s.
How is that possible? Is that normal, that QGIS is that slow?
At first, I thought about that join is making QGIS so slow, but I can't figure out, why it's not affecting pgAdmin, if that is the case?
My spec:
QGIS 3.4.8
PostgreSQL 10.7
PostGIS 2.5.2
pgAdmin 4
EDIT: Removed 'AND' typo as @Joseph mentioned.