oracle表连接的三种关联机制
a.嵌套循环:NL。用一张表的每一条数据取和另一张表的每条数据进行关联
b.哈希连接:hash。在内存中完成运算,匹配的是两张表共同的哈希值
c.排序合并:sort merge。两张表进行排序,然后用一张表的每一条数据取和另一张表的每条数据进行关联
你们一般用哪种关联机制
在表连接的时候,oracle中的hints优化器会自动把三种关联机制都试一遍,选择一种最优的机制给到我们,
所以我们一般选择默认的关联机制
以下连接,oracle默认用的是sort merge:cost=6 (hints优化器)
select *
from emp e
join dept d
on e.deptno = d.deptno
强行使用嵌套循环NL:cost=10(hints优化器)
select /*+ USE_NL(e,d)*/ *
from emp e
join dept d
on e.deptno = d.deptno
强行使用哈希连接hash:cost=7(hints优化器)
select /*+ USE_HASH(e,d)*/ *
from emp e
join dept d
on e.deptno = d.deptno
强行使用排序连接MERGE(hints优化器)
select /*+ USE_MERGE(e,d)*/ *
from emp_range_list e
join dept d
on e.deptno = d.deptno
并行执行parallel(抢夺资源)。一般用:4,8,16(hints优化器)
select /*+ parallel(16)*/ *
from emp_range_list e
join dept d
on e.deptno = d.deptno
优选选择扫描哪种表leading (oracle默认是扫描小表) (hints优化器)
select /*+ leading(e,d)*/ *
from emp e
join dept d
on e.deptno = d.deptno
强制使用指定的索引 : /+ index(表名,索引名)/
给emp1表的deptno,sal创建组合和位图索引,在查询的时候,会使用哪个索引呢
create index pk_emp11 on emp1(deptno, sal); --组合索引
create bitmap index pk_emp12 on emp1(deptno); --位图索引
select * from emp1 where deptno = 10; --现在走的是位图索引,cost=1
select /*+ index(e,pk_emp11)*/ * from emp1 e where deptno = 10; --现在走的是组合索引,cost=2


736

被折叠的 条评论
为什么被折叠?



