Re: why index scan not working when using 'like'? - Mailing list pgsql-performance

From Josh Berkus
Subject Re: why index scan not working when using 'like'?
Date
Msg-id 200311251151.40781.josh@agliodbs.com
Whole thread Raw
In response to why index scan not working when using 'like'?  (LIANHE SHAO <lshao2@jhmi.edu>)
Responses Re: why index scan not working when using 'like'?  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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


pgsql-performance by date:

Previous
From: Dror Matalon
Date:
Subject: Re: why index scan not working when using 'like'?
Next
From: Gaetano Mendola
Date:
Subject: Re: [Fwd: Re: Optimize]