<div dir="ltr">Two things:<div><br></div><div>(a) I'm curious what happens when you unroll all those embedded subqueries and let the planner try to do what it does best, something like this:</div><div><br></div><div><div>SELECT DISTINCT ON (<a href="http://photo.id">photo.id</a>)</div><div>  <a href="http://photo.id">photo.id</a>, </div><div>  photo.filename, </div><div>  ST_AsText(photo.geometry) AS geometry, </div><div>  ST_AsText(photo.center) AS center, </div><div>  photo.angle</div><div>FROM photo</div><div>JOIN "photoMetadata" meta</div><div>ON <a href="http://photo.id">photo.id</a> = meta."photoId"</div><div>WHERE (key = 'source' AND value = 'KADASTER')</div><div>AND (key = 'year' AND value::int BETWEEN 1866 AND 1981)</div><div>AND ST_Intersects(photo.geometry, ST_SetSRID(ST_MakePoint(4.5063099203616, 51.923602970634), 4326)) </div><div>ORDER BY photo.filenam</div></div><div><br></div><div>(b) You should strongly consider changing your metadata table from the key/value table into a jsonb table, with the metadata in JSON. Then for things like date, and source, you can build functional indexes to allow fast filtering on those common metadata fields, while still allowing fully free-form metadata objects. This would make the whole thing both simpler and a lot faster.</div><div><br></div><div>P.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 16, 2016 at 8:48 AM, Sandro Santilli <span dir="ltr"><<a href="mailto:strk@kbt.io" target="_blank">strk@kbt.io</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Fri, Dec 16, 2016 at 03:33:32PM +0100, Arjen Haayman wrote:<br>
> QUERY PLAN<br>
<br>
[...]<br>
<span class=""><br>
> What does this tell you?<br>
<br>
</span>That your query is too complex ?<br>
Check out <a href="http://explain.depesz.com" rel="noreferrer" target="_blank">http://explain.depesz.com</a><br>
<div class="HOEnZb"><div class="h5"><br>
--strk;<br>
______________________________<wbr>_________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">http://lists.osgeo.org/<wbr>mailman/listinfo/postgis-users</a></div></div></blockquote></div><br></div>