光标移动大法---落落大神
执行计划
SQL> grant dba to scott;
Grant succeeded.
SQL> conn scott/scott
Connected.
创建测试表test
SQL> create table test as select * fromdba_objects;
Table created.
查看执行计划
SQL> explain plan for select count(*) from test;
Explained.
SQL> select* from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------
Plan hash value: 1950795681
-------------------------------------------------------------------
| Id | Operation | Name |Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0| SELECT STATEMENT | | 1| 160 (2)| 00:00:02 |
| 1| SORT AGGREGATE | | 1 | | |
| 2| TABLE ACCESSFULL| TEST | 53509 | 160 (2)| 00:00:02 |
-------------------------------------------------------------------
Note
-----
- dynamicsampling used for this statement
13 rows selected.
SQL> select sum(bytes/1024/1024) "size_MB" from user_segments where segment_name='TEST';
size_MB
----------
6
我们查出来这个表有6MB,那么扫描的时候大概会扫描6MB
创建索引
SQL> create index idx on test(object_id);
Index created.
SQL> alter table test modify object_id not null;
Table altered.
SQL> explainplan for select count(*) from test;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------
Plan hash value: 2452503352
----------------------------------------------------------------------
| Id | Operation | Name |Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------
| 0| SELECT STATEMENT | | 1 | 29 (4)| 00:00:01 |
| 1| SORT AGGREGATE | | 1 | | |
| 2| INDEX FASTFULL SCAN| IDX | 53509 | 29 (4)| 00:00:01 |
----------------------------------------------------------------------
Note
-----
-dynamic sampling used for this statement
13 rows selected.
做sql优化不要看cost、时间,因为cost是靠数据公式算出来的,不一定准。要看访问路径以及访问的基数。
执行计划——显示当前的访问路径
表JOIN的实验
SQL>explain plan for select * from scott.dept d,scott.emp e where d.deptno=e.deptno and d.deptno=20;
Explained.
SQL> select * from table(dbms_xplan.display);
15:22:08 SYS@test(test)> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
Plan hash value: 568005898
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 290 | 4 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 5 | 290 | 4 (0)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 20 | 1 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL | EMP | 5 | 190 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("D"."DEPTNO"=20)
4 - filter("E"."DEPTNO"=20)
17 rows selected.
15:22:29 SYS@test(test)>
执行顺序:32410
分析:
Id=1 NESTED LOOPS :嵌套循环,显示表与表之间的连接方式,此为父操作
Id=2 TABLE ACCESS BY INDEX ROWID:y与id=4缩进相同,处于同一个级别,但先执行上面的操作。Id=2下面又有一个缩进,意味着3是2的子操作,2是父操作,子操作先执行。
因此执行顺序是32410。
光标移动大法
看<光标移动大发.txt>,就用sql*plus看,不要用阅读器看。
不是缩进越靠右越先执行!
从下往上看:
光标放到42行的‘table’前面,向上移,可以看到正好移动到3‘nested’前面,说明,2是3和42的父操作;3是个嵌套循环,他自己也是个父操作;再把光标放到3的第一个子操作前即4;4也是个父操作,把光标移到4的第一个子操作即5前,5和39平;再移到5的第一个子操作前即6,发现6、19、40平,但40在39后面,不能穿墙(不能穿过39到40),所以40不能算;再放到7,7和18连接;7是父操作,看8和13;8是父操作,看9和10,因为9在10上,所以最先执行9!
从上往下看:
将光标放到0,发现没有与其对齐的;再把光标下移,放到1,发现也没有与其对齐的;光标移到2,没有与其对齐的;移到3,42与其对齐;接下来的步骤参照从下往上看。
看执行计划——找出父与子操作的关系