pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adé <ade.hey@gmail.com>
To: pgsql-hackers@lists.postgresql.org
Subject: [PATCH] Fix for slow GIN index queries when "gin_fuzzy_search_limit" setting is relatively small for large tables
Date: Sun, 2 Feb 2020 13:06:01 -0500
Message-ID: <CAEknJCdS-dE1Heddptm7ay2xTbSeADbkaQ8bU2AXRCVC2LdtKQ@mail.gmail.com> (raw)

Hello all,

Like the title says, using "gin_fuzzy_search_limit" degrades speed when it
has a relatively low setting.

What do I mean by "relatively low"? An example would be when a table with a
GIN index has many millions of rows and a particular keyword search has
1,000,000 possible results because the keyword is very common (or it's just
that the table is so supremely large that even a somewhat common keyword
appears enough to return one million results). However, you only want to
return around 100 random results from that one million, so you set
gin_fuzzy_search_limit to 100. That limit is relatively low when you look
at the ratio of the limit value to the possible results: 100 / 1,000,000  =
0.0001. You'll find the query is very slow for such a low ratio. It isn't
so slow if gin_fuzzy_search_limit is 100 but the keyword search has only a
total of 10,000 possible results (resulting in a higher ratio of 0.1).

This would explain why in the documentation it is said that "From
experience, values in the thousands (e.g., 5000 — 20000) work well". It's
not so common to have queries that return large enough result sets such
that gin_fuzzy_search_limit values between 5,000 and 20,000 would result in
low ratios and so result in the performance issue I've observed (these
gin_fuzzy_search_limit values have relatively high ratios between 0.005 and
0.02 if you have 1,000,000 results for a keyword search). However, if you
desire a lower gin_fuzzy_search_limit such as 100, while also having a
relatively larger table, you'll find this slowness issue.

I discussed this issue more and the reason for it in my original bug
report:
https://www.postgresql.org/message-id/16220-1a0a4f0cb67cafdc@postgresql.org

Attached is SQL to test and observe this issue and also attached is a patch
I want to eventually submit to a commitfest.

Best regards,
Adé

Attachments:

  gin_fuzzy_search_limit_test.sql (application/octet-stream, 1.9K) download

ginget.patch (application/octet-stream, 1.0K) download --- /src/backend/access/gin/ginget.c +++ /src/backend/access/gin/ginget.c @@ -906,13 +906,20 @@ } else { + bool droppedItem = false; /* A posting tree */ do { /* If we've processed the current batch, load more items */ while (entry->offset >= entry->nlist) { - entryLoadMoreItems(ginstate, entry, advancePast, snapshot); + /* Load items past curItem if item was dropped and curItem is greater than advancePast */ + if (droppedItem == true && ginCompareItemPointers(&entry->curItem, &advancePast) > 0) + { + entryLoadMoreItems(ginstate, entry, entry->curItem, snapshot); + } else { + entryLoadMoreItems(ginstate, entry, advancePast, snapshot); + } if (entry->isFinished) { @@ -923,8 +930,10 @@ entry->curItem = entry->list[entry->offset++]; + droppedItem = false; + } while (ginCompareItemPointers(&entry->curItem, &advancePast) <= 0 || - (entry->reduceResult == true && dropItem(entry))); + (entry->reduceResult == true && (droppedItem = dropItem(entry)))); } }

view thread (6+ messages)  latest in thread

Message-ID: <CAEknJCdS-dE1Heddptm7ay2xTbSeADbkaQ8bU2AXRCVC2LdtKQ@mail.gmail.com>
Permalink:  ../CAEknJCdS-dE1Heddptm7ay2xTbSeADbkaQ8bU2AXRCVC2LdtKQ@mail.gmail.com/
Also on:    postgresql.org/message-id/CAEknJCdS-dE1Heddptm7ay2xTbSeADbkaQ8bU2AXRCVC2LdtKQ@mail.gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: ade.hey@gmail.com
  Subject: Re: [PATCH] Fix for slow GIN index queries when "gin_fuzzy_search_limit" setting is relatively small for large tables
  In-Reply-To: <CAEknJCdS-dE1Heddptm7ay2xTbSeADbkaQ8bU2AXRCVC2LdtKQ@mail.gmail.com>
  References: <CAEknJCdS-dE1Heddptm7ay2xTbSeADbkaQ8bU2AXRCVC2LdtKQ@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox