<div dir="ltr">Hello all,<div><br></div><div>I'm trying to update a table of polygons with the distance to the nearest polygon in the same table and of the same class. I suspect that there is something fishy about the way my query is build.</div>
<div><br></div><div>I was using something like this:</div><div><br></div><div><div>UPDATE cosc.cosn1 </div><div>SET enn = c.ENN<span class="" style="white-space:pre">       </span></div><div>FROM (SELECT DISTINCT ON(g1.gid)  g1.gid As ref_gid, ST_Distance(g1.geom,g2.geom) As ENN    </div>
<div><span class="" style="white-space:pre">            </span>FROM "cosc"."cosn1" As g1, "cosc"."cosn1" As g2   </div><div><span class="" style="white-space:pre">               </span>WHERE g1.gid < g2.gid AND g1.class = g2.class</div>
<div><span class="" style="white-space:pre">            </span>ORDER BY g1.gid, ST_Distance(g1.geom,g2.geom)) as c</div><div>WHERE gid = c.ref_gid;</div></div><div><br></div><div>It's was taking a long time, but that wasn't surprising. I got my results in 150000ms.</div>
<div><br></div><div>While doing some tests I tried no narrow the query a bit by specifying the gid of a feature in the bottom WHERE statement (of the update).</div><div><br></div><div><div>UPDATE cosc.cosn1 </div><div>SET enn = c.ENN<span class="" style="white-space:pre">     </span></div>
<div>FROM (SELECT DISTINCT ON(g1.gid)  g1.gid As ref_gid, ST_Distance(g1.geom,g2.geom) As ENN    </div><div><span class="" style="white-space:pre">              </span>FROM "cosc"."cosn1" As g1, "cosc"."cosn1" As g2   </div>
<div><span class="" style="white-space:pre">            </span>WHERE g1.gid < g2.gid AND g1.class = g2.class</div><div><span class="" style="white-space:pre">           </span>ORDER BY g1.gid, ST_Distance(g1.geom,g2.geom)) as c</div><div>
WHERE gid = 2 AND gid = c.ref_gid;</div></div><div><br></div><div>Also not surprising, this took a lot less, around 300ms</div><div><br></div><div>What started to bug me was the fact that using a different condition to narrow the query by only updating 10 features... </div>
<div><br></div><div><div>UPDATE cosc.cosn1 </div><div>SET enn = c.ENN<span class="" style="white-space:pre">       </span></div><div>FROM (SELECT DISTINCT ON(g1.gid)  g1.gid As ref_gid, ST_Distance(g1.geom,g2.geom) As ENN    </div>
<div><span class="" style="white-space:pre">            </span>FROM "cosc"."cosn1" As g1, "cosc"."cosn1" As g2   </div><div><span class="" style="white-space:pre">               </span>WHERE g1.gid < g2.gid AND g1.class = g2.class</div>
<div><span class="" style="white-space:pre">            </span>ORDER BY g1.gid, ST_Distance(g1.geom,g2.geom)) as c</div><div>WHERE gid < 10 AND gid = c.ref_gid;</div></div><div><br></div><div>It needed 147510ms to show the results, almost the same as updating the all table!</div>
<div><br></div><div>Even more weird was the fact that if I manual set gid = x from [1,10] none of the queries toke more that 400ms to perform.</div><div><br></div><div>Therefore, I must do something wrong for sure!</div><div>
<br></div><div>Thank you for your help,</div><div><br></div><div>Alexandre Neto</div></div>