Re: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Loles <lolesft(at)gmail(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')
Date: 2023-10-30 14:08:20
Message-ID: 2400807.1698674900@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Loles <lolesft(at)gmail(dot)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

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Jeff Janes 2023-10-30 14:11:55 Re: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')
Previous Message Ron 2023-10-30 13:42:19 Re: AUTOVACUUM