我对查询优化的知识不多,在这里我需要一些帮助。这是我们项目的示例代码,它是长期运行的oracle查询,用于从2个不同的表(student,student_info)中提取数据。
是否有可能对此进行优化? where
子句“和”操作在这里如何工作?
在执行AND子句时,它保持任何顺序吗?下面的查询如何通过删除行b.student_id in ('a123','b123','c123')
的代码来使前后有所不同。
我们没有权限在该表列上添加索引。
如何在不创建索引的情况下提高性能。
select a.student_id
max(decode(a.marks_limit, 99.99,100,null )) as max_marks,
b.student_city_code "NYC",
from student a,
student_info b
where a.student_id=b.student_id
and a.student_id in ('a123','b123','c123')
and b.student_id in ('a123','b123','c123')
and b.adress_modified > TO_TIMESTAMP('2003/12/13 10:13:18', 'YYYY/MM/DD HH:MI:SS')
group by a.student_id, b.student_city_code;
答案 0 :(得分:0)
您可以:
select a.student_id
max(decode(a.marks_limit, 99.99,100,null )) as max_marks,
b.student_city_code "NYC",
from student a
join student_info b
on a.student_id=b.student_id -- explicit join
where a.student_id in ('a123','b123','c123') -- removing duplicate condition
and b.adress_modified>TO_TIMESTAMP('2003/12/13 10:13:18','YYYY/MM/DD HH:MI:SS')
group by a.student_id, b.student_city_code;
并添加索引:
CREATE INDEX id1 ON student(student_id);
CREATE INDEX id2 ON student_info(student_id);
CREATE INDEX id3 ON student_info(adress_modified);
答案 1 :(得分:0)
首先,使用具有合理的表别名和JOIN
表达式的正确,明确,正确 case
语法编写查询。
修复后,我会期望像这样:
select s.student_id
max(s.marks_limit) as max_marks, -- I have no idea what the decode() is supposed to be doing
si.student_city_code
from student s join
student_info si
on s.student_id = si.student_id
where s.student_id in ('a123', 'b123', 'c123')
and si.adress_modified > TIMESTAMP '2003-12-13T10:13:18'HH:MI:SS')
group by s.student_id, si.student_city_code;
我将从student(student_id)
和student_info(student_id address_modified, student_city_code)
的索引开始。
答案 2 :(得分:0)
只是一些建议。
您已经拥有a.student_id = b.student_id,因此('a123,'b123','c123')中的condizione b.student_id只是一个有用的重复 您还应该基于where子句
使用显式联接符号,而不是旧的隐式符号 select a.student_id
max(decode(a.marks_limit, 99.99,100,null )) as max_marks,
b.student_city_code "NYC",
from student a
INNER JOIN student_info b ON a.student_id= b.student_id
WHERE
and a.student_id in ('a123','b123','c123')
and b.adress_modified > TO_TIMESTAMP('2003/12/13 10:13:18', 'YYYY/MM/DD HH:MI:SS')
group by a.student_id, b.student_city_code
为了获得更好的性能,您应该检查表上的复合索引
student_info ( adress_modified, student_id )
或
student_info ( adress_modified, student_id, student_city_code )