文章目录
1 概述
1. 包括但不限于以下情况
2. 持续补充...
2 性能优化
2.1 优化准则
准则 | 措施 |
---|---|
减少磁盘访问 | 减少数据的访问(合理利用索引) |
仅返回需要的字段 | |
减少网络传输 | 批量处理数据,减少网络 io |
减少开销 | cpu 开销:减少排序、全表查询等 |
内存开销:使用绑定变量 | |
利用资源 | 表分区、并行 |
2.2 执行计划
2.3 sql 执行顺序
3 优化策略
3.1 减少通配符 * 的使用
通配符 * 会额外 '增加开销'
Oracle 会先查询 '数据字典',再转换为对应的列 -- dba_col_comments
3.2 避免不走索引
1. like 最右原则
select * from scott.emp e where e.ename like 'W%'; -- 高效
select * from scott.emp e where e.ename like '%W%'; -- 低
select * from scott.emp e where e.ename like '%W'; -- 低
2. '表达式' 独立成行
select * from scott.emp e WHERE e.mgr >= 700 * 10; -- 高效
select * from scott.emp e WHERE e.mgr/700 >= 10; -- 低
3. 避免在 '非函数索引' 列上使用 '函数' -- 隐式类型转换 同理
select * from scott.emp e where e.empno like '78%'; -- 高效
select * from scott.emp e where substr(e.empno, 1, 2) = '78'; -- 低
3.3 减少对表的查询
-- sql 写法类似,语法仅供参考
-- 情况1: where 条件中
select t1.value
from table1 t1
where t1.col1 = (select t2.col1
from table2 t2
where t2.col = '520')
and t1.col2 = (select t2.col2
from table2 t2
where t2.col = '520'); -- 低
select t1.value
from table1 t1
where (t1.col1, t1.col2) = (select t2.col1, t2.col2
from table2 t2
where t2.col = '520'); -- 高效
-- 情况2:子查询
select (select t2.value1
from table2 t2
where t2.col = t1.col) value1,
(select t2.value2
from table2 t2
where t2.col = t1.col) value2
from table1 t1
where t1.value = '1314'; -- 低
select t2.value1,
t2.value2
from table1 t1,
table2 t2
where t2.col = t1.col
and t1.value = '1314'; -- 高效
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。