本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
27. 基础表的选择
基础表(driving table)是指被最先访问的表(通常以全表扫描的方式被访问). 根据优化器的不同, sql语句中基础表的选择是不一样的.
如果你使用的是cbo (cost based optimizer),优化器会检查sql语句中的每个表的物理大小,索引的状态,然后选用花费最低的执行路径.
如果你用rbo (rule based optimizer) , 并且所有的连接条件都有索引对应, 在这种情况下, 基础表就是from 子句中列在最后的那个表.
举例:
select a.name , b.manager
from worker a,
lodging b
where a.lodging = b.loding;
由于lodging表的loding列上有一个索引, 而且worker表中没有相比较的索引, worker表将被作为查询中的基础表.
28. 多个平等的索引
当sql语句的执行路径可以使用分布在多个表上的多个索引时, oracle会同时使用多个索引并在运行时对它们的记录进行合并, 检索出仅对全部索引有效的记录.
在oracle选择执行路径时,唯一性索引的等级高于非唯一性索引. 然而这个规则只有
当where子句中索引列和常量比较才有效.如果索引列和其他表的索引类相比较. 这种子句在优化器中的等级是非常低的.
如果不同表中两个想同等级的索引将被引用, from子句中表的顺序将决定哪个会被率先使用. from子句中最后的表的索引将有最高的优先级.
如果相同表中两个想同等级的索引将被引用, where子句中最先被引用的索引将有最高的优先级.
举例:
deptno上有一个非唯一性索引,emp_cat也有一个非唯一性索引.
select ename,
from emp
where dept_no = 20
and emp_cat = ‘a’;
这里,deptno索引将被最先检索,然后同emp_cat索引检索出的记录进行合并. 执行路径如下:
table access by rowid on emp
and-equal
index range scan on dept_idx
index range scan on cat_idx
29. 等式比较和范围比较
当where子句中有索引列, oracle不能合并它们,oracle将用范围比较.
举例:
deptno上有一个非唯一性索引,emp_cat也有一个非唯一性索引.
select ename
from emp
where deptno > 20
and emp_cat = ‘a’;
这里只有emp_cat索引被用到,然后所有的记录将逐条与deptno条件进行比较. 执行路径如下:
table access by rowid on emp
index range scan on cat_idx
30. 不明确的索引等级
当oracle无法判断索引的等级高低差别,优化器将只使用一个索引,它就是在where子句中被列在最前面的.
举例:
deptno上有一个非唯一性索引,emp_cat也有一个非唯一性索引.
select ename
from emp
where deptno > 20
and emp_cat > ‘a’;
这里, oracle只用到了dept_no索引. 执行路径如下:
table access by rowid on emp
index range scan on dept_idx
译者按:
我们来试一下以下这种情况:
sql> select index_name, uniqueness from user_indexes where table_name = 'emp';
index_name uniquenes
------------------------------ ---------
empno unique
emptype nonunique
sql> select * from emp where empno >= 2 and emp_type = 'a' ;
no rows selected
execution plan
----------------------------------------------------------
0 select statement optimizer=choose
1 0 table access (by index rowid) of 'emp'
2 1 index (range scan) of 'emptype' (non-unique)
虽然empno是唯一性索引,但是由于它所做的是范围比较, 等级要比非唯一性索引的等式比较低!