I have created aquery that joins several tables in Oracle, but the execution time is very slow, around 1-2 minutes. Is there a method to make the query faster?
SELECT S.ID STAFF_ID,
d.year_enrol,
C.COUNSELLOR_ID,
count(*) Num_Of_Stud
FROM aab_ASSIGNED_COUNSELLOR C,
aab_STAFF S,
v_aab_student d
WHERE C.COUNSELLOR_ID = S.COUNSELLOR_ID
AND SYSDATE BETWEEN C.EFFECTIVE_DATE_FM AND C.EFFECTIVE_DATE_TO
AND C.HOST_COUNSELLOR_IND = 'Y'
AND c.student_num = d.student_num
AND (d.prog_code, d.subcode) NOT IN (('11345',' '),
('22678',' '),
('93451', ' '),
('62378','OPT'))
GROUP BY S.ID,
d.year_enrol,
C.COUNSELLOR_ID;
You should create index for each variable involve in the where
part. You could create simple index or a composite for with all the field.
Create Index
for each fields on the WHERE
not just the Primary Key
aab_ASSIGNED_COUNSELLOR
COUNSELLOR_ID
EFFECTIVE_DATE_FM
HOST_COUNSELLOR_IND
student_num
aab_STAFF
COUNSELLOR_ID
v_aab_student
student_num
prog_code
subcode
Also you can use Explain Plan to see what is doing the query. Sometime even when you add the index the planner decide not use it. That depend on your data size.
ADDED notes.
USE INNER JOIN
instead of WHERE
syntaxis, this wont improve performace just for better reading
And you may try creating an implicit table instead of using IN
. With Left JOIN
and T.prog_code IS NULL
do the trick.
SELECT s.id staff_id, d.year_enrol, c.counsellor_id, Count(*) num_of_stud
FROM aab_assigned_counsellor C
INNER JOIN aab_staff S,
ON c.counsellor_id = s.counsellor_id
INNER JOIN v_aab_student D
ON c.student_num = d.student_num
LEFT JOIN
(
SELECT '11345' prog_code, ' ' subcode
UNION
SELECT '22678' prog_code, ' ' subcode
UNION
SELECT '93451' prog_code, ' ' subcode
UNION
SELECT '62378' prog_code, 'OPT' subcode
) AS T
ON D.prog_code = T.prog_code
AND D.subcode = T.subcode
WHERE C.host_counsellor_ind = 'Y'
AND T.prog_code IS NULL
AND sysdate BETWEEN C.effective_date_fm AND C.effective_date_to
GROUP BY S.id, D.year_enrol, C.counsellor_id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.