Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Using "WHERE LIKE '%Word'" That *will* Use An Index... Works But Feels Stupid!


FYI, I'm using INNODB.

Let's say I have a table with a city column containing 30,000 records, and I want to provide a search form 'starts with' and 'ends with fields.

So a user would supply the following:

Starts with: Cha
Ends with: tte

And the search would find a record such as 'Charlotte'

The 'starts with' search can use an index (eg, LIKE 'Cha%'), but ends with can't (eg, LIKE '%tte').

A solution seems to be to create a city_reverse column, add a unique index on that, then perform an 'ends with' search on the city names in reverse.

For instance, the 'Charlotte' record would be stored in the city_reverse column as such:

'ettolrahC'

...and I'd perform an 'ends with' search using the unique index:

LIKE 'ett%'

The above works, but it feels stupid, and adds weight to the database. Is there an alternative?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread

    Copyright © 2005-08 www.BigResource.com, All rights reserved