PostgreSQL CROSS JOIN indexing for performance

蹲街弑〆低调 提交于 2019-12-02 10:05:54

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 ids):

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!