Re: why index scan not working when using 'like'?

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: LIANHE SHAO <lshao2(at)jhmi(dot)edu>, pgsql-performance(at)postgresql(dot)org
Subject: Re: why index scan not working when using 'like'?
Date: 2003-11-25 19:51:40
Message-ID: 200311251151.40781.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Lianhe,

> I want to use index on the gene_symbol column in my
> query and gene_symbol is indexed. but when I use
> lower (gene_symbol) like lower('%mif%'), the index
> is not used. While when I change to
> lower(gene_symbol) = lower('mif'), the index is used
> and index scan works, but this is not what I like. I
> want all the gene_symbols containing substring
> 'mif' are pulled out, and not necessarily exactly match.

LIKE '%mif%' is what's called an "unanchored text search" and it cannot use an
index. The database *has* to scan the full text looking for the substring.
This is true of all database platforms I know of.

In regular text fields containing words, your problem is solvable with full
text indexing (FTI). Unfortunately, FTI is not designed for arbitrary
non-language strings. It could be adapted, but would require a lot of
hacking.

So you will need to find a way to restructure you data to avoid needing
unanchored text searches. One way would be to break down the gene_symbol
field into its smallest atomic components and store those in an indexed child
table. Or if you're searching on the same values all the time, you can
create a partial index.

--
-Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Dror Matalon 2003-11-25 19:56:13 Re: why index scan not working when using 'like'?
Previous Message LIANHE SHAO 2003-11-25 19:48:49 why index scan not working when using 'like'?