2011-10-06 4 views
1

Oracle에서 오랜 시간이 걸리는 쿼리가 있습니다. 내가 죽이기까지 몇 시간 동안 뛰었 어. 속도를 낼 수있는 방법이 있습니까? forecast_entry 테이블에없는하고 있습니다 random_selection 테이블에서내 쿼리를 완료하는 데 너무 오래 걸린다

의 주요 목적은 randnum, dropper_id 모두를 얻을 수 있으며, ozip3 :

select distinct(random_selection.randnum), 
    random_selection.dropper_id, 
    random_selection.ozip3 
from random_selection 
where random_selection.dropper_id is not null 
and random_selection.quarter = 121 
and (random_selection.dropper_id, random_selection.randnum, random_selection.quarter) in 
    (select forecast_entry.dropper, forecast_entry.rand_num, production_weeks.yyq 
    from forecast_entry, production_weeks 
    where forecast_entry.week = production_weeks.production_week 
    and production_weeks.project_cd = 'EXFC' 
    and production_weeks.yyq >= 121) 

union 

select distinct(random_selection.randnum), 
    dropper_city_brk_2.dropper_id, 
    random_selection.ozip3 
from random_selection, dropper_city_brk_2, dropper 
where random_selection.ozip3 = dropper_city_brk_2.zip3 
and dropper.dropper_id = dropper_city_brk_2.dropper_id 
and dropper.active = 1 
and dropper_city_brk_2.dropper_id <> 10002 
and random_selection.quarter = 121 
and random_selection.dropper_id is null 
and (random_selection.dropper_id, random_selection.randnum, random_selection.quarter) in 
    (select forecast_entry.dropper, forecast_entry.rand_num, production_weeks.yyq 
    from forecast_entry, production_weeks 
    where forecast_entry.week = production_weeks.production_week 
    and production_weeks.project_cd = 'EXFC' 
    and production_weeks.yyq >= 121) 

쿼리 설명 : 여기에

내 쿼리입니다 yyq 121에 있으며 EXFC의 프로젝트 코드가 있습니다. yyq는 week와 production week를 연관시켜 production_weeks 테이블에서 검색됩니다. 일부 dropper_id는 null이므로 ozip3 및 zip3을 연결하여 dropper_city_brk_2 테이블에서 해당 데이터를 가져와야합니다. 우리는 dropper_id가 비활성 인 것을 원하지 않으므로 그들은 active 1을 가져야합니다. 이것은 dropper 테이블을 연결하는 것입니다.

희망이 그렇지 않으면 도움이 아주 단단하다,

+2

질문과 설정 한 색인에 표 스키마를 포함해야합니다. – jadarnel27

+0

이것은 내 인생에서 본 가장 큰 SQL 쿼리입니다. 그것을 읽으려면 작은 휴식을 취해야합니다. –

+4

큰 것을 보았습니다 ...... –

답변

1

난 당신이 요지에 일반 설명 적어도 출력을 제공한다 덧글에 동의를하는 데 도움이됩니다.

Forecast_entry, production_weeks 하위 쿼리를 두 번 실행하고 있습니다. CREATE TEMPORARY TABLE을 사용하여 해당 쿼리를 한 번 계산하면 성능이 향상 될 수 있습니다.

"where X in (subquery)"를 사용하고 있는데, 이는 조인보다 비효율적입니다. 여기에 IN 절을 사용하는 대신 방금 제안한 임시 테이블에서 INNER JOIN을 수행 할 수 있습니다.

중복을 허용 할 수 있거나 중복이 없다는 것을 알고 있으면 UNION ALL으로 전환 할 수 있습니다. UNION ALL은 UNION보다 DB에 대한 작업이 덜 필요합니다.

마지막으로 3 조각으로 분리하여 각각 따로 테스트 할 수 있습니다. 조각은 하위 쿼리와 당신이 결합하고있는 두 개의 쿼리입니다. 그러면 느린 부분을 좁히는 데 도움이 될 수 있습니다.

관련 문제