Oracle sql 优化详解

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';  -- 高效
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼丸丶粗面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值