If I have a table with millions of records where I am performing a wildcard search, should I be indexing on all columns?
Example:
table.account_id = X
AND (table.col1 LIKE '%abc%'
OR table.col2 LIKE %abc%
OR LOWER(table.col3) LIKE %abc%
OR LOWER(table.col4) LIKE %abc%)
That is returning in about 1-2s depending on what I am searching. However, I need this to be < .5s since it is an autocomplete field.
My question is if I should be creating an index such as:
CREATE INDEX ix_xxxx ON table (col1, col2,lower(col3),lower(col4), owner_firm_id)
or should I be looking at to_tsvector or text_pattern_ops in the index?
I did try something like the below but it didn't get it to < 1s.
CREATE INDEX ix_name ON table
USING gist (col1 gist_trgm_ops, col2 gist_trgm_ops, LOWER(col3) gist_trgm_ops, col4 gist_trgm_ops, account_id);