Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a database and want to be able to look up in a table a search that's something like: select * from table where column like "abc%def%ghi" or select * from table where column like "%def%ghi" Is there a way to index the column so that this isn't too slow?

Edit: Can I also clarify that the database is read only and won't be updated often.

share|improve this question
    
I think you need a full-text index – user2511414 Jul 13 '13 at 19:35
    
This question is a better fit for dba.stackexchange.com – Dave Jarvis Sep 30 '14 at 0:34

Options for text search and indexing include:

From the minimal information given above, I'd say that only a trigram index will be able to help you, since you're doing infix searches on a string and not looking for dictionary words. Unfortunately, trigram indexes are huge and rather inefficient; don't expect some kind of magical performance boost, and keep in mind that they take a lot of work for the database engine to build and keep up to date.

share|improve this answer

For the like operator use one of the operator classes varchar_pattern_ops or text_pattern_ops

create index test_index on test_table (col varchar_pattern_ops);

That will only work if the pattern does not start with a % in which case another strategy is required.

share|improve this answer
2  
If full-text searching is needed, pg_trgm might work. I've used it in the past and it works pretty well under certain conditions. It should be noted that the indexes get quite large. IIRC, about 2.5x the size of the indexed column. – bma Jul 14 '13 at 3:25

If you need just to, for instance, get unique substrings in an entire table, you can create a substring index:

CREATE INDEX  i_test_sbstr ON tablename (substring(columname, 5, 3)); 
-- start at position 5, go for 3 characters

It is important that the substring() parameters in the index definition are
the same as you use in your query.

ref: http://www.postgresql.org/message-id/[email protected]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.