从15小时优化到1分钟的SQL

--old.sql  15hours
SELECT * FROM FACULTY_SEARCH_EXPORT_VW VW
            left join table(faculty_salary_log(null)) SAL_LOG_VW 
            on VW.UNVL_ID = SAL_LOG_VW.UNVL_ID 
            left join 
            (
                SELECT  o.salary_total_amt as future_Effect_100_FTE_Salary,
                     o.salary_effective_date as future_effective_from,
                   rec.offer_recipient_unvl_id as future_unvl_id,
                   'recruitment' as future_adjust_type_id,
                   4 as future_adjust_reason_id,
                         o.salary_est_incr_amt as future_adjustment_amt
                FROM offer_salary_cost o 
             INNER JOIN recruitment_offer rec ON o.offer_id=rec.offer_id
                WHERE o.salary_effective_date>current_date
                      AND nvl(o.salary_total_amt, 0) > 0
                UNION ALL
                    --Query for retention promise
                SELECT 
                   o.salary_total_amt as future_Effect_100_FTE_Salary,
                     o.salary_effective_date as future_effective_from,
                   ret.faculty_unvl_id as future_unvl_id,
                   'retention' as future_adjust_type_id,
                   5 as future_adjust_reason_id,
                         o.salary_est_incr_amt as future_adjustment_amt
               FROM offer_salary_cost o 
               INNER JOIN retention_offer ret ON o.offer_id=ret.offer_id
               WHERE o.salary_effective_date>current_date
               AND nvl(o.salary_total_amt, 0) > 0
        ) future
        on vw.unvl_id= future.future_unvl_id
        where 1=1;



--new.sql   1 minutes
SELECT * FROM (SELECT * FROM FACULTY_SEARCH_EXPORT_VW) VW
            left join (SELECT * FROM table(faculty_salary_log(null))) SAL_LOG_VW 
            on VW.UNVL_ID = SAL_LOG_VW.UNVL_ID 
            left join 
            ( SELECT * FROM (
               SELECT  o.salary_total_amt as future_Effect_100_FTE_Salary,
                     o.salary_effective_date as future_effective_from,
                   rec.offer_recipient_unvl_id as future_unvl_id,
                   'recruitment' as future_adjust_type_id,
                   4 as future_adjust_reason_id,
                         o.salary_est_incr_amt as future_adjustment_amt
                FROM offer_salary_cost o 
             INNER JOIN recruitment_offer rec ON o.offer_id=rec.offer_id
                WHERE o.salary_effective_date>current_date
                      AND nvl(o.salary_total_amt, 0) > 0
                UNION ALL
                    --Query for retention promise
                SELECT 
                   o.salary_total_amt as future_Effect_100_FTE_Salary,
                     o.salary_effective_date as future_effective_from,
                   ret.faculty_unvl_id as future_unvl_id,
                   'retention' as future_adjust_type_id,
                   5 as future_adjust_reason_id,
                         o.salary_est_incr_amt as future_adjustment_amt
               FROM offer_salary_cost o 
               INNER JOIN retention_offer ret ON o.offer_id=ret.offer_id
               WHERE o.salary_effective_date>current_date
               AND nvl(o.salary_total_amt, 0) > 0)
        ) future
        on vw.unvl_id= future.future_unvl_id
        where 1=1;



你可能感兴趣的:(JOIN,优化,Date,table,null,search)