<div dir="ltr"><div>Hi Raúl,</div><div><br></div><div>Thanks for your suggestions!</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">* Make sure Postgresql and Postgis are build with optimizations on. You<br>can build with both -O2 (or -O3) and -g, so you get optimized code with<br>symbols. Disable any extra logging.<br></blockquote><div>Actually, I build Postgres and PostGIS with `-O0` to generate a detailed function calling information in the `FlameGraph` before. </div><div>And I test the index with the same build version. I will change the build later.</div><div><br></div><div>I also made a test document draft here((<a href="https://docs.google.com/document/d/1m4oxBAsKCyjAnYmkCmQ0X_ltiid5tliFwF3rtdzlKsc/edit?usp=sharing" target="_blank">https://docs.google.com/document/d/1m4oxBAsKCyjAnYmkCmQ0X_ltiid5tliFwF3rtdzlKsc/edit?usp=sharing</a>).</div><div>And I am not sure why the `no index` way of querying would only hit 18 buffers. I am working on that.</div><div><br></div><div>Best regards,</div><div>Han</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jul 27, 2021 at 4:55 AM Raúl Marín <raul@rmr.ninja> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
As Paul is mentioning, it appears that you are assuming that having lots <br>
of shared buffer hits is good but it isn't. A shared buffer hit meant <br>
that you needed to read a page and it happened to be cached in memory; <br>
but indexes are useful because they avoid the need of reading <br>
unnecessary pages (in memory or in disk), so an ideal sort would be one <br>
that put the data you are looking in the same pages (so all the rows in <br>
the page are useful) and the ideal index would be one that knew which <br>
pages as fast as possible (also reading as few index pages as possible).<br>
<br>
It's good that you are measuring query times and it's clear that <br>
something odd is going on.<br>
<br>
If I had to debug this I would start by making sure I'm measuring things <br>
that are comparable:<br>
* For the queries running indexes, check that they are using the same <br>
plan and that they are always using the index in the queries.<br>
* Run multiple queries (repeatedly) for each index, not just once and <br>
get minimum / avg / mode of the timings.<br>
* Make sure Postgresql and Postgis are build with optimizations on. You <br>
can build with both -O2 (or -O3) and -g, so you get optimized code with <br>
symbols. Disable any extra logging.<br>
* Disable Postgresql JIT (if you haven't done it already) as it won't <br>
help you here and will make things harder to measure.<br>
* Confirm you are getting the same output for all indexes and queries.<br>
<br>
If you have that already, I would recommend focusing on just one query <br>
(I'd say && is the most used operator) and confirm that you see similar <br>
results with different input (changing the geometry you are querying <br>
against the index). It's not the same to run a query that looks for 1% <br>
of the data, that one that looks for 10% or 90% (where the index isn't <br>
useful and probably won't be used by Postgres).<br>
If you have something that is more or less reproducible then it's easier <br>
to debug with an smaller dataset and you can use anything from a <br>
profiler (like perf / Xcode Instruments), extra logs, etc to try to <br>
understand how many pages you are trying to read with the query and why <br>
it is so different between gist and hilbert presort.<br>
<br>
Regards,<br>
Raúl.<br>
<br>
On 26/7/21 20:55, Paul Ramsey wrote:<br>
> The column names in the table are a little confusing :)<br>
><br>
> I don't have any thing I can think that would explain the execution time difference, so you'll probably need to gather more evidence. Comparing the call counts (gproff? callgrind?) might give a clue where the extra work is going in the sorted indexes. Actually, if your sort function was in fact "desorting" the inputs, that could explain both the extra shared buffer hits and the performance... like, if your whole index floats into memory then you'd expect lots of buffer hits, and the more work on the index, the more hits. So if you had a terrible index you'd get both lots of buffer hits and a lot of time spent.<br>
><br>
> Basically, it shouldn't take a lot of extra steps on a sorted index than on a normal gist index, so if there *are* a lot of extra index accesses... something is very awry in the sorting.<br>
><br>
> P.<br>
><br>
>> On Jul 25, 2021, at 10:21 PM, Han Wang <<a href="mailto:hanwgeek@gmail.com" target="_blank">hanwgeek@gmail.com</a>> wrote:<br>
>><br>
>> Hi Giuseppe and Hi Regina,<br>
>><br>
>> After checking the paper of GiST and implementation in Postgres. I think the query performance should be considered besides the building process. In the larger data test scenario, the building time of different indexes are similar because Postgres just hashes the tuples and sorts them and packs them into pages, building the tree index from bottom to up. With a bad hash order definition, the building process cannot detect the poor index query performance. So it is necessary to test the index query performance. I have tested the query performance with the `EXPLAIN` operator, using the sql scripts like other indexes in the `/regress`. But I am not familiar with PL/pgSQL, so I handle the log with some python scripts.<br>
>><br>
>> In this test, I focus on the buffer hits and execution time of different tasks of different index types including `No Index`, `Simple GiST index`, `X hash function`, `morton hash function` and `hilbert hash function`. And there are some results:<br>
>> Shared buffer hits:<br>
>> Index Create Time(ms) << &< && &> >> ~= ~ @ &<| <<| |>> |&><br>
>> 0 No Index 0 18 18 18 18 18 18 18 18 18 18 18 18<br>
>> 1 GiST Index 25.249 40237 46085 3009 40297 46025 3009 3009 3009 41994 45295 41934 45355<br>
>> 2 X PreSort Index 8.829 443620 441235 3009 444501 440354 3009 3009 3009 441568 442503 440687 443384<br>
>> 3 Morton PreSort Index 16.885 447779 447446 4079 448669 446556 4079 4079 4079 445362 449428 444472 450318<br>
>> 4 Hilbert PreSort Index 16.824 446714 444058 3558 447600 443172 3558 3558 3558 446072 445394 445186 446280<br>
>> Execution time:<br>
>><br>
>> Index Create Time(ms) << &< && &> >> ~= ~ @ &<| <<| |>> |&><br>
>> 0 No Index 0 567.251 565.720 481.618 561.877 563.452 480.128 478.275 478.518 567.144 572.191 563.255 556.281<br>
>> 1 GiST Index 25.249 289.255 293.143 28.838 289.002 291.296 28.336 28.597 26.947 295.394 297.556 293.988 299.760<br>
>> 2 X PreSort Index 8.829 440.861 445.630 37.960 439.564 440.535 37.594 37.979 37.741 386.662 384.635 385.166 388.832<br>
>> 3 Morton PreSort Index 16.885 421.999 413.427 77.002 422.939 412.130 77.415 75.102 76.056 416.205 446.599 410.614 434.613<br>
>> 4 Hilbert PreSort Index 16.824 417.539 415.962 56.583 421.226 414.553 56.320 55.600 55.338 416.639 421.243 418.550 417.094<br>
>> The number of shared buffer hits are far bigger than the original one. But what confuses me is that the execution times are worse. I am trying to figure out why this happened.<br>
>> What's more, I am not very clear about the relationship between query performance and the number of shared buffer hits.<br>
>><br>
>> If you have any questions or suggestions, please let me know.<br>
>><br>
>> Best regards,<br>
>> Han<br>
>><br>
>> On Thu, Jul 8, 2021 at 1:32 AM Giuseppe Broccolo <<a href="mailto:g.broccolo.7@gmail.com" target="_blank">g.broccolo.7@gmail.com</a>> wrote:<br>
>> Hi Han,<br>
>><br>
>> Il giorno mer 7 lug 2021 alle ore 18:05 Han Wang <<a href="mailto:hanwgeek@gmail.com" target="_blank">hanwgeek@gmail.com</a>> ha scritto:<br>
>> Hi Regina and hi Giuseppe,<br>
>><br>
>> Thanks for your reply!<br>
>><br>
>> I am now checking the original paper and postgres's implement of gist. And now I think the query performance test after building gist index is necessary. Because as Darafei mentioned, the fast sorting building method may just pack the sorted tuples into pages regardless of which hash function it use. The correctness of index may be checked in the runtime query. At present, I am working on confirm the io access of hash functions.<br>
>><br>
>> Feel free to give suggestions or questions.<br>
>><br>
>> The correctness of the index should be covered by some of the regression tests in action for the GiST support in PostGIS - for instance, kNN searches etc. - for the moment I am really curious about the I/O access of the hash functions. This is something maybe it has not been checked even in PostgreSQL, and maybe it would be good to share the results with them as well.<br>
>><br>
>> Keep us updated, and thank you for your help!<br>
>><br>
>> Giuseppe.<br>
>> _______________________________________________<br>
>> postgis-devel mailing list<br>
>> <a href="mailto:postgis-devel@lists.osgeo.org" target="_blank">postgis-devel@lists.osgeo.org</a><br>
>> <a href="https://lists.osgeo.org/mailman/listinfo/postgis-devel" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-devel</a><br>
> _______________________________________________<br>
> postgis-devel mailing list<br>
> <a href="mailto:postgis-devel@lists.osgeo.org" target="_blank">postgis-devel@lists.osgeo.org</a><br>
> <a href="https://lists.osgeo.org/mailman/listinfo/postgis-devel" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-devel</a><br>
<br>
<br>
_______________________________________________<br>
postgis-devel mailing list<br>
<a href="mailto:postgis-devel@lists.osgeo.org" target="_blank">postgis-devel@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/postgis-devel" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-devel</a><br>
</blockquote></div>