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 1pX9Qv-0000dw-UF for pgsql-admin@arkaria.postgresql.org; Tue, 28 Feb 2023 23:30:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pX9Qu-0001dc-4J for pgsql-admin@arkaria.postgresql.org; Tue, 28 Feb 2023 23:30:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pX9Qt-0001dT-PV for pgsql-admin@lists.postgresql.org; Tue, 28 Feb 2023 23:30:55 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pX9Qr-0008Fs-Mv for pgsql-admin@lists.postgresql.org; Tue, 28 Feb 2023 23:30:55 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 31SNUpNA1278259; Tue, 28 Feb 2023 18:30:51 -0500 From: Tom Lane To: Teju Jakkidi vlogs cc: pgsql-admin Subject: Re: Query taking long with levenshtein function In-reply-to: References: Comments: In-reply-to Teju Jakkidi vlogs message dated "Tue, 28 Feb 2023 15:11:55 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1278257.1677627051.1@sss.pgh.pa.us> Date: Tue, 28 Feb 2023 18:30:51 -0500 Message-ID: <1278258.1677627051@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Teju Jakkidi vlogs writes: > We have a query as below which uses the levenshtein function to compare > strings. > SELECT "name", levenshtein("name",'some string') as p FROM table1 > where levenshtein("name",'some string') <= 2 order by p desc; > We have a GIST index built on top of this table as below: > CREATE INDEX gist_idx ON table1 USING GIST("name"); AFAIK, that index is completely useless for this query. I don't really see a good way to index it either --- "levenshtein distance less than X" seems like a not very tractable requirement. Can you formulate your matching rules some other way? regards, tom lane