You must add an account ID as the first column of any index you create. In fact, this will narrow the search in the strings belonging to this account. For full-text gist or gin indexes, you will need to install the btree_gist or btree_gin extensions.
If you only need to search for the first letters, the simplest and possibly the fastest would be to use regular btree, which supports text operations for both columns and performs 2 searches. To support text prefix and lower () requests to ensure case insensitivity, you will need to use op_class_text_opstern_ops.
CREATE INDEX contacts_firstname_idx ON contacts(aid, lower(firstname) text_pattern_ops); CREATE INDEX contacts_lastname_idx ON contacts(aid, lower(lastname) text_pattern_ops);
Then the query will look something like this:
SELECT * FROM contacts WHERE aid = 123 AND (lower(firstname) LIKE 'an%' OR lower(lastname) LIKE 'an%')
source share