ITPub博客

首页 > 数据库 > Oracle > 基于Oracle的sql优化(1)

基于Oracle的sql优化(1)

原创 Oracle 作者:swq618 时间:2017-01-23 10:38:11 0 删除 编辑
先创建表和索引
create table emp_temp as select * from emp;

create index idx_mgr_temp on emp_temp(mgr);

create index idx_deptno_temp on emp_temp(deptno);

执行sql
select * from emp_temp where mgr>100 and deptno >100;


查看优化器模式:
SQL>  show parameter optimizer_mode;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
optimizer_mode                       string      ALL_ROWS
SQL>  select name, value from v$parameter where name='optimizer_mode';   

NAME
--------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------
optimizer_mode
ALL_ROWS

修改
SQL> alter session set optimizer_mode='RULE';  (scott)
SQL> set autotrace traceonly explain;
SQL> select * from emp_temp where mgr>100 and deptno >100;
Execution Plan
----------------------------------------------------------
Plan hash value: 1670750536

-------------------------------------------------------
| Id  | Operation                   | Name            |
-------------------------------------------------------
|   0 | SELECT STATEMENT            |                 |
|*  1 |  TABLE ACCESS BY INDEX ROWID| EMP_TEMP        |
|*  2 |   INDEX RANGE SCAN          | IDX_DEPTNO_TEMP |
-------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("MGR">100)
   2 - access("DEPTNO">100)

Note
-----
   - rule based optimizer used (consider using cbo)

此时如果我们发现走deptno的索引没有mgr的效率高,我们该怎么办?
SQL> select * from emp_temp where mgr>100 and deptno+0>100;

Execution Plan
----------------------------------------------------------
Plan hash value: 2973289657

----------------------------------------------------
| Id  | Operation                   | Name         |
----------------------------------------------------
|   0 | SELECT STATEMENT            |              |
|*  1 |  TABLE ACCESS BY INDEX ROWID| EMP_TEMP     |
|*  2 |   INDEX RANGE SCAN          | IDX_MGR_TEMP |
----------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("DEPTNO"+0>100)
   2 - access("MGR">100)

Note
-----
   - rule based optimizer used (consider using cbo)

SQL>




来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29674916/viewspace-2132832/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-05-24

  • 博文量
    108
  • 访问量
    289809