本文分享自华为云社区《GaussDB(DWS)性能调优:Sort+Groupagg聚集引起的性能瓶颈案例》,作者: O泡果奶~ 。
本文针对SQL语句长时间执行不出来,且verbose执行计划中出现Sort+GroupAgg聚集方式的案例进行分析。
1、【问题描述】
语句执行时间过长,2300s+也无法得出结果。从verbose执行计划可以看出存在sort聚合。
2、【原始语句】
vbnet
SELECT /*+ set global(agg_redistribute_enhancement on) set global (best_agg_plan 3)*/
dm_ebg_glb_kpi_sum_w_v."na_level_name",
dm_ebg_glb_kpi_sum_w_v."na_level",
dm_ebg_glb_kpi_sum_w_v."sales_lv3_prod_list_code",
dm_ebg_glb_kpi_sum_w_v."sales_lv1_prod_list_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv2_prod_list_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv1_prod_list_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv0_prod_list_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv0_prod_list_code",
dm_ebg_glb_kpi_sum_w_v."sales_lv1_prod_list_code",
dm_ebg_glb_kpi_sum_w_v."sales_lv3_prod_list_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv2_prod_list_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv3_prod_list_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv0_prod_list_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv2_prod_list_code",
dm_ebg_glb_kpi_sum_w_v."period_id",
dm_ebg_glb_kpi_sum_w_v."year",
dm_ebg_glb_kpi_sum_w_v."month",
dm_ebg_glb_kpi_sum_w_v."report_category_cn_name",
dm_ebg_glb_kpi_sum_w_v."report_category_en_name",
dm_ebg_glb_kpi_sum_w_v."currency_code",
dm_ebg_glb_kpi_sum_w_v."exchange_rate_type_cn_name",
dm_ebg_glb_kpi_sum_w_v."exchange_rate_type_en_name",
dm_ebg_glb_kpi_sum_w_v."report_item_code",
dm_ebg_glb_kpi_sum_w_v."report_item_cn_name",
dm_ebg_glb_kpi_sum_w_v."report_item_en_name",
dm_ebg_glb_kpi_sum_w_v."report_item_type",
dm_ebg_glb_kpi_sum_w_v."report_item_flag",
dm_ebg_glb_kpi_sum_w_v."region_code",
dm_ebg_glb_kpi_sum_w_v."region_cn_name",
dm_ebg_glb_kpi_sum_w_v."region_en_name",
dm_ebg_glb_kpi_sum_w_v."oversea_flag",
dm_ebg_glb_kpi_sum_w_v."repoffice_code",
dm_ebg_glb_kpi_sum_w_v."repoffice_cn_name",
dm_ebg_glb_kpi_sum_w_v."repoffice_en_name",
dm_ebg_glb_kpi_sum_w_v."ebg_focus_cn_name",
dm_ebg_glb_kpi_sum_w_v."ebg_focus_en_name",
dm_ebg_glb_kpi_sum_w_v."lv0_prod_rnd_team_code",
dm_ebg_glb_kpi_sum_w_v."lv0_prod_rd_team_cn_name",
dm_ebg_glb_kpi_sum_w_v."lv0_prod_rd_team_en_name",
dm_ebg_glb_kpi_sum_w_v."lv1_prod_rnd_team_code",
dm_ebg_glb_kpi_sum_w_v."lv1_prod_rd_team_cn_name",
dm_ebg_glb_kpi_sum_w_v."lv1_prod_rd_team_en_name",
dm_ebg_glb_kpi_sum_w_v."lv2_prod_rnd_team_code",
dm_ebg_glb_kpi_sum_w_v."lv2_prod_rd_team_cn_name",
dm_ebg_glb_kpi_sum_w_v."lv2_prod_rd_team_en_name",
dm_ebg_glb_kpi_sum_w_v."lv3_prod_rnd_team_code",
dm_ebg_glb_kpi_sum_w_v."lv3_prod_rd_team_cn_name",
dm_ebg_glb_kpi_sum_w_v."lv3_prod_rd_team_en_name",
dm_ebg_glb_kpi_sum_w_v."named_account_flag",
dm_ebg_glb_kpi_sum_w_v."domtc_entps_indu_class_code",
dm_ebg_glb_kpi_sum_w_v."domtc_entps_indu_class_cn_name",
dm_ebg_glb_kpi_sum_w_v."domtc_entps_indu_class_en_name",
dm_ebg_glb_kpi_sum_w_v."industry_class_code",
dm_ebg_glb_kpi_sum_w_v."industry_class_cn_name",
dm_ebg_glb_kpi_sum_w_v."industry_class_en_name",
dm_ebg_glb_kpi_sum_w_v."sub_industry_class_code",
dm_ebg_glb_kpi_sum_w_v."sub_industry_class_cn_name",
dm_ebg_glb_kpi_sum_w_v."sub_industry_class_en_name",
dm_ebg_glb_kpi_sum_w_v."focus_industry_cn_name",
dm_ebg_glb_kpi_sum_w_v."focus_industry_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_mode_code",
dm_ebg_glb_kpi_sum_w_v."sales_mode_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_mode_en_name",
SUM(dm_ebg_glb_kpi_sum_w_v."ptd_amt") as "ptd_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."py_ptd_amt") as "py_ptd_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."pp_ptd_amt") as "pp_ptd_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."qtd_amt") as "qtd_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."py_qtd_amt") as "py_qtd_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."pp_qtd_amt") as "pp_qtd_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."ytd_amt") as "ytd_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."py_ytd_amt") as "py_ytd_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."py_all_ytd_amt") as "py_all_ytd_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."end_bal_amt") as "end_bal_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."cp_open_bal_amt") as "cp_open_bal_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."pq_end_bal_amt") as "pq_end_bal_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."cy_open_bal_amt") as "cy_open_bal_amt",
SUM(dm_ebg_glb_kpi_sum_w_v."py_end_bal_amt") as "py_end_bal_amt"
FROM fin_dmr_ebgdis.dm_ebg_glb_kpi_sum_w_v
where 1 = 1
and 1 = 1
AND dm_ebg_glb_kpi_sum_w_v."period_id" = 202302
group by dm_ebg_glb_kpi_sum_w_v."na_level_name",
dm_ebg_glb_kpi_sum_w_v."na_level",
dm_ebg_glb_kpi_sum_w_v."sales_lv3_prod_list_code",
dm_ebg_glb_kpi_sum_w_v."sales_lv1_prod_list_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv2_prod_list_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv1_prod_list_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv0_prod_list_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv0_prod_list_code",
dm_ebg_glb_kpi_sum_w_v."sales_lv1_prod_list_code",
dm_ebg_glb_kpi_sum_w_v."sales_lv3_prod_list_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv2_prod_list_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv3_prod_list_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv0_prod_list_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_lv2_prod_list_code",
dm_ebg_glb_kpi_sum_w_v."period_id",
dm_ebg_glb_kpi_sum_w_v."year",
dm_ebg_glb_kpi_sum_w_v."month",
dm_ebg_glb_kpi_sum_w_v."report_category_cn_name",
dm_ebg_glb_kpi_sum_w_v."report_category_en_name",
dm_ebg_glb_kpi_sum_w_v."currency_code",
dm_ebg_glb_kpi_sum_w_v."exchange_rate_type_cn_name",
dm_ebg_glb_kpi_sum_w_v."exchange_rate_type_en_name",
dm_ebg_glb_kpi_sum_w_v."report_item_code",
dm_ebg_glb_kpi_sum_w_v."report_item_cn_name",
dm_ebg_glb_kpi_sum_w_v."report_item_en_name",
dm_ebg_glb_kpi_sum_w_v."report_item_type",
dm_ebg_glb_kpi_sum_w_v."report_item_flag",
dm_ebg_glb_kpi_sum_w_v."region_code",
dm_ebg_glb_kpi_sum_w_v."region_cn_name",
dm_ebg_glb_kpi_sum_w_v."region_en_name",
dm_ebg_glb_kpi_sum_w_v."oversea_flag",
dm_ebg_glb_kpi_sum_w_v."repoffice_code",
dm_ebg_glb_kpi_sum_w_v."repoffice_cn_name",
dm_ebg_glb_kpi_sum_w_v."repoffice_en_name",
dm_ebg_glb_kpi_sum_w_v."ebg_focus_cn_name",
dm_ebg_glb_kpi_sum_w_v."ebg_focus_en_name",
dm_ebg_glb_kpi_sum_w_v."lv0_prod_rnd_team_code",
dm_ebg_glb_kpi_sum_w_v."lv0_prod_rd_team_cn_name",
dm_ebg_glb_kpi_sum_w_v."lv0_prod_rd_team_en_name",
dm_ebg_glb_kpi_sum_w_v."lv1_prod_rnd_team_code",
dm_ebg_glb_kpi_sum_w_v."lv1_prod_rd_team_cn_name",
dm_ebg_glb_kpi_sum_w_v."lv1_prod_rd_team_en_name",
dm_ebg_glb_kpi_sum_w_v."lv2_prod_rnd_team_code",
dm_ebg_glb_kpi_sum_w_v."lv2_prod_rd_team_cn_name",
dm_ebg_glb_kpi_sum_w_v."lv2_prod_rd_team_en_name",
dm_ebg_glb_kpi_sum_w_v."lv3_prod_rnd_team_code",
dm_ebg_glb_kpi_sum_w_v."lv3_prod_rd_team_cn_name",
dm_ebg_glb_kpi_sum_w_v."lv3_prod_rd_team_en_name",
dm_ebg_glb_kpi_sum_w_v."named_account_flag",
dm_ebg_glb_kpi_sum_w_v."domtc_entps_indu_class_code",
dm_ebg_glb_kpi_sum_w_v."domtc_entps_indu_class_cn_name",
dm_ebg_glb_kpi_sum_w_v."domtc_entps_indu_class_en_name",
dm_ebg_glb_kpi_sum_w_v."industry_class_code",
dm_ebg_glb_kpi_sum_w_v."industry_class_cn_name",
dm_ebg_glb_kpi_sum_w_v."industry_class_en_name",
dm_ebg_glb_kpi_sum_w_v."sub_industry_class_code",
dm_ebg_glb_kpi_sum_w_v."sub_industry_class_cn_name",
dm_ebg_glb_kpi_sum_w_v."sub_industry_class_en_name",
dm_ebg_glb_kpi_sum_w_v."focus_industry_cn_name",
dm_ebg_glb_kpi_sum_w_v."focus_industry_en_name",
dm_ebg_glb_kpi_sum_w_v."sales_mode_code",
dm_ebg_glb_kpi_sum_w_v."sales_mode_cn_name",
dm_ebg_glb_kpi_sum_w_v."sales_mode_en_name" limit 200
3、【性能分析】
由于语句长时间无法执行完毕,通过其verbose执行计划中的E-rows可以看出,由于较小且去重后行数变化不大,优化器采用了Sort+GroupAgg的聚集方式。
通常情况下,Sort+GroupAgg性能并不如Hashagg,此时,可以通过利用
ini
set enable_sort = off;
或是
scss
+set [gloabal] (enable_sort off)
来避免使用Sort+GroupAgg聚集方式。
补充:Sort+GroupAgg与Hashagg对比
从上图中可以看出,调优后语句执行时间下降为22s+,性能大大提高。从performance计划可以看出,原始SQL语句verbose计划中E-rows不准确,导致优化器选择了Sort+GroupAgg聚集方式,从而使得语句执行性能下降。
附件enable_sort-1为调优前verbose执行计划,附件enable_sort-2为调优后performance执行计划