oracle进阶(五)执行计划

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/x18094/article/details/89532913

oracle进阶(五)执行计划 

主要内容:认识优化器,认识执行计划,以及如何优化执行计划。

create table test1(
a_id number(5),
name varchar2(20)
);
create index idx_test1 on test1(a_id);
create table test2(
b_id number(5),
name varchar2(20)
);
create index idx_test2 on test2(b_id);

优化器

执行sql时,优化器会根据sql语句生成执行计划,包括选择驱动表,执行顺序,用不用索引。优秀的sql语句能够使优化器生成好的执行计划。

如何查看执行计划

1.explain plan命令的语法是依次执行如下两条命令:

explain plan for +     目标SQL
select * from     table(dbms_xplan.display)


2.plsql f5 也就是对explain进行封装

初识执行计划

select * 全表扫描,不会用上索引。

select * from test1;

查询字段和查询条件都用到索引:select a_id from test1 a where a.a_id=1


--全表扫描
select b.* from test2 b,test1 a where a.a_id=b.b_id and a.name='';


--用上b表的索引
select a.* from test2 b,test1 a where a.a_id=b.b_id and a.name='';


--优化后,用上a表的索引
select b.* from test2 b,test1 a where a.a_id=b.b_id and b.name='';

执行计划优化

4.执行计划优化原则:合理选择驱动表,利用好索引。
具体实施:
尽量不用select *
尽量用上索引
尽量使用带有条件的表作为驱动表,以保证全表扫描时能带上条件,得到较小的row source。
尽量使用组合索引的中引导索引,而非组合索引中的非引导索引

猜你喜欢

转载自blog.csdn.net/x18094/article/details/89532913