[英]Query optimization of oracle query
我对查询优化的了解不多,我需要一些帮助。 这是我们项目的示例代码,它是长期运行的 oracle 查询,用于从 2 个不同的表(student、student_info)中提取数据。
有没有可能优化这个? where
子句“and”操作在这里如何工作?
它在执行 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;
你可以:
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);
首先,使用正确、明确、正确的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)
上的索引开始。
只是一些建议。
你已经有 a.student_id=b.student_id 所以 ('a123,'b123','c123') 中的条件 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 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.