This is the second part of my question. So I have the following table,
CREATE TABLE public.main_transaction
(
id integer NOT NULL DEFAULT nextval(\'main_trans
Your main problem is the OR
— you can never get decent performance as long as you have an OR
like this in your WHERE
clause.
Rewrite the query as follows:
SELECT * FROM main_transaction t
JOIN main_profile p ON t.profile_id = p.id
JOIN main_customer c ON p.user_id = c.id
WHERE upper(t.request_no) LIKE upper(concat('%','0-90-6 12 ','%'))
UNION
SELECT * FROM main_transaction t
JOIN main_profile p ON t.profile_id = p.id
JOIN main_customer c ON p.user_id = c.id
WHERE upper(c.phone) LIKE upper(concat('%','0-90-6 12','%'));
Then make sure you have the following indexes (apart from the indexes on the id
s):
CREATE INDEX ON main_transaction (profile_id);
CREATE INDEX ON main_transaction USING gin (upper(request_no) gin_trgm_ops);
CREATE INDEX ON main_profile (user_id);
CREATE INDEX ON main_customer USING gin (upper(phone) gin_trgm_ops);
That should make a difference.