Oracle sql调优多个计数和不同

时间:2014-03-18 13:29:06

标签: sql oracle

嘿伙计,我在oracle上运行了以下查询。

 SELECT DISTINCT
 T_TRATAMIENTO.CampaignID AS CAMPAIGNID,
 T_TRATAMIENTO.OfferID AS OFFERID,
 T_CALENDARIO.ActualDate AS ACTUALDATE,
 count(CASE T_TRATAMIENTO.CntrlTreatmtFlag WHEN 0 THEN T_TRATAMIENTO.TreatmentSize END) as NUM_OF_OFFERS,
 count(CASE T_TRATAMIENTO.CntrlTreatmtFlag WHEN 1 THEN T_TRATAMIENTO.TreatmentSize END) as NUM_OF_OFFERS_CG,
 count (distinct (case T_TRATAMIENTO.CntrlTreatmtFlag when 0 then T_TRATAMIENTO.OfferHistoryID END)) as NUM_OFF_VERS,
 count (distinct (case T_TRATAMIENTO.CntrlTreatmtFlag when 1 then T_TRATAMIENTO.OfferHistoryID END)) as NUM_OFF_VERS_CG,
 count(distinct (CASE WHEN T_TRATAMIENTO.CntrlTreatmtFlag = 0 and T_ESTATUSCONTACTO.CountsAsContact=1 THEN T_HISTORIALCONTACTO.CustomerID END)) as UNIQUE_RECIPIENTS,
 count(distinct (CASE T_TRATAMIENTO.CntrlTreatmtFlag WHEN 1 THEN T_HISTORIALCONTACTO.CustomerID END)) as UNIQUE_RECIP_CG FROM
 T_ESTATUSCONTACTO,
 T_CALENDARIO,
 T_TRATAMIENTO
 LEFT OUTER JOIN
 T_HISTORIALCONTACTO ON
 T_TRATAMIENTO.PackageID = T_HISTORIALCONTACTO.PackageID
 WHERE
 T_HISTORIALCONTACTO.CellID = T_TRATAMIENTO.CellID
 AND
 T_HISTORIALCONTACTO.ContactStatusID = T_ESTATUSCONTACTO.ContactStatusID
 AND
 T_HISTORIALCONTACTO.DateID = T_CALENDARIO.DateID
 AND
 T_TRATAMIENTO.HasDetailHistory = 0 GROUP BY
 T_TRATAMIENTO.CampaignID,
 T_TRATAMIENTO.OfferID, T_CALENDARIO.ActualDate;

表T_HISTORIALCONTACTO有8000万条记录,仍然在增长,其他表只有不到100条记录,而且还有时间响应。也进行全面扫描。我已经实现了索引,但仍然表现出缓慢的性能。

如何调整此SQL查询?你会推荐什么?我非常感谢你的帮助。 Thanxs提前

1 个答案:

答案 0 :(得分:1)

首先,您不需要第一个DISTINCT,因为您在前3列(CAMPAIGNID,OFFERID,ACTUALDATE)上有分组。 其次,我建议避免使用非常消耗的“笛卡尔合并连接”。所以尝试这种新的连接方法。 如果仍然发生笛卡尔联接,请尝试在这些表之间进行“连接”:T_ESTATUSCONTACTO,T_CALENDARIO,T_TRATAMIENTO。 目前,您只将连接分隔为T_HISTORIALCONTACTO

SELECT 
 T_TRATAMIENTO.CampaignID AS CAMPAIGNID,
 T_TRATAMIENTO.OfferID AS OFFERID,
 T_CALENDARIO.ActualDate AS ACTUALDATE,
 count(CASE T_TRATAMIENTO.CntrlTreatmtFlag WHEN 0 THEN T_TRATAMIENTO.TreatmentSize END) as NUM_OF_OFFERS,
 count(CASE T_TRATAMIENTO.CntrlTreatmtFlag WHEN 1 THEN T_TRATAMIENTO.TreatmentSize END) as NUM_OF_OFFERS_CG,
 count (distinct (case T_TRATAMIENTO.CntrlTreatmtFlag when 0 then T_TRATAMIENTO.OfferHistoryID END)) as NUM_OFF_VERS,
 count (distinct (case T_TRATAMIENTO.CntrlTreatmtFlag when 1 then T_TRATAMIENTO.OfferHistoryID END)) as NUM_OFF_VERS_CG,
 count(distinct (CASE WHEN T_TRATAMIENTO.CntrlTreatmtFlag = 0 and T_ESTATUSCONTACTO.CountsAsContact=1 THEN T_HISTORIALCONTACTO.CustomerID END)) as UNIQUE_RECIPIENTS,
 count(distinct (CASE T_TRATAMIENTO.CntrlTreatmtFlag WHEN 1 THEN T_HISTORIALCONTACTO.CustomerID END)) as UNIQUE_RECIP_CG FROM
 T_TRATAMIENTO 
 LEFT OUTER JOIN T_HISTORIALCONTACTO ON T_HISTORIALCONTACTO.CellID = T_TRATAMIENTO.CellID
 JOIN T_CALENDARIO  on T_HISTORIALCONTACTO.DateID = T_CALENDARIO.DateID
 JOIN T_ESTATUSCONTACTO on T_HISTORIALCONTACTO.ContactStatusID = T_ESTATUSCONTACTO.ContactStatusID
 WHERE
 T_TRATAMIENTO.HasDetailHistory = 0 
 GROUP BY
 T_TRATAMIENTO.CampaignID,
 T_TRATAMIENTO.OfferID, T_CALENDARIO.ActualDate;