如何在oracle中优化左连接子查询

时间:2014-12-22 02:37:45

标签: optimization oracle11g subquery left-join

我想用这么多左连接子查询来优化查询。查看我的查询;

SELECT DISTINCT pi.proforma_no,
  m.vv_cd,
  m.pid,
  bl.bill_lading_nbr,
  bld.cntr_nbr,
  NVL(ts.quantity, bld.quantity) AS quantity,
  vsc.vsl_nm,
  vsc.in_voy_nbr,
  m.rec_status,
  bld.idx_no,
  bld.colli_type,
  bld.volume,
  bld.gross_weight_kgs,
  bld.has_heavy_lift,
  NVL(NVL(pi.fa,pi.consignee),NVL(bld.cust_tariff_client,NVL(bl.fa,bl.consignee))) AS client,
  col.is_pos,
  col.cargo_type,
  bld.direct_delivery,
  cn.cntr_seq_nbr,
  bld.package_type_cd,
  bld.cargo_dtl_desc,
  NVL(ts.quantity, bld.quantity) AS quantity,
  bld.weight,
  bld.t1,
  bl.pload AS port_code,
  bld.purpose,
  bld.volume,
  bld.GROSS_WEIGHT_KGS,
  pi.WAIVE_DOC_FEE,
  pi.WAIVE_DOC_FEE_REMARKS,
  m.pid
  || '='
  || trim(bl.bill_lading_nbr)
  || '='
  || bld.idx_no
  || '='
  || trim(bld.cntr_nbr)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     AS unique_key,
  DECODE(NVL(bld.CUST_TARIFF_CLIENT,''),'', DECODE('N','N',DECODE(tariff.is_metric_ton,'Y',tariff.local_rate                                                                                           *bld.weight,tariff.local_rate*bld.quantity), NVL(DECODE(ext_tariff_toStd.is_metric_ton,'Y',ext_tariff_toStd.local_rate*bld.weight,ext_tariff_toStd.local_rate*bld.quantity),0)), DECODE('N','N',DECODE(custTar.is_metric_ton,'Y',custTar.local_rate*bld.weight,custTar.local_rate*bld.quantity),0))                                          AS local_rate,
  DECODE(NVL(bld.CUST_TARIFF_CLIENT,''),'', DECODE('N','N',DECODE(tariff.is_metric_ton,'Y',tariff.global_rate                                                                                          *bld.weight,tariff.global_rate*bld.quantity), NVL(DECODE(ext_tariff_toStd.is_metric_ton,'Y',ext_tariff_toStd.global_rate*bld.weight,ext_tariff_toStd.global_rate*bld.quantity),0)), DECODE('N','N',DECODE(custTar.is_metric_ton,'Y',custTar.global_rate*bld.weight,custTar.global_rate*bld.quantity),0))                                     AS global_rate,
  DECODE(NVL(bld.CUST_TARIFF_CLIENT,''),'', DECODE('N','N',tariff.version_nbr,DECODE(col.tariff_ext_cost_id,NULL,0,NVL(ext_tariff_toStd.version_nbr,NVL(ext_tariff.version_nbr,tariff.version_nbr)))), -1)                                                                                                                                                                                                                                                                                                                                              AS version_nbr,
  DECODE(NVL(bld.CUST_TARIFF_CLIENT,''),'', DECODE('N','N',tariff.id,DECODE(col.tariff_ext_cost_id,NULL,0,NVL(ext_tariff_toStd.id,NVL(ext_tariff.id,tariff.id)))),                                     -1)                                                                                                                                                                                                                                                                                                                                              AS tariff_id,
  DECODE(NVL(bld.CUST_TARIFF_CLIENT,''),'', DECODE('N','N',0,(NVL(DECODE(ext_tariff.is_metric_ton,'Y',ext_tariff.local_rate                                                                            *bld.weight,ext_tariff.local_rate*bld.quantity),0))), 0)                                                                                                                                                                                                                                                                                         AS local_ext_amount,
  DECODE(NVL(bld.CUST_TARIFF_CLIENT,''),'', DECODE('N','N',0,(NVL(DECODE(ext_tariff.is_metric_ton,'Y',ext_tariff.global_rate                                                                           *bld.weight,ext_tariff.global_rate*bld.quantity),0))), 0)                                                                                                                                                                                                                                                                                        AS global_ext_amount,
  (NVL(ts.quantity, bld.quantity)                                                                                                                                                                          *NVL(disch.local_amount,0)) + DECODE(col.is_pos,'Y',DECODE(NVL(pos.local_amount,0),0,0,(bld.weight*pos.local_amount)),0) + DECODE(col.is_pos_add_rate,'Y',DECODE(bld.weight,0,0,NVL(addPos.local_amount,0)),0)                                                                                                                                   AS local_other_amount,
  (NVL(ts.quantity, bld.quantity)                                                                                                                                                                          *NVL(disch.global_amount,0)) + DECODE(col.is_pos,'Y',DECODE(NVL(pos.global_amount,0),0,0,(bld.weight*pos.global_amount)),0) + DECODE(col.is_pos_add_rate,'Y',DECODE(bld.weight,0,0,NVL(addPos.global_amount,0)),0)                                                                                                                               AS global_other_amount,
  NVL(doc.local_amount,0)                                                                                                                                                                                  + (DECODE(SIGN(ROUND(ROUND(to_number(spTax.param_value)/100,2)*NVL(doc.local_amount,0),2)-ROUND(ROUND(to_number(spTax.param_value)/100,2)*NVL(doc.local_amount,0),0)-0.05),-1, ROUND(ROUND(to_number(spTax.param_value)/100,2)*NVL(doc.local_amount,0),0)+0.05, ROUND(ROUND(to_number(spTax.param_value)/100,2)*NVL(doc.local_amount,0),0)))     AS local_doc_amount,
  NVL(doc.global_amount,0)                                                                                                                                                                                 + (DECODE(SIGN(ROUND(ROUND(to_number(spTax.param_value)/100,2)*NVL(doc.global_amount,0),2)-ROUND(ROUND(to_number(spTax.param_value)/100,2)*NVL(doc.global_amount,0),0)-0.05),-1, ROUND(ROUND(to_number(spTax.param_value)/100,2)*NVL(doc.global_amount,0),0)+0.05, ROUND(ROUND(to_number(spTax.param_value)/100,2)*NVL(doc.global_amount,0),0))) AS global_doc_amount,
  (NVL(ts.quantity, bld.quantity)                                                                                                                                                                          *NVL(disch.local_amount,0))                                                                                                                                                                                                                                                                                                                      AS local_disch_amount,
  DECODE(col.is_pos,'Y',DECODE(NVL(pos.local_amount,0),0,0,(bld.weight                                                                                                                                     *pos.local_amount)),0) + DECODE(col.is_pos_add_rate,'Y',DECODE(bld.weight,0,0,NVL(addPos.local_amount,0)),0)                                                                                                                                                                                                                                     AS local_pos_amount,
  (NVL(ts.quantity, bld.quantity)                                                                                                                                                                          *NVL(disch.global_amount,0))                                                                                                                                                                                                                                                                                                                     AS global_disch_amount,
  DECODE(col.is_pos,'Y',DECODE(NVL(pos.global_amount,0),0,0,(bld.weight                                                                                                                                    *pos.global_amount)),0) + DECODE(col.is_pos_add_rate,'Y',DECODE(bld.weight,0,0,NVL(addPos.global_amount,0)),0)                                                                                                                                                                                                                                   AS global_pos_amount,
  NVL(doc.local_amount,0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   AS local_pure_doc_amount,
  NVL(doc.global_amount,0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  AS global_pure_doc_amount,
  DECODE(NVL(bld.CUST_TARIFF_CLIENT,''),'', DECODE('N','N',tariff.local_rate, NVL(ext_tariff_toStd.local_rate,0)), DECODE('N','N',custTar.local_rate,0))                                                                                                                                                                                                                                                                                                                                                                                            AS tariff_local_rate,
  DECODE(NVL(bld.CUST_TARIFF_CLIENT,''),'', DECODE('N','N',tariff.global_rate, NVL(ext_tariff_toStd.global_rate,0)), DECODE('N','N',custTar.global_rate,0))                                                                                                                                                                                                                                                                                                                                                                                         AS tariff_global_rate,
  DECODE(pi.proforma_no, NULL, 'N', DECODE(bld.is_paid,'N',DECODE(pi.proforma_no,NULL,'N',DECODE(pi.is_special,'Y','SP','UP')),bld.is_paid))                                                                                                                                                                                                                                                                                                                                                                                                                AS bld_status,
  tariff.remark,
  col.is_std_cargo,
  DECODE(NVL(bld.CUST_TARIFF_CLIENT,''),'',DECODE('N','N',tariff.serv_id
  ||NVL(ext_tariff_toStd.serv_id,''),ext_tariff.serv_id),custTar.serv_id) AS serv_id
FROM manifest m
INNER JOIN vessel_call vsc
ON vsc.vv_cd = m.vv_cd
LEFT JOIN vessel_event ve
ON ve.vv_cd = vsc.vv_cd
INNER JOIN bl
ON bl.pid              = m.pid
AND bl.transfer_status = 'N'
INNER JOIN bl_dtl bld
ON bld.pid              = m.pid
AND bld.is_paid         = 'N'
AND bld.bill_lading_nbr = bl.bill_lading_nbr
LEFT JOIN tally_sheet ts
ON ts.manifest_pid                  = m.pid
AND trim(upper(ts.bill_lading_nbr)) = trim(upper(bl.bill_lading_nbr))
AND ts.cargo_idx_no                 = bld.idx_no
AND trim(upper(ts.cntr_nbr))        = trim(upper(bld.cntr_nbr))
INNER JOIN colli col
ON trim(col.colli_code) = trim(bld.colli_type)
LEFT JOIN
  (SELECT pi.*,
    pid.bill_lading_nbr,
    pid.cntr_no
  FROM proforma_invoice_dtl pid,
    proforma_invoice pi
  WHERE pid.proforma_no        = pi.proforma_no
  AND pi.reversal_proforma    IS NULL
  AND pid.quantity            >= 0
  AND pid.total_global_amount >=0
  AND pid.reversal_proforma   IS NULL
  ) pi
ON pi.bill_lading_nbr = bld.bill_lading_nbr
AND pi.cntr_no        = bld.cntr_nbr
AND pi.vv_cd          = m.vv_cd
LEFT JOIN system_parameter spTax
ON spTax.param_cd = 'SAF_VAT'
LEFT JOIN
  (SELECT NVL(serv.local_amount,0) AS local_rate,
    NVL(serv.global_amount,0)      AS global_rate,
    tar.id,
    tar.is_metric_ton,
    tar.version_nbr,
    tver.eff_start_dttm,
    tar.remark,
    serv.id AS serv_id
  FROM tariffs tar
  INNER JOIN tariff_details tardet
  ON tardet.tariff_id = tar.id
  INNER JOIN services serv
  ON serv.id = tardet.service_id
  INNER JOIN tariff_version tver
  ON tver.version_nbr        = tar.version_nbr
  AND serv.service_type NOT IN ('C','R','L','D','DG')
  AND serv.rec_status        = 'A'
  WHERE tar.rec_status       = 'A'
  ORDER BY tver.eff_start_dttm DESC
  ) tariff ON tariff.id = tariff_end_date(NVL(ve.atb,vsc.vsl_eta_dttm),DECODE(bld.purpose,'TS1W',col.TARIFF_1WAYTRANS_ID,'TS2W',col.TARIFF_2WAYTRANS_ID,col.tariff_dischg_id))
LEFT JOIN
  (SELECT NVL(serv.local_amount,0) AS local_rate,
    NVL(serv.global_amount,0)      AS global_rate,
    tar.id,
    tar.is_metric_ton,
    tar.version_nbr,
    tver.eff_start_dttm,
    serv.id AS serv_id
  FROM tariffs tar
  INNER JOIN tariff_details tardet
  ON tardet.tariff_id = tar.id
  INNER JOIN tariff_version tver
  ON tver.version_nbr = tar.version_nbr
  INNER JOIN services serv
  ON serv.id                 = tardet.service_id
  AND serv.service_type NOT IN ('C','R','L','D','DG')
  AND serv.rec_status        = 'A'
  WHERE tar.rec_status       = 'A'
  ORDER BY tver.eff_start_dttm DESC
  ) ext_tariff_toStd ON ext_tariff_toStd.id = col.tariff_ext_cost_id
LEFT JOIN
  (SELECT NVL(serv.local_amount,0) AS local_rate,
    NVL(serv.global_amount,0)      AS global_rate,
    tar.id,
    tar.is_metric_ton,
    tar.version_nbr,
    tver.eff_start_dttm,
    serv.id AS serv_id,
    tardet.usage
  FROM tariffs tar
  INNER JOIN tariff_details tardet
  ON tardet.tariff_id = tar.id
  INNER JOIN tariff_version tver
  ON tver.version_nbr = tar.version_nbr
  INNER JOIN services serv
  ON serv.id             = tardet.service_id
  AND serv.service_type IN ('C','R','L','DG')
  AND serv.rec_status    = 'A'
  WHERE tar.rec_status   = 'A'
  ORDER BY tver.eff_start_dttm DESC
  ) ext_tariff ON ((ext_tariff.id = col.tariff_ext_cost_id
AND NVL(ext_tariff.usage,'%') LIKE '%'
  ||bld.purpose
  ||'%')
OR ext_tariff.id = tariff_end_date(NVL(ve.atb,vsc.vsl_eta_dttm),DECODE(bld.purpose,'TS1W',col.TARIFF_1WAYTRANS_ID,'TS2W',col.TARIFF_2WAYTRANS_ID,col.tariff_dischg_id)))
LEFT JOIN
  (SELECT custTar.EFFECTIVE_DTTM,
    bld.bill_lading_nbr,
    bld.cntr_nbr,
    DECODE(NVL(DECODE(bld.purpose,'TS2W',custTar.tariff_2waytrans_amt,'TS1W',custTar.tariff_1waytrans_amt,custTar.tariff_disch_amt),0),0,local_rate,DECODE(bld.purpose,'TS2W',custTar.tariff_2waytrans_amt,'TS1W',custTar.tariff_1waytrans_amt,custTar.tariff_disch_amt))  AS local_rate,
    DECODE(NVL(DECODE(bld.purpose,'TS2W',custTar.tariff_2waytrans_amt,'TS1W',custTar.tariff_1waytrans_amt,custTar.tariff_disch_amt),0),0,global_rate,DECODE(bld.purpose,'TS2W',custTar.tariff_2waytrans_amt,'TS1W',custTar.tariff_1waytrans_amt,custTar.tariff_disch_amt)) AS global_rate,
    custTar.id,
    DECODE(bld.purpose,'TS2W',NVL(custTar.tariff_2waytrans_mton,is_metric_ton), 'TS1W',NVL(custTar.tariff_1waytrans_mton,is_metric_ton), NVL(custTar.tariff_disch_mton,is_metric_ton)) AS is_metric_ton,
    version_nbr,
    cust_tariff_val.serv_id
  FROM customized_tariff custTar
  INNER JOIN vessel_call vsc
  ON upper(vsc.vsl_nm) LIKE '%'
    || trim(upper(NVL('EURPCARGO VENEZIA','')))
    || '%'
  AND vsc.in_voy_nbr = trim(upper(NVL('14120502','')))
  OR vsc.vv_cd       = trim(NVL('2014000433',''))
  INNER JOIN manifest m
  ON m.vv_cd = NVL(vsc.vv_cd,'')
  INNER JOIN bl_dtl bld
  ON bld.cust_tariff_client = custTar.client_code
  AND bld.pid               = m.pid
  INNER JOIN customized_tariff_dtl custTarDtl
  ON custTarDtl.cust_tar_id       = custTar.id
  AND trim(custTarDtl.colli_code) = bld.colli_type
  LEFT JOIN
    (SELECT NVL(serv.local_amount,0) AS local_rate,
      NVL(serv.global_amount,0)      AS global_rate,
      tar.id,
      tar.is_metric_ton,
      tar.version_nbr,
      tver.eff_start_dttm,
      serv.id AS serv_id
    FROM tariffs tar
    INNER JOIN tariff_details tardet
    ON tardet.tariff_id = tar.id
    INNER JOIN tariff_version tver
    ON tver.version_nbr = tar.version_nbr
    INNER JOIN services serv
    ON serv.id           = tardet.service_id
    AND serv.rec_status  = 'A'
    WHERE tar.rec_status = 'A'
    ORDER BY tver.eff_start_dttm DESC
    ) cust_tariff_val ON cust_tariff_val.id = DECODE(bld.purpose,'TS2W',custTar.TARIFF_2WAYTRANS_ID,'TS1W',custTar.TARIFF_1WAYTRANS_ID,custTar.TARIFF_DISCH_ID)
  WHERE trim(custTar.client_code)           = bld.cust_tariff_client
  AND custTar.rec_status                    = 'A'
  ORDER BY custTar.effective_dttm DESC,
    custTar.LAST_MODIFY_DTTM DESC
  ) custTar ON TO_CHAR(custTar.EFFECTIVE_DTTM,'yyyymmdd') <= TO_CHAR(NVL(ve.atb,vsc.vsl_eta_dttm),'yyyymmdd')
AND custTar.bill_lading_nbr                                = bld.bill_lading_nbr
AND custTar.cntr_nbr                                       = bld.cntr_nbr
LEFT JOIN
  (SELECT NVL(serv.local_amount,0) AS local_amount,
    NVL(serv.global_amount,0)      AS global_amount,
    tar.id,
    tar.is_metric_ton,
    tar.version_nbr,
    tver.eff_start_dttm
  FROM tariffs tar
  INNER JOIN tariff_details tardet
  ON tardet.tariff_id = tar.id
  INNER JOIN tariff_version tver
  ON tver.version_nbr = tar.version_nbr
  INNER JOIN services serv
  ON serv.id             = tardet.service_id
  AND serv.service_type IN ('D')
  AND serv.rec_status    = 'A'
  WHERE tar.rec_status   = 'A'
  ORDER BY tver.eff_start_dttm DESC
  ) disch ON disch.id = col.tariff_ext_cost_id
LEFT JOIN
  (SELECT rownum AS seq,
    local_amount,
    global_amount,
    last_modify_dttm
  FROM services
  WHERE service_type = 'U'
  AND rec_status     = 'A'
  ORDER BY last_modify_dttm DESC
  ) doc
ON doc.seq = 1
LEFT JOIN
  (SELECT rownum AS seq,
    local_amount,
    global_amount,
    last_modify_dttm
  FROM services
  WHERE service_type = 'L'
  AND rec_status     = 'A'
  ORDER BY last_modify_dttm DESC
  ) heavyLift
ON heavyLift.seq = 1
LEFT JOIN
  (SELECT rownum AS seq,
    local_amount,
    global_amount,
    last_modify_dttm
  FROM services
  WHERE service_type = 'P'
  AND rec_status     = 'A'
  ORDER BY last_modify_dttm DESC
  ) pos
ON pos.seq = 1
LEFT JOIN
  (SELECT rownum AS seq,
    local_amount,
    global_amount,
    last_modify_dttm
  FROM services
  WHERE service_type = 'O'
  AND rec_status     = 'A'
  ORDER BY last_modify_dttm DESC
  ) addPos
ON addPos.seq = 1
LEFT JOIN cntr cn
ON cn.cntr_nbr    = bld.cntr_nbr
AND cn.txn_status = 'A'
WHERE m.pid
  || '='
  || trim(bl.bill_lading_nbr)
  || '='
  || bld.idx_no
  || '='
  || trim(bld.cntr_nbr) LIKE NVL(null,'%')
AND (m.vv_cd = trim(NVL('2014000433',''))
OR ( upper(vsc.vsl_nm) LIKE '%'
  || trim(upper(NVL('EURPCARGO VENEZIA','')))
  || '%'
AND vsc.in_voy_nbr = trim(upper(NVL('14120502',''))) ))
ORDER BY bl.bill_lading_nbr;

有关优化oracle的左连接子查询的任何建议。

0 个答案:

没有答案