Hi there,
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?
Hi there,
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 won't be able to optimize this as it stands.
Because of the wildcard at the front of the search, it is required to scan the entire table for matches, meaning it can't use indexes.
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.
You need full text index. This may help http://wiki.postgresql.org/wiki/Full%5FText%5FIndexing%5Fwith%5FPostgreSQL
Generally like '%something%' is not indexable.
But.
There are couple of issues: