Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mvN1i-0002Xf-RT for pgsql-performance@arkaria.postgresql.org; Thu, 09 Dec 2021 17:16:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mvN1g-0006Cd-KF for pgsql-performance@arkaria.postgresql.org; Thu, 09 Dec 2021 17:16:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mv1uP-0006Vy-Mj for pgsql-performance@lists.postgresql.org; Wed, 08 Dec 2021 18:43:17 +0000 Received: from mout-p-202.mailbox.org ([2001:67c:2050::465:202]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mv1uN-0007M3-0a for pgsql-performance@lists.postgresql.org; Wed, 08 Dec 2021 18:43:16 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:105:465:1:1:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4J8Qz06SZNzQkFk; Wed, 8 Dec 2021 19:43:08 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Subject: Re: pg_trgm word_similarity query does not use index for input strings longer than 8 characters To: Tom Lane , Laurenz Albe Cc: pgsql-performance@lists.postgresql.org References: <9b1caa63c39ff31e5d6c22bb8575368743433028.camel@cybertec.at> <3500099.1638896885@sss.pgh.pa.us> From: pgsql-performance@jhacker.de Message-ID: <144fe629-99b1-c773-7d37-5c1fca5d911a@mailbox.org> Date: Wed, 8 Dec 2021 19:43:03 +0100 MIME-Version: 1.0 In-Reply-To: <3500099.1638896885@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thank you both a lot for the insights and your input. > Yeah, this test case seems very unrealistic, both as to table size > and as to the lack of variability of the table entries. The example was based on real data with a more complicated query which prompted me to investigate the issue. The distinction between slow and fast queries is not as clear cut as with the generated data, but the general problem remains. >> Since you have SSDs, you should tune "random_page_cost = 1.1". I tested different values of random_page_cost with various queries. Too small values increased the execution time again, due to too eager index usage. I identified the optimum for my use case at 1.4. This solved my problem, thanks. Regards Jonathan On 07.12.21 18:08, Tom Lane wrote: > Laurenz Albe writes: >> On Tue, 2021-11-30 at 22:38 +0100, pgsql-performance@jhacker.de wrote: >>> INSERT INTO song (artist, title) >>> SELECT 'artist','title' >>> FROM generate_series(1,10000); >>> >>> \set query '12345678' >>> >>> -- This query is slow >>> EXPLAIN ANALYZE >>> SELECT song.artist, song.title >>> FROM song >>> WHERE (song.artist %> :'query' OR song.title %> :'query') >>> ; > >> The table is quite small; with a bigger table, the test would be more meaningful. > > Yeah, this test case seems very unrealistic, both as to table size > and as to the lack of variability of the table entries. I think the > latter is causing the indexscans to take less time than they otherwise > might, because none of the extracted trigrams find any matches. > >> Since you have SSDs, you should tune "random_page_cost = 1.1". > > Right. Poking at gincostestimate a bit, I see that for this > operator the indexscan cost estimate is basically driven by the > number of trigrams extracted from the query string (nine in this > test case) and the index size; those lead to a predicted number > of index page fetches that's then scaled by random_page_cost. > That's coming out to make it look more expensive than the seqscan. > It's actually not more expensive, but that's partially because > page fetch costs are really zero in this test case (everything > will stay in shared buffers the whole time), and partially because > the unrealistic data pattern is leading to not having to look at > as much of the index as gincostestimate expected. > > In general, it appears correct that longer query strings lead to a > higher index cost estimate, because they produce more trigrams so > there's more work for the index match to do. (At some level, a > longer query means more work in the seqscan case too; but our cost > models are inadequate to predict that.) > > regards, tom lane >