Nesting a Query to Improve Performance
BEGIN
-- Inefficient, calls function for every row
FOR item IN (SELECT DISTINCT (Sqrt(department_id)) col_alias
FROM employees)
LOOP
dbms_output.Put_line(item.col_alias);
END LOOP;
-- Efficient, only calls function once for each distinct value.
FOR item IN (SELECT Sqrt(department_id) col_alias
FROM (SELECT DISTINCT department_id
FROM employees))
LOOP
dbms_output.Put_line(item.col_alias);
END LOOP;
END;
/
OUTPUT:-
7.07106781186547524400844362104849039285
9.48683298050513799599668063329815560116
8.36660026534075547978172025785187489393
10.48808848170151546991453513679937598475
3.16227766016837933199889354443271853372
6.32455532033675866399778708886543706744
5.47722557505166113456969782800802133953
4.47213595499957939281834733746255247088
7.74596669241483377035853079956479922167
8.94427190999915878563669467492510494176
10
10
5.47722557505166113456969782800802133953
4.47213595499957939281834733746255247088
8.36660026534075547978172025785187489393
9.48683298050513799599668063329815560116
10.48808848170151546991453513679937598475
7.07106781186547524400844362104849039285
6.32455532033675866399778708886543706744
8.94427190999915878563669467492510494176
3.16227766016837933199889354443271853372
7.74596669241483377035853079956479922167
PL/SQL procedure successfully completed.
0 comments:
Post a Comment