Re: type-casting and LIKE queries

From: Lincoln Yeoh <lyeoh(at)pop(dot)jaring(dot)my>
To: valerian <valerian2(at)hotpop(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: type-casting and LIKE queries
Date: 2003-03-15 06:00:35
Message-ID: 5.1.0.14.1.20030315134412.0281fb70@mbox.jaring.my
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 02:15 PM 3/14/03 -0500, valerian wrote:
>this? I noticed that the ? character doesn't have this behavior, so I
>tried a few things like:
>
> SELECT * FROM test WHERE home_phone::text LIKE '??????4820'
>
>and this does use the index, but it's a bit of a hack, especially if
>you're searching on a varchar(255) column...

Are you using the ? character as a wildcard? AFAIK _ and % are the wildcard
characters for LIKE.

> > or alternatively harness the immense power of algebra to achieve your
> > devious ends :)
>
>Not sure what you're implying here, unless maybe you were thinking along
>the lines of:
>
> SELECT * FROM test WHERE home_phone >= 4070000000 AND home_phone <=
> 4079999999
>
>But unfortunately that won't work, as I have to be able to do searches on
>any substring of the home_phone column...

Would having two indexes cover enough cases for you? One that allows
indexed LIKE '407%'. And one that allows '%4820'.

For the latter just reverse the string and index it, and do a search on
LIKE '0284%'.

Maybe you could create a function that reverses strings, not sure how that
would work tho - could look messy since you probably don't want to reverse
the % too.

Not sure if Postgresql would be able to productively use both indexes to
find a substring in the middle of text, given a suitable query. If the
substring is in a fixed position in the middle I think it can.

Regards,
Link.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Lincoln Yeoh 2003-03-15 06:09:19 Re: The folding of unquoted names to lower case in
Previous Message Tom Lane 2003-03-15 04:02:08 Re: query optimisation