We have a Postgres database which contains 2 millions entries. We have tried using equality search and it works instantly (SELECT * FROM a WHERE b = "asd")
But we would like "LIKE '%asd%'" operation to be fast too. How do we do that?
We have a Postgres database which contains 2 millions entries. We have tried using equality search and it works instantly (SELECT * FROM a WHERE b = "asd")
But we would like "LIKE '%asd%'" operation to be fast too. How do we do that?
You need full text index. This may help http://wiki.postgresql.org/wiki/Full_Text_Indexing_with_PostgreSQL
Generally like '%something%' is not indexable.
But.
There are couple of issues:
You can't really speed it up because that syntax will not allow the indexes to be used. If at all possible you should never use a wildcard as the first part of a LIKE. Without knowing the first character of the field, there is no way possible to use the index, hence you get a table scan which is slow.
Personally I never let my users do a search without giving me the begining of what they are searching for. In SQL server if you must do this, you can set up a full-text search but I don't know if Postgres has that.
Use some kind of "full text" search index, for example PostGres looks like it has some support built in here.