优化以下 postgreSQL 代码的可能方法是什么?
What are the possible ways to optimize the below postgreSQL code?
我写了这个 SQL 查询来从 greenplum datalake 中获取数据。主要 table 有大约 800,000 行,我正在加入其他 table。下面的查询花费了大量的时间来给出结果。查询时间较长的可能原因是什么?如何解决?
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
- 验证联接是否使用各自的主键和外键。
- 尝试执行查询,一个接一个地删除左联接,这样您就会看到问题的焦点。
- 尝试使用计划执行。
您的查询基本上是:
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
中的第一个table,是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)
上的索引。索引对第一个有帮助是值得怀疑的 table,因为您已经在选择大量数据。
我不确定索引在 Greenplum 中是否合适。
我写了这个 SQL 查询来从 greenplum datalake 中获取数据。主要 table 有大约 800,000 行,我正在加入其他 table。下面的查询花费了大量的时间来给出结果。查询时间较长的可能原因是什么?如何解决?
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
- 验证联接是否使用各自的主键和外键。
- 尝试执行查询,一个接一个地删除左联接,这样您就会看到问题的焦点。
- 尝试使用计划执行。
您的查询基本上是:
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
中的第一个table,是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)
上的索引。索引对第一个有帮助是值得怀疑的 table,因为您已经在选择大量数据。
我不确定索引在 Greenplum 中是否合适。