Thread: LIKE vrs ~~

LIKE vrs ~~

From
"T.J.Farrell"
Date:
Hello,

I was wondering about the performance incidence of :
           SELECT *  FROM table1 WHERE UPPER(field1) LIKE
UPPER('%thomas%');

versus:
           SELECT * FROM table1 WHERE field1 ~~ '%thomas%'

if any...





Re: LIKE vrs ~~

From
Peter Eisentraut
Date:
T.J.Farrell writes:

> I was wondering about the performance incidence of :
> SELECT *  FROM table1 WHERE UPPER(field1) LIKE UPPER('%thomas%');
> versus:
> SELECT * FROM table1 WHERE field1 ~~ '%thomas%'

If you're comparing ~~ with LIKE then there's no difference at all. (Only
the extra cycles to convert LIKE to ~~ internally.) If you're comparing 
true case-insensitive matching to using UPPER, then the latter is probably
faster but doesn't really do the same thing.

-- 
Peter Eisentraut                  Sernanders väg 10:115
peter_e@gmx.net                   75262 Uppsala
http://yi.org/peter-e/            Sweden




Re: LIKE vrs ~~

From
Tom Lane
Date:
"T.J.Farrell" <T.J.Farrell@wanadoo.fr> writes:
> I was wondering about the performance incidence of :
>             SELECT *  FROM table1 WHERE UPPER(field1) LIKE
> UPPER('%thomas%');
> versus:
>             SELECT * FROM table1 WHERE field1 ~~ '%thomas%'

Of course "~~" is just an alternate spelling of LIKE, and is
case-sensitive, so the above two queries are not equivalent.
Perhaps you meant to refer to "~*" which is a case-insensitive
regex match ... but then you'd need a different pattern.

Anyway, if you keep an index on upper(field1) then the first form
is the way to go, since the system can use a left-anchored pattern
as an index range restriction.
        regards, tom lane