I have a small application for schools and I face the next issue I have a query on Oracle that supposed to fetch all teachers that has a specific student in there classes:
select * from teacher
where exists
(
select * from students
where teacher.teacher_id = students.teacher_id
and lower(student_name) like '%john%'
)
But my query is taking a long time, I tried to use IN or even JOIN but that didn't solve my problem:
select * from teacher
where teacher.teacher_id in
(
select students.teacher_id from students
where lower(student_name) like '%john%'
)
and this is using JOIN:
select * from teacher
JOIN
(select students.teacher_id from students
where lower(student_name) like '%john%') STUD
ON STUD.teacher_id = teacher_id
actually all are giving me the same result but I found that using EXISTS was the fastest query, but still taking much time -about 10 min-. I think that there might be a way using %rowtype and nested tables to solve the problem... I am a beginner in Oracle, so if some body can help I will be graceful.
select distinct * from teacher
join student on teacher.teacher_id = student.teacher_id
where student_name ..
and there should be an index on teacher_id of student
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.