<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal>Hi,<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I’ve got this database that has photos that have a location and a table with key/value metadata. There are almost 450,000 photos and 5 million rows of metadata.<o:p></o:p></p><p class=MsoNormal>Searching on metadata alone always works, but when I add a spatial search to it the query freezes if the spatial component is too precise, i.e. if I search at a certain point or on a bounding box that is too small. My workaround now is to always extend to a bounding box with a minimum size. <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I cannot find a way how to tackle this. EXPLAIN ANALYZE fails just as miserably as the original query. I’ve tried all kinds of indexes, but nothing works.<o:p></o:p></p><p class=MsoNormal>The queries have been running successfully for years but at a certain point in time the database got too big apparently and now this happens.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>What I really don’t understand why it would fail when the query gets too specific, I mean searching on a point should be easier than searching a large bounding box. It usually is the other way around??<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Here’s an example query:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>SELECT <o:p></o:p></p><p class=MsoNormal style='text-indent:36.0pt'>"id", <o:p></o:p></p><p class=MsoNormal style='text-indent:36.0pt'>"filename", <o:p></o:p></p><p class=MsoNormal style='text-indent:36.0pt'>ST_AsText("geometry") AS "geometry", <o:p></o:p></p><p class=MsoNormal style='text-indent:36.0pt'>ST_AsText("center") AS "center", <o:p></o:p></p><p class=MsoNormal style='text-indent:36.0pt'>"angle"<o:p></o:p></p><p class=MsoNormal>FROM "photo" <o:p></o:p></p><p class=MsoNormal>WHERE (ST_Intersects("geometry", st_GeomFromText( 'POINT(4.5063099203616 51.923602970634)', 4326))) <o:p></o:p></p><p class=MsoNormal style='text-indent:36.0pt'>AND ("id" IN (<o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt;text-indent:36.0pt'>SELECT  "photoId" FROM "photoMetadata" <o:p></o:p></p><p class=MsoNormal style='margin-left:72.0pt;text-indent:36.0pt'>WHERE ("photoId" IN (<o:p></o:p></p><p class=MsoNormal style='margin-left:108.0pt;text-indent:36.0pt'>SELECT DISTINCT "photoId" FROM "photoMetadata" <o:p></o:p></p><p class=MsoNormal style='margin-left:144.0pt'>WHERE ("value" = 'KADASTER') AND ("key" = 'source'))) <o:p></o:p></p><p class=MsoNormal>                                             AND ("photoId" IN (<o:p></o:p></p><p class=MsoNormal style='margin-left:108.0pt;text-indent:36.0pt'>SELECT DISTINCT "photoId" FROM "photoMetadata" <o:p></o:p></p><p class=MsoNormal style='margin-left:108.0pt;text-indent:36.0pt'>WHERE (key = 'year' AND ( cast(value as int ) >= 1866 AND cast ( value as int ) <= 1981 )))))) <o:p></o:p></p><p class=MsoNormal>ORDER BY "filename" LIMIT 36<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>So this fails. This means that it takes way too long. And a few of these queries running at the same time completely clogs my machine.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>When I change it to something like (ST_Intersects("geometry", st_GeomFromText( 'POLYGON((6.1444640338619 52.265808403464,6.1444640338619 52.281808403464,6.1496640338619 52.281808403464,6.1496640338619 52.265808403464,6.1444640338619 52.265808403464))', 4326)))<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>where the extent of the geometry needs to be large enough.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Does anyone have any clues how to tackle this?<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>