pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Loles <lolesft@gmail.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')
Date: Mon, 30 Oct 2023 10:08:20 -0400
Message-ID: <2400807.1698674900@sss.pgh.pa.us> (raw)
In-Reply-To: <CA+FWGK480H3mRVsSy0xofYhvpx9W1Y_h6nGfoOh0tuE0ecJ45g@mail.gmail.com>
References: <CA+FWGK480H3mRVsSy0xofYhvpx9W1Y_h6nGfoOh0tuE0ecJ45g@mail.gmail.com>

Loles <lolesft@gmail.com> 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





view thread (5+ messages)  latest in thread

Message-ID: <2400807.1698674900@sss.pgh.pa.us>
Permalink:  ../2400807.1698674900@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2400807.1698674900@sss.pgh.pa.us

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: tgl@sss.pgh.pa.us, lolesft@gmail.com
  Subject: Re: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')
  In-Reply-To: <2400807.1698674900@sss.pgh.pa.us>

* 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