0

I'm using Postgres 8.4 and I'm performing a search using ILIKE. Since I'm searching in 4 columns (containing text) from that table I was wondering if it's ok to create a single index for all the 4 columns and not an index for each column.

Thank you.

1 Answer 1

1

This is a bit of a complicated topic. In general databases will not optimize a LIKE query unless it is anchored to the beginning. If you are searching across 4 columns, then this is LIKEly not the case.

http://www.postgresql.org/docs/8.4/static/indexes-types.html

The optimizer can also use a B-tree index for queries involving the pattern matching operators LIKE and ~ if the pattern is a constant and is anchored to the beginning of the string — for example, col LIKE 'foo%' or col ~ '^foo', but not col LIKE '%bar'. However, if your database does not use the C locale you will need to create the index with a special operator class to support indexing of pattern-matching queries; see Section 11.9 below. It is also possible to use B-tree indexes for ILIKE and ~*, but only if the pattern starts with non-alphabetic characters, i.e., characters that are not affected by upper/lower case conversion.

You may consider the full text support in postgresql if you are doing natural language queries (like a search engine)...

Sign up to request clarification or add additional context in comments.

1 Comment

Or wait für 9.1 which will be able to use an index for LIKE and ILIKE: depesz.com/index.php/2011/02/19/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.