我正在尝试在sql下面运行:但它没有取得结果,运行时间超过5小时。
SELECT TD.TIME_KEY, CUST.SITE_ID, CUST.NODE,
COUNT(CUST.ACCOUNT_NUMBER) AS CUSTOMER_CNT
FROM
TIME_DIM TD INNER JOIN
OSP_ACTIVE_SUB_STAGE CUST ON TIME_KEY BETWEEN CUST.CONNECT_DATE AND (CASE WHEN CUST.CUSTOMER_STATUS_CODE = 'A' THEN SYSDATE ELSE CUST.STATUS_DATE END)
WHERE TD.TIME_KEY >= '01-JAN-14'
GROUP BY TD.TIME_KEY, CUST.SITE_ID, CUST.NODE
OSP_ACTIVE_SUB_STAGE的表结构:
Name Null Type
--------------------- ---- -----------
SITE_ID NUMBER(3)
NODE VARCHAR2(5)
HOUSE_NUMBER NUMBER(10)
HOUSE_RESIDENT_NUMBER NUMBER(10)
ACCOUNT_NUMBER NUMBER(10)
CONNECT_DATE DATE
STATUS_DATE DATE
CUSTOMER_STATUS_CODE VARCHAR2(1)
Time_dim是正常的时间维度表。
如何改善和增加此查询的运行时间或重写此查询?
由于
答案 0 :(得分:1)
这是您的查询:
SELECT TD.TIME_KEY, CUST.SITE_ID, CUST.NODE, COUNT(CUST.ACCOUNT_NUMBER) AS CUSTOMER_CNT
FROM TIME_DIM TD INNER JOIN
OSP_ACTIVE_SUB_STAGE CUST
ON TIME_KEY BETWEEN CUST.CONNECT_DATE AND
(CASE WHEN CUST.CUSTOMER_STATUS_CODE = 'A' THEN SYSDATE ELSE CUST.STATUS_DATE END)
WHERE TD.TIME_KEY >= '01-JAN-14';
GROUP BY TD.TIME_KEY, CUST.SITE_ID, CUST.NODE
Oy公司!那里有很多计算。你需要一个不同的方法。而不是在此期间的每一天加入,创建一个连接和结束的表,然后使用累积总和。
假设所有站点/节点组合每天都有连接或结束,您可以这样做:
select site_id, node, dte, sum(inc) as ActivesOnDate
from ((select oass.site_id, oass.node, oass.connect_date as dte, 1 as inc
from osp_active_sub_stage oass
) union all
(select oass.site_id, oass.node,
(case when oass.customer_status_code = 'A'
then trunc(sysdate)
else oass.status_date + 1
end) , -1 as inc
from osp_active_sub_stage oass
)
)
group by site_id, node, dte;
这应该比原始版本运行得快一点。