如何优化此Postgres查询?

时间:2019-05-22 11:42:37

标签: postgresql

我尝试了多种优化查询的方法。但是我无法提高性能并减少执行时间。当前执行它需要6773.436毫秒。如果能在2秒之内完成,那就太好了。

铅表有大约60万条记录和100列。

潜在客户指数:

"lead_pkey" PRIMARY KEY, btree (terminal_lead_id)
"lead_alloted_to_idx" btree (alloted_to)
"lead_city_code_idx" btree (city_code)
"lead_company_code_idx" btree (company_code)
"lead_creation_dtm_idx" btree (creation_dtm)
"lead_crm_ecode_idx" btree (crm_ecode)
"lead_crm_loc_code_idx" btree (location_code)
"lead_division_code_idx" btree (division_code)
"lead_fos_id_idx" btree (fos_id)
"lead_gs_up_dtm_idx" btree (gs_up_dtm)
"lead_lead_datetime_idx" btree (lead_datetime)
"lead_lead_type_idx" btree (lead_type)
"lead_lg_no_idx" btree (lg_no)
"lead_line_of_business_idx" btree (line_of_business)
"lead_loan_sub_type_idx" btree (loan_sub_type)
"lead_lost_reason_idx" btree (lost_reason)
"lead_rescheduled_reason_idx" btree (rescheduled_reason)
"lead_source_code_idx" btree (source_code)
"lead_stage_idx" btree (stage)
"lead_sub_outcome_idx" btree (sub_outcome)
"lead_userid_idx" btree (userid)

查询表有40万条记录和120列。

查询指标:

"enquiry_trans_pkey" PRIMARY KEY, btree (terminal_enquiry_id)
"enquiry_comp_code_idx" btree (company_code)
"enquiry_crm_ecode_idx" btree (crm_ecode)
"enquiry_customer_category_code_idx" btree (customer_category_code)
"enquiry_customer_profile_idx" btree (customer_profile)
"enquiry_customer_type_idx" btree (customer_type)
"enquiry_division_code_idx" btree (division_code)
"enquiry_enquiry_date_idx" btree (enquiry_date)
"enquiry_enquiry_entry_date_idx" btree (enquiry_entry_date)
"enquiry_enquiry_mls_idx" btree (mls)
"enquiry_enquiry_no_idx" btree (enquiry_no)
"enquiry_enquiry_trans_id_idx" btree (enquiry_trans_id)
"enquiry_failure_count_idx" btree (failure_count)
"enquiry_fos_id_idx" btree (fos_id)
"enquiry_gs_up_dtm_idx" btree (gs_up_dtm)
"enquiry_is_exported_idx" btree (is_exported)
"enquiry_lg_no_idx" btree (lg_no)
"enquiry_loc_code_idx" btree (location_code)
"enquiry_mls_party_sl_no_idx" btree (mls_party_sl_no)
"enquiry_source_agent_code_idx" btree (source_agent_code)
"enquiry_source_code_idx" btree (source_code)
"enquiry_sub_div_code_idx" btree (sub_div_code)

以下是我尝试进行优化的内容:

1)用ANY(ARRAY [])代替IN。
2)从该查询中取出一个视图,然后使用该视图查询特定的terminal_enquiry_id
3)交换了enquiry e表和ld子查询的位置。
4)表leadenquiry已经有很多索引,所以我不想添加更多索引并提高插入/更新过程的性能。

以下是我的postgres查询:

SELECT e.company_code,e.enquiry_no FROM 
(
    SELECT DISTINCT ON (lg_no)lg_no,creation_dtm,sales_promo_activity FROM lead WHERE 
    lg_no LIKE 'LC%' OR lg_no LIKE 'LW/%'
    ORDER BY lg_no,creation_dtm
) ld
LEFT JOIN enquiry e ON ld.lg_no = e.lg_no
WHERE e.division_code IN ('000002','000024','000004','000025','000015','000005','000010')
AND ld.sales_promo_activity IN ('003253','003258')
AND e.terminal_enquiry_id = '78759a9f5476a19c_1558509584114_0000';

在我的数据库上解释:

                                                         QUERY PLAN                                                     
    --------------------------------------------------------------------------------------------------------------------
     Merge Join  (cost=120289.82..124859.00 rows=1 width=27)
       Merge Cond: ((ld.lg_no)::text = (e.lg_no)::text)
       ->  Subquery Scan on ld  (cost=120285.35..124852.07 rows=964 width=16)
             Filter: ((ld.sales_promo_activity)::text = ANY ('{003253,003258}'::text[]))
             ->  Unique  (cost=120285.35..121719.92 rows=96374 width=28)
                   ->  Sort  (cost=120285.35..121002.63 rows=286914 width=28)
                         Sort Key: lead.lg_no, lead.creation_dtm
                         ->  Seq Scan on lead  (cost=0.00..94276.22 rows=286914 width=28)
                               Filter: (((lg_no)::text ~~ 'LC%'::text) OR ((lg_no)::text ~~ 'LW/%'::text))
       ->  Sort  (cost=4.48..4.48 rows=1 width=31)
             Sort Key: e.lg_no
             ->  Index Scan using enquiry_trans_pkey on enquiry e  (cost=0.42..4.47 rows=1 width=31)
                   Index Cond: ((terminal_enquiry_id)::text = '78759a9f5476a19c_1558509584114_0000'::text)
                   Filter: ((division_code)::text = ANY ('{000002,000024,000004,000025,000015,000005,000010}'::text[]))
    (14 rows)

我们将不胜感激任何帮助。

0 个答案:

没有答案