1
2
3
|
select
/*+ all_rows */ empno,ename,sal,job
from
emp
where
empno=7396;
|
1
2
3
4
5
|
scott@TEST>show parameter optimizer_mode
NAME
TYPE VALUE
------------------------------------ --------------------------------- ------------------------------
optimizer_mode string ALL_ROWS
|
1
2
3
|
select
/*+ first_rows(10) */ empno,ename,sal,job
from
emp
where
deptno=30;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
scott@TEST>
alter
session
set
optimizer_mode=first_rows_9;
ERROR:
ORA-00096: invalid value FIRST_ROWS_9
for
parameter optimizer_mode, must be
from
among first_rows_1000, first_rows_100, first_rows_10, first_rows_1, first_rows, all_rows, choose,
rule
scott@TEST>
set
autotrace traceonly
scott@TEST>
select
/*+ first_rows(9) */ empno
from
emp;
14
rows
selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 179099197
---------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
---------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 9 | 36 | 1 (0)| 00:00:01 |
| 1 |
INDEX
FULL
SCAN | PK_EMP | 9 | 36 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------
|
集合運算(如UNION,INTERSACT,MINUS,UNION ALL等)
GROUP BY
FOR UPDATE
聚合函數(好比SUM等)
DISTINCT
ORDER BY(對應的排序列上沒有索引)
1
2
3
|
select
/*+
rule
*/ empno,ename,sal,job
from
emp
where
deptno=30;
|
目標SQL除RULE以外還聯合使用了其餘Hint(如DRIVING_SITE)。
目標SQL使用了並行執行
目標SQL所涉及的對象有IOT
目標SQL所涉及的對象有分區表
1
2
3
|
select
/*+
full
(emp) */ empno,ename,sal,job
from
emp
where
deptno=30;
|
1
2
3
|
select
/*+ rowid(emp) */ empno,ename,sal,job
from
emp
where
rowid=
'AAAR3xAAEAAAACXAAA'
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
select
/*+
index
(emp pk_emp) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
select
/*+
index
(emp pk_emp idx_emp_mgr idx_emp_dept) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
select
/*+
index
(emp (empno) (mgr) (deptno)) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
select
/*+
index
*/ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
|
1
2
3
4
5
6
7
8
9
10
11
|
select
/*+ no_index(emp pk_emp) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
select
/*+ no_index(emp pk_emp idx_emp_mgr idx_emp_dept) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
select
/*+ no_index */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
|
1
2
3
4
5
6
7
8
9
10
11
|
select
/*+ index_desc(emp pk_emp) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
select
/*+ index_desc(emp pk_emp idx_emp_mgr idx_emp_dept) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
select
/*+ index_desc */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
scott@TEST>
select
/*+ index_desc(emp,pk_emp) */ empno
from
emp;
EMPNO
----------
7934
7902
7900
7876
7844
7839
7788
7782
7698
7654
7566
7521
7499
7369
14
rows
selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1838043032
-------------------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
-------------------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 10 | 40 | 1 (0)| 00:00:01 |
| 1 |
INDEX
FULL
SCAN DESCENDING| PK_EMP | 10 | 40 | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
......
|
1
2
3
4
5
6
7
8
9
10
11
|
select
/*+ index_combine(emp pk_emp idx_emp_mgr) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902 ;
select
/*+ index_combine(emp pk_emp idx_emp_mgr idx_emp_deptno) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
select
/*+ index_combine(emp) */ empno,ename,sal,job
from
emp
where
empno=7369
and
mgr=7902
and
deptno=20;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
scott@TEST>
create
index
idx_emp_mgr
on
emp(mgr);
Index
created.
scott@TEST>
create
index
idx_emp_dept
on
emp(deptno);
Index
created.
scott@TEST>
select
/*+ index_combine(emp pk_emp idx_emp_mgr idx_emp_deptno) */ empno,ename,sal,job
2
from
emp
3
where
empno=7369
and
mgr=7902
and
deptno=20;
EMPNO ENAME SAL JOB
---------- ------------------------------ ---------- ---------------------------
7369 SMITH 800 CLERK
Execution Plan
----------------------------------------------------------
Plan hash value: 1816402415
-------------------------------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
-------------------------------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 1 | 29 | 2 (0)| 00:00:01 |
| 1 |
TABLE
ACCESS
BY
INDEX
ROWID | EMP | 1 | 29 | 2 (0)| 00:00:01 |
| 2 | BITMAP CONVERSION
TO
ROWIDS | | | | | |
| 3 | BITMAP
AND
| | | | | |
| 4 | BITMAP CONVERSION
FROM
ROWIDS| | | | | |
|* 5 |
INDEX
RANGE SCAN | PK_EMP | | | 0 (0)| 00:00:01 |
| 6 | BITMAP CONVERSION
FROM
ROWIDS| | | | | |
|* 7 |
INDEX
RANGE SCAN | IDX_EMP_MGR | | | 1 (0)| 00:00:01 |
| 8 | BITMAP CONVERSION
FROM
ROWIDS| | | | | |
|* 9 |
INDEX
RANGE SCAN | IDX_EMP_DEPT | | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------
......
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
scott@TEST>
alter
session
set
"_b_tree_bitmap_plans"
=
false
;
Session altered.
scott@TEST>
select
/*+ index_combine(emp pk_emp idx_emp_mgr idx_emp_deptno) */ empno,ename,sal,job
2
from
emp
3
where
empno=7369
and
mgr=7902
and
deptno=20;
EMPNO ENAME SAL JOB
---------- ------------------------------ ---------- ---------------------------
7369 SMITH 800 CLERK
Execution Plan
----------------------------------------------------------
Plan hash value: 2949544139
--------------------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
--------------------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 1 | 29 | 1 (0)| 00:00:01 |
|* 1 |
TABLE
ACCESS
BY
INDEX
ROWID| EMP | 1 | 29 | 1 (0)| 00:00:01 |
|* 2 |
INDEX
UNIQUE
SCAN | PK_EMP | 1 | | 0 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
......
|
1
2
3
4
5
6
7
8
9
10
11
|
select
/*+ index_ffs(emp pk_emp) */ empno
from
emp;
select
/*+ index_ffs(emp idx_emp_1 idx_emp_2) */ empno
from
emp
where
mgr=7902
and
deptno=20;
--create index idx_emp_1 on emp(mgr,deptno,1);
--create index idx_emp_2 on emp(mgr,deptno,2);
select
/*+ index_ffs(emp) */ empno
from
emp;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
scott@TEST>
select
empno
from
emp;
14
rows
selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 179099197
---------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
---------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 10 | 40 | 1 (0)| 00:00:01 |
| 1 |
INDEX
FULL
SCAN | PK_EMP | 10 | 40 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------
......
scott@TEST>
select
/*+ index_ffs(emp) */empno
from
emp;
14
rows
selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 366039554
-------------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
-------------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 10 | 40 | 2 (0)| 00:00:01 |
| 1 |
INDEX
FAST
FULL
SCAN| PK_EMP | 10 | 40 | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------
......
|
1
2
3
4
5
6
7
|
select
/*+ index_join(emp pk_emp idx_emp_mgr) */ empno,mgr
from
emp
where
empno>7369
and
mgr<7902;
select
/*+ index_join(emp) */ empno,mgr
from
emp
where
empno>7369
and
mgr<7902;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
scott@TEST>
select
empno,mgr
2
from
emp
3
where
empno>7369
and
mgr<7902;
12
rows
selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2059184959
-------------------------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
-------------------------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 10 | 80 | 2 (0)| 00:00:01 |
|* 1 |
TABLE
ACCESS
BY
INDEX
ROWID| EMP | 10 | 80 | 2 (0)| 00:00:01 |
|* 2 |
INDEX
RANGE SCAN | IDX_EMP_MGR | 11 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
......
scott@TEST>
select
/*+ index_join(emp) */ empno,mgr
2
from
emp
3
where
empno>7369
and
mgr<7902;
12
rows
selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3030719951
---------------------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
---------------------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 10 | 80 | 3 (34)| 00:00:01 |
|* 1 |
VIEW
|
index
$_join$_001 | 10 | 80 | 3 (34)| 00:00:01 |
|* 2 | HASH
JOIN
| | | | | |
|* 3 |
INDEX
RANGE SCAN| IDX_EMP_MGR | 10 | 80 | 2 (50)| 00:00:01 |
|* 4 |
INDEX
RANGE SCAN| PK_EMP | 10 | 80 | 2 (50)| 00:00:01 |
---------------------------------------------------------------------------------------
......
|
1
2
3
|
select
/*+ and_equal(emp idx_emp_mgr idx_emp_dept) */ empno,mgr
from
emp
where
deptno=20
and
mgr=7902;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
scott@TEST>
select
empno,mgr
2
from
emp
3
where
deptno=20
and
mgr=7902;
Execution Plan
----------------------------------------------------------
Plan hash value: 2059184959
-------------------------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
-------------------------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 1 | 11 | 2 (0)| 00:00:01 |
|* 1 |
TABLE
ACCESS
BY
INDEX
ROWID| EMP | 1 | 11 | 2 (0)| 00:00:01 |
|* 2 |
INDEX
RANGE SCAN | IDX_EMP_MGR | 2 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
......
scott@TEST>
select
/*+ and_equal(emp idx_emp_mgr idx_emp_dept) */ empno,mgr
2
from
emp
3
where
deptno=20
and
mgr=7902;
Execution Plan
----------------------------------------------------------
Plan hash value: 3295440569
--------------------------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
--------------------------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 1 | 11 | 3 (0)| 00:00:01 |
|* 1 |
TABLE
ACCESS
BY
INDEX
ROWID| EMP | 1 | 11 | 3 (0)| 00:00:01 |
| 2 |
AND
-EQUAL | | | | | |
|* 3 |
INDEX
RANGE SCAN | IDX_EMP_MGR | 2 | | 1 (0)| 00:00:01 |
|* 4 |
INDEX
RANGE SCAN | IDX_EMP_DEPT | 5 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
......
|
1
2
3
4
5
6
|
select
/*+ ordered */ e.ename,j.job,e.sal,d.deptno
from
emp e,jobs j,dept d
where
e.empno=j.empno
and
e.deptno=d.deptno
and
d.loc=
'CHICAGO'
order
by
e.ename;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
scott@TEST>
select
e.ename,j.job,e.sal,d.deptno
2
from
emp e,jobs j,dept d
3
where
e.empno=j.empno
4
and
e.deptno=d.deptno
5
and
d.loc=
'CHICAGO'
6
order
by
e.ename;
6
rows
selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 4113290228
-----------------------------------------------------------------------------------------------
| Id | Operation |
Name
|
Rows
| Bytes | Cost (%CPU)|
Time
|
-----------------------------------------------------------------------------------------------
| 0 |
SELECT
STATEMENT | | 5 | 235 | 9 (23)| 00:00:01 |
|