<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi list,</p>
    <p>using spatial predicates on virtual layers is slow by default as
      spatial indexes are not used.</p>
    <p><a class="moz-txt-link-freetext" href="https://docs.qgis.org/testing/en/docs/user_manual/managing_data_source/create_layers.html#use-of-indexes">https://docs.qgis.org/testing/en/docs/user_manual/managing_data_source/create_layers.html#use-of-indexes</a>
      says that one can use the _search_frame_ column to "trigger
      the use of a spatial index". The page has an example where this is
      compared to a MBR/BBOX:</p>
    <p>SELECT * FROM vtab WHERE
      _search_frame_=BuildMbr(-2.10,49.38,-1.3,49.99,4326)<br>
    </p>
    <p>What does it actually mean and what does it really do? What is
      the _search_frame_ really and why can we compare it for *equality*
      against a simple bounding box? Is the "=" operator used for a
      different logic?<br>
    </p>
    <p>Does it magically "trigger" the use of the sources' spatial
      indexes in the background? That would surprise me.</p>
    <p>The original docs of virtual layers at
      <a class="moz-txt-link-freetext" href="https://github.com/mhugo/qgis_vlayers#index-support">https://github.com/mhugo/qgis_vlayers#index-support</a> _search_frame_
      say</p>
    <p>> For spatial indexes, a 'hidden' field named '<em>search_frame</em>'
      is created for each virtual table (i.e. each referenced layer of
      the virtual layer). The bounding box of the given geometry will be
      used
      to restrain the query to a particular region of space. This
      bounding box is passed to the underlyng provider by a
      QgsFeatureRequest with a "filterRect".</p>
    <p>and show this example:</p>
    <p>> SELECT * FROM pt, poly WHERE pt._search_frame_ =
      poly.geometry AND Intersects(pt.geometry, poly.geometry)</p>
    <p>I have some trouble parsing that in my brain. Does it mean that
      there are N QgsFeatureRequests and each uses the _search_frame_
      BBOX of its feature geometry as filterRect?</p>
    <p><br>
    </p>
    <p>It would be great to demystify this and make people more aware of
      how to make their virtual layers not slow.<br>
    </p>
    <p>Thanks!</p>
    <p>Hannes<br>
      <br>
      PS: QgsFeatureRequest.<span class="sig-name descname"><span
          class="pre">setFilterRect</span></span><span class="sig-paren">
        could use a note about the spatial predicate being used to
        determine which features to return. Intersection probably but it
        is not clear. People might expect Within. Might file/fix this
        later but feel welcome to do it before me. :)<br>
      </span></p>
    <pre class="moz-signature" cols="72">-- 
Johannes Kröger / GIS-Entwickler/-Berater

---------------------------------------------
Schon gewusst?
In unserem Blog geben wir Tipps & Tricks zu Open-Source-GIS-Software
und berichten aus unserem Experten-Alltag:
<a class="moz-txt-link-freetext" href="https://wheregroup.com/blog/">https://wheregroup.com/blog/</a>
---------------------------------------------

WhereGroup GmbH
Grevenweg 89
20537 Hamburg
Germany

Tel: +49 (0)228 / 90 90 38 - 36
Fax: +49 (0)228 / 90 90 38 - 11

<a class="moz-txt-link-abbreviated" href="mailto:johannes.kroeger@wheregroup.com">johannes.kroeger@wheregroup.com</a>
<a class="moz-txt-link-abbreviated" href="http://www.wheregroup.com">www.wheregroup.com</a>
Geschäftsführer:
Olaf Knopp, Peter Stamm 
Amtsgericht Bonn, HRB 9885
-------------------------------
</pre>
  </body>
</html>