【问题标题】:Oracle performance issue ExistsOracle 性能问题存在
【发布时间】:2014-02-12 15:02:00
【问题描述】:

我有一个小的学校申请,我面临下一个问题 我有一个关于 Oracle 的查询,它应该获取所有在课堂上有特定学生的老师:

select * from teacher
where exists
 (
   select * from students 
   where teacher.teacher_id = students.teacher_id
     and lower(student_name) like '%john%'
 )

但是我的查询需要很长时间,我尝试使用 IN 甚至 JOIN 但这并没有解决我的问题:

select * from teacher
where teacher.teacher_id in
 (
   select students.teacher_id from students 
   where lower(student_name) like '%john%'
 )

这是使用 JOIN:

select * from teacher
   JOIN 
      (select students.teacher_id from students 
      where lower(student_name) like '%john%') STUD
ON STUD.teacher_id = teacher_id

实际上所有的结果都一样,但我发现使用 EXISTS 是最快的 查询,但仍然需要很多时间-大约 10 分钟-。 我认为可能有一种方法使用 %rowtype 和嵌套表来解决问题...... 我是Oracle的初学者,所以如果有人可以帮助我会很优雅。

【问题讨论】:

  • 数据模型看起来很奇怪。每个学生似乎都有一个老师,除非学生表中每个学生都有多行。如果是这种情况,您需要一个对每个教师都是唯一的表,一个对每个学生都是唯一的表,以及两者之间的交集表。
  • %rowtype 是一个 PL/SQL 构造,与查询性能无关。嵌套表用于在表中存储对象类型,也与您的问题无关。

标签: sql oracle


【解决方案1】:
select distinct * from teacher 
join student on teacher.teacher_id = student.teacher_id 
where student_name ..

学生的teacher_id应该有一个索引

【讨论】:

  • 在下层(学生姓名)上也是如此
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 2018-01-29
相关资源
最近更新 更多