PostgreSQL CROSS JOIN indexing for performance

后端 未结 1 1679
自闭症患者
自闭症患者 2021-01-27 09:46

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         


        
1条回答
  •  滥情空心
    2021-01-27 10:12

    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.

    0 讨论(0)
提交回复
热议问题