有什么方法可以优化下面的postgreSQL代码?

时间:2019-05-22 13:54:53

标签: sql postgresql query-optimization greenplum

我编写了此SQL查询,以从greenplum datalake获取数据。主表有强壮的80万行,我正在与其他表连接。下面的查询花费大量时间才能得出结果。较长的查询时间可能是什么原因?如何解决?

select    
          a.pole,
          t.country_name,
          a.service_area,  
          a.park_name,

          t.turbine_platform_name,
          a.turbine_subtype,
          a.pad as "turbine_name",
          t.system_number as "turbine_id",
          a.customer,
          a.service_contract,   

          a.component,
          c.vendor_mfg as "component_manufacturer",

          a.case_number,
          a.description as "case_description",
          a.rmd_diagnosis as "case_rmd_diagnostic_description",
          a.priority as "case_priority",
          a.status as "case_status",
          a.actual_rootcause as "case_actual_rootcause",
          a.site_trends_feedback as "case_site_feedback",
          a.added as "date_case_added",
          a.start as "date_case_started",
          a.last_flagged as "date_case_flagged_by_algorithm_latest",
          a.communicated as "date_case_communicated_to_field",
          a.field_visible_date as "date_case_field_visbile_date",
          a.fixed as "date_anamoly_fixed",
          a.expected_clse as "date_expected_closure",
          a.request_closure_date as "date_case_request_closure",
          a.validation_date as "date_case_closure",
          a.production_related,
          a.estimated_value as "estimated_cost_avoidance",
          a.cms,
          a.anomaly_category,
          a.additional_information as "case_additional_information",
          a.model,
          a.full_model,
          a.sent_to_field as "case_sent_to_field"

      from app_pul.anomaly_stage a
 left join ge_cfg.turbine_detail t on a.scada_number = t.system_number and a.added > '2017-12-31'
 left join tbwgr_v.pmt_wmf_tur_component_master_t c on a.component = c.component_name

2 个答案:

答案 0 :(得分:0)

您的查询基本上是:

 select . . .
 from app_pul.anomaly_stage a left join
      ge_cfg.turbine_detail t 
      on a.scada_number = t.system_number and
         a.added > '2017-12-31' left join
         tbwgr_v.pmt_wmf_tur_component_master_t c
         on a.component = c.component_name

首先,忽略a上的条件,因为它是left join中的第一个表,并且是on子句。因此,我假设您实际上打算对其进行过滤,因此将查询编写为:

 select . . .
 from app_pul.anomaly_stage a left join
      ge_cfg.turbine_detail t 
      on a.scada_number = t.system_number left join
      tbwgr_v.pmt_wmf_tur_component_master_t c
      on a.component = c.component_name
 where a.added > '2017-12-31'

这可能有助于提高性能。然后在Postgres中,您需要在turbine_detail(system_number)pmt_wmf_tur_component_master_t(component_name)上建立索引。令人怀疑的是,索引是否会对第一个表有所帮助,因为您已经在选择大量数据。

我不确定Greenplum中的索引是否合适。

答案 1 :(得分:-2)

  1. 验证联接是否使用各自的主键和外键。
  2. 尝试执行查询,以消除一个左连接在另一个左连接之后的情况,因此您将重点放在问题上。
  3. 尝试使用计划执行。