Thread: Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')
We have a simple query with several tables joined.
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.
We have tried an index on the name field, an UPPER(name) expression index, a GIN index, etc. but nothing seems to work.
We need to optimize the condition WHERE UPPER(name) LIKE UPPER('%p_name%)
Hi,
You can try with extension pg_tgrm to create gin index to use LIKE with ‘%expression%’
https://www.postgresql.org/docs/current/pgtrgm.html
https://mazeez.dev/posts/pg-trgm-similarity-search-and-fast-like
Sylvie Halat
De : Loles <lolesft@gmail.com>
Envoyé : lundi 30 octobre 2023 14:28
À : pgsql-admin@postgresql.org
Objet : Optimized WHERE UPPER(name) LIKE UPPER('%p_name%')
Hi!
A question about query optimization.
We have a simple query with several tables joined.
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.
We have tried an index on the name field, an UPPER(name) expression index, a GIN index, etc. but nothing seems to work.
We need to optimize the condition WHERE UPPER(name) LIKE UPPER('%p_name%)
Any ideas?
Thanks!!
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
Hi!A question about query optimization.
We have a simple query with several tables joined.
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.
We have tried an index on the name field, an UPPER(name) expression index, a GIN index, etc. but nothing seems to work.
- https://www.postgresql.org/docs/current/pgtrgm.html
- https://www.postgresql.org/docs/current/datatype-textsearch.html#DATATYPE-TSVECTOR
- https://www.postgresql.org/docs/current/textsearch.html
- https://www.postgresql.org/docs/current/functions-textsearch.html
- https://www.postgresql.org/docs/current/fuzzystrmatch.html