Re: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%') - Mailing list pgsql-admin

From Tom Lane
Subject Re: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')
Date
Msg-id 2400807.1698674900@sss.pgh.pa.us
Whole thread Raw
In response to Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')  (Loles <lolesft@gmail.com>)
List pgsql-admin
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



pgsql-admin by date:

Previous
From: Ron
Date:
Subject: Re: AUTOVACUUM
Next
From: Jeff Janes
Date:
Subject: Re: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')