The following is an example of the data I can get when searching for “amadeus” substring:
db=# SELECT entidade FROM gma WHERE entidade ILIKE '%amadeus%';
entidade
---------------------------------
Hairdresser Amadeus
Snack-Bar Amadeus
Restaurant Amadeus
Restaurant Amadeus
Restaurant Amadeus
Amadeus - Musical Instruments
(6 rows)
However I want to be able replace ILIKE by LIKE. So I tried to index entidade with only lower-case letters:
db=# CREATE INDEX idx_gma_entidade ON gma USING btree
db-# ( lower(entidade) );
CREATE INDEX
By now I was expecting to access exactly the same data using LIKE:
db=# SELECT entidade FROM gma WHERE entidade LIKE '%amadeus%';
entidade
----------
(0 rows)
But, as you can see, the result is not what I expected…
Can somebody care to explain why? And, if possible, how can I achieve the expected behaviour?
You need to use the lower() function on the column when running your select:
SELECT entidade FROM gma WHERE lower(entidade) LIKE '%amadeus%';But because you have a wildcard at the front, the query will never use an index anyway. So there is no point in creating one