以下查询需要将近1分钟才能执行。该表有200万条记录。它可以被重写以更快地执行吗?
SELECT CLIENT_FNM FROM (SELECT DISTINCT TRIM(A.CLIENT_FNM) AS client_fnm
FROM gen_clientvendor_m A JOIN gen_clientvendor_m B ON (TRIM(A.CLIENT_FNM) = TRIM(B.CLIENT_FNM))
WHERE A.CLIENT_VENDOR_ID <> B.CLIENT_VENDOR_ID
AND A.PARTY_TYPE = 'C'
AND A.status = 'A'
ORDER BY TRIM(CLIENT_FNM))
WHERE ROWNUM <= 5
答案 0 :(得分:0)
试试这个:
SELECT a.CLIENT_FNM
FROM (select distinct TRIM(CLIENT_FNM)CLIENT_FNM, CLIENT_VENDOR_ID, PARTY_TYPE, status from gen_clientvendor_m) A
JOIN gen_clientvendor_m B ON A.CLIENT_FNM = TRIM(B.CLIENT_FNM)
WHERE A.CLIENT_VENDOR_ID <> B.CLIENT_VENDOR_ID
AND A.PARTY_TYPE = 'C'
AND A.status = 'A'
ORDER BY TRIM(CLIENT_FNM))
WHERE ROWNUM <= 5
答案 1 :(得分:0)
我会聚合TRIM(client_fnm)
,以便只读一次表。
select client_fnm
from
(
select
trim(client_fnm) as client_fnm,
row_number() over (order by trim(client_fnm)) as rn
from gen_clientvendor_m
group by trim(client_fnm)
having count(distinct client_vendor_id) > 1
and count(case when party_type = 'C' and status = 'A' then 1 end) > 0
)
where rn <= 5;
我用标准SQL的ROWNUM
替换了Oracle的ROW_NUMBER
。 (最好是FETCH FIRST n ROWS ONLY
,但这只能从Oracle 12c开始提供。)
要快速获得此功能,您应该拥有以下功能索引:
create index idx1 on gen_clientvendor_m(trim(client_fnm));
甚至是覆盖指数:
create index idx2 on gen_clientvendor_m(trim(client_fnm), party_type, status, client_vendor_id);
答案 2 :(得分:0)
对于您的查询,我提供以下两个索引:
create index index1 on gen_clientvendor_m(party_type, status, trim(client_fnm), client_vendor_id);
快速找到PARTY_TYPE = 'C' AND status = 'A'
的记录,并为加入提供trim(client_fnm)
和client_vendor_id
。
和
create index index2 on gen_clientvendor_m(trim(client_fnm), client_vendor_id);
快速自我加入这两个领域。