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 1muQsD-0003AY-Ee for pgsql-performance@arkaria.postgresql.org; Tue, 07 Dec 2021 03:10:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1muQsC-0006DN-6W for pgsql-performance@arkaria.postgresql.org; Tue, 07 Dec 2021 03:10:32 +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 1muQsB-0006DD-G8 for pgsql-performance@lists.postgresql.org; Tue, 07 Dec 2021 03:10:31 +0000 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1muQs3-00059d-S4 for pgsql-performance@lists.postgresql.org; Tue, 07 Dec 2021 03:10:30 +0000 Received: by mail-wr1-x429.google.com with SMTP id a9so26397729wrr.8 for ; Mon, 06 Dec 2021 19:10:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=byBd6GJnvX9P0T6xdTDVUok4BdKwnks22KvV0KGvWGA=; b=ZbalN81AHn9hyYLpbLLqlmajPZdEGbYxAC4ozoufWChi+fqe4wAbTgG5IEA6J4HCbi exeF65zinMeQTm+/miNBuuZZ6GqTEi7A93MOpavDdsFWczJmdf7Hp8xP1KWw57kC0+vT s5OXmJSj0GDcpPl2XjgmKlf5rzbLkRp5avwV5glWU/hYv6eZaKbOTO50BtH8Y2QykQGJ 6Vtc891540kEQ60Cpb5fuE3gOcJp3HU6F+4P/G2aRw8vI3nfAczqhX0YlIguujpwzbcd P4dJhCUgDH/KEE05P3bNIjfkmu04dqxVT7PeVQU6VbkfY9LjpKl92x0YNKHVdDuh5MnM bYLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=byBd6GJnvX9P0T6xdTDVUok4BdKwnks22KvV0KGvWGA=; b=01CJrin7Cpy94+ujM2KJXkHjaWFSDXrpfpq6SSJvk02tJQYMxUSMxvoZ7qvTWW5Shu kC+eH/sHxb1K9Hvs+PbQfBd6V2/qc061kb4Cke5o9ZNcEy22PTHTeRCtEtlOLtieMgXx cU0uV7wLapRRngtlXLrsztxpdotHQrIo1HJPVg5S09BLRbmZM01E9/LejwLs+O1gd1Dd mI+YHmTG64G3qVhWsL6KF+YfWpS2ixsRtI7bkyp1867A86LT0flTjQh4oAdfG9MeC5j8 83aOyUPWDnB+VCHLLE3NS0+706Uh6PAWaq6JG+8Ay0mHRhbO95ccHkVUL8+GiF8/m8SL GE4g== X-Gm-Message-State: AOAM531wF8a6f+73rA7bIgU63OgjaIHhafYZf4W4gkP0d/XCIZ1by8DW 9k2fdzf3PrkT8l6DgXGtLwxvHg5rtPbn7g== X-Google-Smtp-Source: ABdhPJxTxJy0cP71GWwR6tg+i6vPQHCfBeNoPMVBnUj4d6BsA22lz1SRIbOSBhdNToq4gGrNjaiMog== X-Received: by 2002:adf:f1cc:: with SMTP id z12mr47773807wro.395.1638846622047; Mon, 06 Dec 2021 19:10:22 -0800 (PST) Received: from localhost.localdomain (62-46-248-236.adsl.highway.telekom.at. [62.46.248.236]) by smtp.gmail.com with ESMTPSA id k37sm1128007wms.21.2021.12.06.19.10.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Dec 2021 19:10:21 -0800 (PST) Message-ID: <9b1caa63c39ff31e5d6c22bb8575368743433028.camel@cybertec.at> Subject: Re: pg_trgm word_similarity query does not use index for input strings longer than 8 characters From: Laurenz Albe To: pgsql-performance@jhacker.de, pgsql-performance@lists.postgresql.org Date: Tue, 07 Dec 2021 04:10:20 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-2.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2021-11-30 at 22:38 +0100, pgsql-performance@jhacker.de wrote: > ## Setup Information > Hardware: Intel i5-8250U, 8GB RAM, encrypted SSD, no RAID > [...] > > Configuration: > The config file was not changed. > [...] > > ## Test Case > [...] > CREATE EXTENSION pg_trgm; > > CREATE TABLE song ( >      artist      varchar(20), >      title       varchar(20) > ); > > INSERT INTO song (artist, title) > SELECT 'artist','title' > FROM generate_series(1,10000); > > CREATE INDEX artist_trgm ON song USING GIN (artist gin_trgm_ops); > CREATE INDEX title_trgm ON song USING GIN (title gin_trgm_ops); > > -- Tips from https://wiki.postgresql.org/wiki/Slow_Query_Questions > ANALYZE; > VACUUM; > REINDEX TABLE song; > > \set query '12345678' > > -- This query is slow > EXPLAIN ANALYZE > SELECT song.artist, song.title > FROM song > WHERE (song.artist %> :'query' OR song.title %> :'query') > ; > > set enable_seqscan=off; > > -- This query is fast > 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. Since you have SSDs, you should tune "random_page_cost = 1.1". This makes the planner prefer index scans, and it leads to the index scan being chosen in your case. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com