SQL提高性能(左连接?)

时间:2016-12-07 22:48:13

标签: sql performance alm qc

由于性能问题,以下查询不再有效。我没有几个左连接。

主要问题是我在同一张表中搜索3“MAX”,以便从不同的值中获取最大值。

有优化方法吗?

SELECT B.BG_BUG_ID "Defect ID" ,
       BG_USER_TEMPLATE_16 "Status" ,
       BG_USER_TEMPLATE_12 "Priority"  ,
       BG_USER_TEMPLATE_11 "Severity",
       BG_DETECTION_DATE "Detectado em",
       BG_USER_TEMPLATE_01 "Fase Detecção",
       BG_USER_TEMPLATE_10 "Fornecedor",
       BG_USER_TEMPLATE_18 "Area Responsavel",
       BG_RESPONSIBLE "Assinado para",
       BG_USER_TEMPLATE_09 "Sistema Correção",
       BG_USER_TEMPLATE_17 "Ambiente",
       BG_USER_TEMPLATE_19 "Causa Raiz",
       c.rtt_Time "RTT time",
       BG_CLOSING_DATE "Data de Fechamento",
       BG_USER_17 "Data Alteração Status",
       al_description "Caminho Test Plan",
       BG_SUMMARY  "Descrição Resumida",
       B.Fixed_Time "Fixed time",
       BG_DETECTED_BY   "Detectado por",
       BG_USER_TEMPLATE_15  "Motivo",
       BG_USER_TEMPLATE_04  "Motivo Status Waiting",
       BG_USER_01 "Pendente com",
       A.reopen_Time "lAST REOPEN DATE",
       BG_TARGET_RCYC "Target Cycle",
       R_CYCLE.rcyc_name "Target Cycle",
       REL_NAME "Target Release",
       BG_TARGET_REL "Target Release" ,
       BG_USER_TEMPLATE_08 "Tipo",
       BG_USER_TEMPLATE_07 "Tipo de Solicitação",
       BG_USER_TEMPLATE_06 "Status da Solicitação",
       BG_USER_04 "Contador Fixing",
       BG_USER_26 /*Defect.Reaberturas Validas*/ "Contador Reopened",
       BG_USER_TEMPLATE_20 "CTs Impactados",
       BG_USER_20 "Data Prevista Correção",
       BG_USER_23 "Macro Processo",
       BG_USER_TEMPLATE_03 "Defeito Associado"
FROM BUG B LEFT JOIN  ALL_LISTS AL
ON B.bg_subject = AL.al_item_id
LEFT JOIN (SELECT bg_bug_id, max(au_time) reopen_Time
     from
       bug,audit_log, audit_properties
     where AU_ENTITY_TYPE='BUG'
     and au_entity_id=bg_bug_id
     AND AP_ACTION_ID=au_ACTION_ID
     AND AP_FIELD_NAME='BG_USER_TEMPLATE_16'
     AND AP_NEW_VALUE='Reopened'
     group by bg_bug_id)  A
ON A.BG_BUG_ID=B.BG_BUG_ID
LEFT JOIN (SELECT bg_bug_id, max(au_time) fixed_time
     from
       bug,audit_log, audit_properties
     where AU_ENTITY_TYPE='BUG'
     and au_entity_id=bg_bug_id
     AND AP_ACTION_ID=au_ACTION_ID
     AND AP_FIELD_NAME='BG_USER_TEMPLATE_16'
     AND AP_NEW_VALUE='Ready to Application'
     group by bg_bug_id)  B
ON A.BG_BUG_ID=B.BG_BUG_ID
LEFT JOIN (SELECT bg_bug_id, max(au_time) rtt_time
     from
       bug,audit_log, audit_properties
     where AU_ENTITY_TYPE='BUG'
     and au_entity_id=bg_bug_id
     AND AP_ACTION_ID=au_ACTION_ID
     AND AP_FIELD_NAME='BG_USER_TEMPLATE_16'
     AND AP_NEW_VALUE='Ready to Test'
     group by bg_bug_id)  C
ON A.BG_BUG_ID=B.BG_BUG_ID
left join RELEASE_CYCLES R_CYCLE
on b.BG_TARGET_RCYC = R_CYCLE.rcyc_id
Where   1=1
and BG_TARGET_RCYC= 1085
Order by B.BG_BUG_ID

2 个答案:

答案 0 :(得分:0)

您可以提前尝试将子查询放入临时表吗?然后加入临时表?

答案 1 :(得分:0)

在我看来,您可以将这三个子查询/派生表折叠到下面的单个子查询中。我不想因为没有更新你的连接语法而大喊大叫但没有表别名我不想猜测哪些列对应于什么。

select
    bg_bug_id,
    max(case when AP_NEW_VALUE = 'Reopened' then au_time end) reopen_Time,
    max(case when AP_NEW_VALUE = 'Ready to Application' then au_time end) fixed_Time,
    max(case when AP_NEW_VALUE = 'Ready to Test' then au_time end) rtt_Time,
from bug, audit_log, audit_properties
where
    au_entity_id = bg_bug_id AND AP_ACTION_ID = au_ACTION_ID /* join conditions */
    AND AU_ENTITY_TYPE = 'BUG' AND AP_FIELD_NAME = 'BG_USER_TEMPLATE_16'
    AND AP_NEW_VALUE IN
        ('Reopened', 'Ready to Application', 'Ready to Test') /* not really necessary? */
group by bg_bug_id

这里的技巧是使用case表达式过滤掉每个聚合的适当值。

尽管我刚才说过,但这里是对内部联盟的一种刺痛:

from
    bug b
    inner join audit_log l on l.au_entity = b.bg_bug_id
    inner join audit_properties p on p.ap_action_id = l.au_action_id