我有这个查询,它连接了几个表,然后是很多字段的groupbys。
SELECT a.SVC_ORD_NBR
,a.PO_NBR
,f.CUST_NBR as SHIPTO_NBR
,f.CUST_NAME
,f.CUST_ID
,f.CUST_LVL_2_NBR as Fleet_num
,f.CUST_LVL_2_NAME as Fleet
,f.CITY_NAME as SHIPTO_CITY
,f.STATE_CD as SHIPTO_STATE
,a.CREATE_DTM
,e.START_DTM as ACTVTY_START
,e.END_DTM as ACTVTY_END
,e.OPER_NBR
,o.DEBIT_MEMO_DTM
,o.CLOSE_DTM
,c.EQUIP_NBR as SAP_EQUIP_NBR
,c.TECH_NBR as Door_ID
,c.EQUIP_TYP_CD
,c.EQUIP_TYP_DESC
,c.BEGIN_WRNTY_DTE
,c.END_WRNTY_DTE
,b.REPLCMT_DTE
,h.PRIORITY_CD
,h.PRIORITY_DESC
,b.REPLCMT_CYCLE_CNT
,b.PREV_REPLCMT_DTE
,b.PREV_REPLCMT_CYCLE_CNT
,d.PROD_CD
,d.PRIM_PROD_DESC
,d.CUST_PROD_CTGY_CD
,b.ACTVTY_TYP_CD
,b.ACTVTY_TYP_DESC
,b.QTY
,b.UOM
,b.GRS_AMT_DCRNCY
,b.DISC_AMT_DCRNCY
,b.NET_AMT_DCRNCY
,a.STD_LABR_NET_AMT_DCRNCY
,a.OVTM_LABR_NET_AMT_DCRNCY
,a.TRAVEL_NET_AMT_DCRNCY
,a.MATL_NET_AMT_DCRNCY
,a.TAX_NET_AMT_DCRNCY
,a.FRT_NET_AMT_DCRNCY
,h.PM_ACTVTY_TYP_CD
,h.PM_ACTVTY_TYP_DESC
,b.CAUSE_CD
,b.CAUSE_DESC
,a.BRNCH_ID
,br.BRNCH_CD
,br.BRNCH_NAME
,SAP_SLS_ORG_NAME
FROM DMT_MAS_SVC_ORD_FACT a
INNER JOIN DMT_MAS_SVC_ACTVTY_FACT b on a.SVC_ORD_ID=b.SVC_ORD_ID
INNER JOIN DMT_MAS_SVC_ACTVTY_DEGEN_DIM e on e.SVC_ACTVTY_ID=b.SVC_ACTVTY_ID
INNER JOIN DMT_MAS_EQUIP_STORE_DIM c on b.EQUIP_STORE_ID=c.EQUIP_STORE_ID
INNER JOIN DMT_MAS_PROD_DIM d on b.PROD_ID=d.PROD_ID
INNER JOIN DMT_MAS_CUST_DIM f on a.SHIPTO_CUST_ID=f.CUST_ID
INNER JOIN DMT_MAS_SVC_ORD_MISC_DIM h on a.SVC_ORD_MISC_DIM_ID = h.SVC_ORD_MISC_DIM_ID
INNER JOIN DMT_MAS_SVC_ORD_DEGEN_DIM o on o.SVC_ORD_ID = a.SVC_ORD_ID
INNER JOIN DMT_MAS_BRNCH_DIM br on br.BRNCH_ID = a.BRNCH_ID
WHERE CUST_LVL_2_NBR IN ('0080000268')
AND e.START_DTM>='2016-01-01'
AND f.SRC_SYS_DEL_FLAG != 'Y'
AND CUST_NAME NOT LIKE '%USE%'
AND CUST_NAME NOT LIKE '%DNU%'
AND CUST_NAME NOT LIKE '%CLOSED%'
AND CUST_NAME NOT LIKE '%use%'
AND CUST_NAME NOT LIKE '%RELOCATED%'
AND CUST_NAME NOT LIKE '%OLD%'
AND CREATE_DTM >= NOW() - INTERVAL '1.5 years'
GROUP BY a.SVC_ORD_NBR
,a.PO_NBR
,f.CUST_NBR
,f.CUST_NAME
,f.CUST_ID
,f.CUST_LVL_2_NBR
,f.CUST_LVL_2_NAME
,f.CITY_NAME
,f.STATE_CD
,e.START_DTM
,e.END_DTM
,e.OPER_NBR
,c.EQUIP_NBR
,c.TECH_NBR
,c.EQUIP_TYP_CD
,c.EQUIP_TYP_DESC
,c.BEGIN_WRNTY_DTE
,c.END_WRNTY_DTE
,b.REPLCMT_DTE
,h.PRIORITY_CD
,h.PRIORITY_DESC
,b.REPLCMT_CYCLE_CNT
,b.PREV_REPLCMT_DTE
,b.PREV_REPLCMT_CYCLE_CNT
,d.PROD_CD
,d.PRIM_PROD_DESC
,d.CUST_PROD_CTGY_CD
,b.ACTVTY_TYP_CD
,b.ACTVTY_TYP_DESC
,b.QTY
,b.UOM
,b.GRS_AMT_DCRNCY
,b.DISC_AMT_DCRNCY
,b.NET_AMT_DCRNCY
,a.STD_LABR_NET_AMT_DCRNCY
,a.OVTM_LABR_NET_AMT_DCRNCY
,a.TRAVEL_NET_AMT_DCRNCY
,a.MATL_NET_AMT_DCRNCY
,a.TAX_NET_AMT_DCRNCY
,a.FRT_NET_AMT_DCRNCY
,h.PM_ACTVTY_TYP_CD
,h.PM_ACTVTY_TYP_DESC
,b.CAUSE_CD
,b.CAUSE_DESC
,a.CREATE_DTM
,a.BRNCH_ID
,br.BRNCH_CD
,br.BRNCH_NAME
,SAP_SLS_ORG_NAME
,o.DEBIT_MEMO_DTM
,o.CLOSE_DTM
完成返回33080行的查询大约需要20分钟。我在查询上运行了explain analyze
,结果发现大部分时间都在seq scan
上进行。第二个耗时的任务是hash join
。
确切地说,它是表DMT_MAS_SVC_ACTVTY_FACT
,其中有6米行,执行seq扫描。
Seq Scan on dmt_mas_svc_actvty_fact b (cost=0.00..397,938.32 rows=6,851,932 width=730) (actual time=0.029..2,427.990 rows=6,851,932 loops=1)
就hash join
而言,它与已经编入索引的表svc_ord_id
的{{1}}字段有关。所以我想那里没什么可做的。
dmt_mas_svc_actvty_fact
我不明白为这么大的桌子做了昂贵的Hash Join (cost=189,428.58..613,158.11 rows=9,647 width=2,916) (actual time=1,021.686..5,234.921 rows=36,276 loops=1)
Hash Cond: (b.svc_ord_id = a.svc_ord_id)
。我该怎么做才能提高性能? seq scan
的完整输出低于
explain (analyze, verbose, buffers)