我有两个查询要检索faultCount
和responseCount
,如下所示,它可以正常工作。
select count(*) as faultCount,
COMP_IDENTIFIER
from CORDYS_NCB_LOG
where AUDIT_CONTEXT='FAULT'
group by COMP_IDENTIFIER
order by responseCount;
select count(*) as responseCount,
COMP_IDENTIFIER
from CORDYS_NCB_LOG
where AUDIT_CONTEXT='RESPONSE'
group by COMP_IDENTIFIER
order by responseCount;
我需要加入以获取这样的列:COMP_IDENTIFIER,faultCount,responseCount
。以下查询完成此任务。但是执行需要很长时间(> 16秒)。
select count(case AUDIT_CONTEXT when 'FAULT' then 1 end) as faultCount,
count(case AUDIT_CONTEXT when 'RESPONSE' then 1 end) as responseCount,
COMP_IDENTIFIER
from CORDYS_NCB_LOG
group by COMP_IDENTIFIER
order by responseCount;
我正在寻找一个简单快捷的查询。提前谢谢。
答案 0 :(得分:3)
这需要更长时间的一个可能原因是,即使CORDYS_NCB_LOG
不是AUDIT_CONTEXT
或FAULT
,您也会在RESPONSE
中阅读所有行。只有您感兴趣的行。
您可以将其添加到现有查询的WHERE
子句中:
select count(case AUDIT_CONTEXT when 'FAULT' then 1 end) as faultCount,
count(case AUDIT_CONTEXT when 'RESPONSE' then 1 end) as responseCount,
COMP_IDENTIFIER
from CORDYS_NCB_LOG
where AUDIT_CONTEXT in ('FAULT', 'RESPONSE')
group by COMP_IDENTIFIER
order by responseCount;