Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qxSwM-0013Tp-NK for pgsql-admin@arkaria.postgresql.org; Mon, 30 Oct 2023 14:08:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qxSwL-00EaDz-2H for pgsql-admin@arkaria.postgresql.org; Mon, 30 Oct 2023 14:08:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qxSwK-00EaDr-Nj for pgsql-admin@lists.postgresql.org; Mon, 30 Oct 2023 14:08:24 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qxSwI-0033l5-8M for pgsql-admin@postgresql.org; Mon, 30 Oct 2023 14:08:23 +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 39UE8KHw2400808; Mon, 30 Oct 2023 10:08:20 -0400 From: Tom Lane To: Loles cc: pgsql-admin@postgresql.org Subject: Re: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%') In-reply-to: References: Comments: In-reply-to Loles message dated "Mon, 30 Oct 2023 14:28:17 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2400806.1698674900.1@sss.pgh.pa.us> Date: Mon, 30 Oct 2023 10:08:20 -0400 Message-ID: <2400807.1698674900@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Loles writes: > We have seen performance down when we use WHERE UPPER(name) LIKE > UPPER('%Alice%') in the condition. > If we compare with WHERE UPPER(name) = UPPER('Alice') it runs very fast. Presumably, that works well because you have a btree index on UPPER(name). But btree can't optimize a pattern with a leading '%' --- see https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-BTREE You could possibly use a pg_trgm index instead, see https://www.postgresql.org/docs/current/pgtrgm.html The performance properties will be somewhat different from btree, but it might get the job done. regards, tom lane