<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>
<div>As already mentioned by others, the more rows the longer it takes to build the index.</div>

<div> </div>

<div>In preparation for a conference talk, I'm currently testing the performance of different spatial indexes that PostGIS offers: GiST, sp-GiST and BRIN. I'm also testing with 1 billion randomly generated points, lines and polygons and it took me around 6 hours to build the GiST index on my laptop. sp-GIST was usually twice as fast.</div>

<div> </div>

<div>So far, I can say that GiST tends to slightly faster for large data sets than sp-GIST and often twice as fast as BRIN. The big advantage of BRIN imo is that it's fast to build (6hrs vs. 3,5 min) and requires hardly any disk space (50 GB vs. 3,6 MB) due to its simplicity. Just give it a try and see if the executions times are fine for you. Note, that it can be necessary to execute SET enable_seqscan = false before. In my case, only then did the query planner recognize my BRIN index.</div>

<div> </div>

<div>For optimal performance your spatial can be clustered on disk. For this, I created a functional btree index transforming the geometries with ST_GeoHash. Btree indexes can be created in parallel with the latest Postgres version (took me 30min). Unfortunately, the CLUSTER command then takes a long time (8hrs on my machine). In the end my test query (a simple containment test) took around 250ms with BRIN (and 220ms with GIST and sp-GIST).</div>

<div> </div>

<div>lg Felix</div>
 

<div> 
<div style="margin: 10.0px 5.0px 5.0px 10.0px;padding: 10.0px 0 10.0px 10.0px;border-left: 2.0px solid rgb(195,217,229);">
<div style="margin: 0 0 10.0px 0;"><b>Gesendet:</b> Sonntag, 13. Januar 2019 um 18:38 Uhr<br/>
<b>Von:</b> "Giuseppe Broccolo" <g.broccolo.7@gmail.com><br/>
<b>An:</b> "PostGIS Users Discussion" <postgis-users@lists.osgeo.org><br/>
<b>Betreff:</b> Re: [postgis-users] Slow construction of GiST index, but better with smaller # of big rows</div>

<div>
<div>
<div>
<div>Hi Wembo,</div>
 

<div class="gmail_quote">
<div>Il giorno sab 12 gen 2019 alle ore 16:29 Wenbo Tao <<a href="mailto:taowenbo1993@gmail.com" onclick="parent.window.location.href='mailto:taowenbo1993@gmail.com'; return false;" target="_blank">taowenbo1993@gmail.com</a>> ha scritto:</div>

<blockquote class="gmail_quote" style="margin: 0.0px 0.0px 0.0px 0.8ex;border-left: 1.0px solid rgb(204,204,204);padding-left: 1.0ex;">
<div>Hello,
<div> </div>

<div>    I was trying to build a GiST index on a geometry column in a table with 1 billion rows. It took an entire week to finish. </div>

<div> </div>

<div>    Then I reduced the number of rows by grouping closer objects into one clump (using some clustering algorithm), and then compressed the clump as one row (the geometry column becomes the bounding box of all objects in that clump). The construction then went way faster -- down to 12 hours. I did this because the query I need to answer is finding all objects whose bbox intersects with a given rectangle. I can now query all clumps whose bbox intersects with the rectangle. </div>

<div> </div>

<div>   So essentially, the index construction is slow for too many rows, but much faster for a smaller # of bigger rows. Any intuition why this is the case would be greatly appreciated!</div>
</div>
</blockquote>

<div> </div>

<div>Well, building GiST indexes requires an execution time that grows linearly with the size of the dataset (~O(N)). Of course, also hardware (CPU, storage, ...) impacts the build. So long execution times for one billion rows sound reasonable.</div>

<div> </div>

<div>Your solution could be fine: you cluster close objects and index the obtained rows, than you can retrieve the clusters themselves and finally find the exact match. Of course, it is not an "elegant" solution.</div>

<div> </div>

<div>You already had the suggestion to partition your table, and then index the single partitions, that could be completely fine.</div>

<div> </div>

<div>A second suggestion I would like to give you, is to consider BRIN indexing, thought specifically for large datasets:<br/>
<br/>
<a href="https://postgis.net/docs/using_postgis_dbmanagement.html#brin_indexes" target="_blank">https://postgis.net/docs/using_postgis_dbmanagement.html#brin_indexes</a><br/>
 </div>

<div>Of course, there are some limitations with this index, so I invite you to read the linked documentation and consider your specific use case. But for intersections between bbox (and your case looks to be the case), BRINs could be a really good solution.</div>

<div> </div>

<div>Hope this can help,</div>

<div>Giuseppe.</div>
</div>
</div>
</div>
_______________________________________________ postgis-users mailing list postgis-users@lists.osgeo.org <a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a></div>
</div>
</div>
</div></div></body></html>