I have a Oracle SQL query like below,
select *
from employees a, department b
where a.empoyee_id = 10
and a.dept_no = b.dept_no
and a.salary between 10000 and 20000
and a.start_date between date1 and date2
and a.end_date between date3 and date4
Above query works fine and fetching results in seconds.
But if convert it to a stored procedure like below,
Procedure GETDATA(
EMP_ID IN NUMBER,
MIN_SAl IN NUMBER,
MAX_SAL IN NUMBER,
MIN_START_DATE IN VARCHAR2,
MAX_START_DATE IN VARCHAR2,
MIN_END_DATE IN VARCHAR2,
MAX_END_DATE IN VARCHAR2,
RESULT OUT dataset
)
IS
BEGIN
open RESULT FOR
select * from employees a, department b
where EMPLOYEE_ID = EMP_ID AND a.dept_no = b.dept_no
and (MIN_SAl IS NULL OR MAX_SAL IS NULL) OR (a.salary between MIN_SAl and MAX_SAL)
and (MIN_START_DATE IS NULL OR MAX_START_DATE IS NULL) OR (a.start_date between MIN_START_DATE and MAX_START_DATE)
and (MIN_END_DATE IS NULL OR MAX_END_DATE IS NULL) OR (a.end_date between MIN_END_DATE and MAX_END_DATE);
END GETDATA;
exec GETDATA(10, NULL, NULL, NULL, NULL, NULL, NULL, :p)
Above stored procedure takes more than 10 seconds but where as running it alone results come within seconds. I see index is added for all the columns in where clause (salary, start_date, end_date)
I see it takes more time in date criterias. I googled and modified the start_date and end_date datatype to DATE from VARCHAR but still no luck. Why it is taking more time in stored procedure but comes within seconds running as standalone?