Skip to content

Commit 1ce718b

Browse files
committed
sqlplans
1 parent 6284b63 commit 1ce718b

File tree

17 files changed

+1820
-0
lines changed

17 files changed

+1820
-0
lines changed

optimizer/execution_plans/part0/EM_sqlmonitor_output.html

Lines changed: 323 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Some examples for generating SQL execution plans.
2+
3+
The scripts are designed to work in SQL Plus or SQLCL.
4+
5+
Spool files (.lst) are included so you can see the expected results.
6+
7+
To run the examples yourself, use a DBA account and start by running "ctables.sql". Note that this will drop some tables - so use a test database only!
8+
9+
Next, run the examples: eg1.sql ... eg5.sql
10+
11+
An interactive report from Enterprise Manager us also included: EM_sqlmonitor_output.html
12+
13+
### DISCLAIMER
14+
15+
* These scripts are provided for educational purposes only.
16+
* They are NOT supported by Oracle World Wide Technical Support.
17+
* The scripts have been tested and they appear to work as intended.
18+
* You should always run scripts on a test instance.
19+
20+
### WARNING
21+
22+
* These scripts drop and create tables. For use on test databases.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set linesize 220 tab off pagesize 1000 trims on
2+
column plan_table_output format a120
3+
4+
SELECT *
5+
FROM table(DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'LAST ADVANCED'));
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
PROMPT ***********************************************************
3+
PROMPT
4+
PROMPT WARNING!
5+
PROMPT About to drop tables 'roles', 'employees' and 'departments'
6+
PROMPT
7+
PROMPT ***************************************************************************************************************
8+
9+
PAUSE Press <cr> to continue...
10+
11+
drop table roles purge;
12+
drop table employees purge;
13+
drop table departments purge;
14+
15+
create table employees (id number(10) not null, ename varchar2(100) not null, dept_id number(10) not null, role_id number(10) not null, staffno number(10) not null);
16+
create table departments (id number(10) not null, dname varchar2(100) not null);
17+
create table roles (id number(10) not null, rname varchar2(100) not null);
18+
19+
begin
20+
for i in 1..500
21+
loop
22+
insert into employees values (i,'Employee Name '||i, mod(i,10) + 1, mod(i,10), i);
23+
end loop;
24+
for i in 1..10
25+
loop
26+
insert into departments values (i,'Department Name '||i);
27+
insert into roles values (i,'Role Name '||i);
28+
insert into roles values (i+11,'Role Name '||i);
29+
end loop;
30+
commit;
31+
end;
32+
/
33+
34+
exec dbms_stats.gather_table_stats(user,'employees');
35+
exec dbms_stats.gather_table_stats(user,'departments');
36+
exec dbms_stats.gather_table_stats(user,'roles');
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
SQL> @eg1
2+
SQL> column ename format a20
3+
SQL> column rname format a20
4+
SQL>
5+
SQL> select e.ename,r.rname
6+
2 from employees e
7+
3 join roles r on (r.id = e.role_id)
8+
4 join departments d on (d.id = e.dept_id)
9+
5 where e.staffno <= 10
10+
6 and d.dname in ('Department Name 1','Department Name 2');
11+
12+
ENAME RNAME
13+
-------------------- --------------------
14+
Employee Name 1 Role Name 1
15+
16+
SQL>
17+
SQL> @advanced
18+
SQL> set linesize 220 tab off pagesize 1000 trims on
19+
SQL> column plan_table_output format a120
20+
SQL>
21+
SQL> SELECT *
22+
2 FROM table(DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'LAST ADVANCED'));
23+
24+
PLAN_TABLE_OUTPUT
25+
------------------------------------------------------------------------------------------------------------------------
26+
SQL_ID 3aun1ts4t8b80, child number 0
27+
-------------------------------------
28+
select e.ename,r.rname from employees e join roles r on
29+
(r.id = e.role_id) join departments d on (d.id = e.dept_id) where
30+
e.staffno <= 10 and d.dname in ('Department Name 1','Department Name
31+
2')
32+
33+
Plan hash value: 2341252972
34+
35+
-----------------------------------------------------------------------------------
36+
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
37+
-----------------------------------------------------------------------------------
38+
| 0 | SELECT STATEMENT | | | | 6 (100)| |
39+
|* 1 | HASH JOIN | | 3 | 192 | 6 (0)| 00:00:01 |
40+
|* 2 | HASH JOIN | | 3 | 147 | 4 (0)| 00:00:01 |
41+
|* 3 | TABLE ACCESS FULL| DEPARTMENTS | 2 | 42 | 2 (0)| 00:00:01 |
42+
|* 4 | TABLE ACCESS FULL| EMPLOYEES | 10 | 280 | 2 (0)| 00:00:01 |
43+
| 5 | TABLE ACCESS FULL | ROLES | 20 | 300 | 2 (0)| 00:00:01 |
44+
-----------------------------------------------------------------------------------
45+
46+
Query Block Name / Object Alias (identified by operation id):
47+
-------------------------------------------------------------
48+
49+
1 - SEL$9E43CB6E
50+
3 - SEL$9E43CB6E / D@SEL$2
51+
4 - SEL$9E43CB6E / E@SEL$1
52+
5 - SEL$9E43CB6E / R@SEL$1
53+
54+
Outline Data
55+
-------------
56+
57+
/*+
58+
BEGIN_OUTLINE_DATA
59+
IGNORE_OPTIM_EMBEDDED_HINTS
60+
OPTIMIZER_FEATURES_ENABLE('12.2.0.1')
61+
DB_VERSION('12.2.0.1')
62+
ALL_ROWS
63+
OUTLINE_LEAF(@"SEL$9E43CB6E")
64+
MERGE(@"SEL$58A6D7F6" >"SEL$3")
65+
OUTLINE(@"SEL$3")
66+
OUTLINE(@"SEL$58A6D7F6")
67+
MERGE(@"SEL$1" >"SEL$2")
68+
OUTLINE(@"SEL$2")
69+
OUTLINE(@"SEL$1")
70+
FULL(@"SEL$9E43CB6E" "D"@"SEL$2")
71+
FULL(@"SEL$9E43CB6E" "E"@"SEL$1")
72+
FULL(@"SEL$9E43CB6E" "R"@"SEL$1")
73+
LEADING(@"SEL$9E43CB6E" "D"@"SEL$2" "E"@"SEL$1" "R"@"SEL$1")
74+
USE_HASH(@"SEL$9E43CB6E" "E"@"SEL$1")
75+
USE_HASH(@"SEL$9E43CB6E" "R"@"SEL$1")
76+
END_OUTLINE_DATA
77+
*/
78+
79+
Predicate Information (identified by operation id):
80+
---------------------------------------------------
81+
82+
1 - access("R"."ID"="E"."ROLE_ID")
83+
2 - access("D"."ID"="E"."DEPT_ID")
84+
3 - filter(("D"."DNAME"='Department Name 1' OR "D"."DNAME"='Department
85+
Name 2'))
86+
4 - filter("E"."STAFFNO"<=10)
87+
88+
Column Projection Information (identified by operation id):
89+
-----------------------------------------------------------
90+
91+
1 - (#keys=1) "E"."ENAME"[VARCHAR2,100], "R"."RNAME"[VARCHAR2,100]
92+
2 - (#keys=1; rowset=256) "E"."ENAME"[VARCHAR2,100],
93+
"E"."ROLE_ID"[NUMBER,22]
94+
3 - (rowset=256) "D"."ID"[NUMBER,22]
95+
4 - (rowset=256) "E"."ENAME"[VARCHAR2,100], "E"."DEPT_ID"[NUMBER,22],
96+
"E"."ROLE_ID"[NUMBER,22]
97+
5 - (rowset=256) "R"."ID"[NUMBER,22], "R"."RNAME"[VARCHAR2,100]
98+
99+
100+
73 rows selected.
101+
102+
SQL> spool off
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
column ename format a20
2+
column rname format a20
3+
4+
select e.ename,r.rname
5+
from employees e
6+
join roles r on (r.id = e.role_id)
7+
join departments d on (d.id = e.dept_id)
8+
where e.staffno <= 10
9+
and d.dname in ('Department Name 1','Department Name 2');
10+
11+
--
12+
-- Show advanced plan for the SQL statement above
13+
--
14+
@advanced
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
SQL> @eg2
2+
SQL> column ename format a20
3+
SQL> column rname format a20
4+
SQL>
5+
SQL> select /*+ gather_plan_statistics */
6+
2 e.ename,r.rname
7+
3 from employees e
8+
4 join roles r on (r.id = e.role_id)
9+
5 join departments d on (d.id = e.dept_id)
10+
6 where e.staffno <= 10
11+
7 and d.dname in ('Department Name 1','Department Name 2');
12+
13+
ENAME RNAME
14+
-------------------- --------------------
15+
Employee Name 1 Role Name 1
16+
17+
SQL>
18+
SQL> @stats
19+
SQL> set linesize 210 tab off pagesize 1000 trims on
20+
SQL> column plan_table_output format a200
21+
SQL>
22+
SQL> SELECT *
23+
2 FROM table(DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'LAST ALLSTATS ADVANCED'));
24+
25+
PLAN_TABLE_OUTPUT
26+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
27+
SQL_ID 589s24wvxcknf, child number 0
28+
-------------------------------------
29+
select /*+ gather_plan_statistics */ e.ename,r.rname from
30+
employees e join roles r on (r.id = e.role_id) join
31+
departments d on (d.id = e.dept_id) where e.staffno <= 10 and
32+
d.dname in ('Department Name 1','Department Name 2')
33+
34+
Plan hash value: 2341252972
35+
36+
--------------------------------------------------------------------------------------------------------------------------------------------------------
37+
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
38+
--------------------------------------------------------------------------------------------------------------------------------------------------------
39+
| 0 | SELECT STATEMENT | | 1 | | | 6 (100)| | 1 |00:00:00.01 | 28 | | | |
40+
|* 1 | HASH JOIN | | 1 | 3 | 192 | 6 (0)| 00:00:01 | 1 |00:00:00.01 | 28 | 1448K| 1448K| 839K (0)|
41+
|* 2 | HASH JOIN | | 1 | 3 | 147 | 4 (0)| 00:00:01 | 2 |00:00:00.01 | 17 | 2545K| 2545K| 930K (0)|
42+
|* 3 | TABLE ACCESS FULL| DEPARTMENTS | 1 | 2 | 42 | 2 (0)| 00:00:01 | 2 |00:00:00.01 | 7 | | | |
43+
|* 4 | TABLE ACCESS FULL| EMPLOYEES | 1 | 10 | 280 | 2 (0)| 00:00:01 | 10 |00:00:00.01 | 7 | | | |
44+
| 5 | TABLE ACCESS FULL | ROLES | 1 | 20 | 300 | 2 (0)| 00:00:01 | 20 |00:00:00.01 | 8 | | | |
45+
--------------------------------------------------------------------------------------------------------------------------------------------------------
46+
47+
Query Block Name / Object Alias (identified by operation id):
48+
-------------------------------------------------------------
49+
50+
1 - SEL$9E43CB6E
51+
3 - SEL$9E43CB6E / D@SEL$2
52+
4 - SEL$9E43CB6E / E@SEL$1
53+
5 - SEL$9E43CB6E / R@SEL$1
54+
55+
Outline Data
56+
-------------
57+
58+
/*+
59+
BEGIN_OUTLINE_DATA
60+
IGNORE_OPTIM_EMBEDDED_HINTS
61+
OPTIMIZER_FEATURES_ENABLE('12.2.0.1')
62+
DB_VERSION('12.2.0.1')
63+
ALL_ROWS
64+
OUTLINE_LEAF(@"SEL$9E43CB6E")
65+
MERGE(@"SEL$58A6D7F6" >"SEL$3")
66+
OUTLINE(@"SEL$3")
67+
OUTLINE(@"SEL$58A6D7F6")
68+
MERGE(@"SEL$1" >"SEL$2")
69+
OUTLINE(@"SEL$2")
70+
OUTLINE(@"SEL$1")
71+
FULL(@"SEL$9E43CB6E" "D"@"SEL$2")
72+
FULL(@"SEL$9E43CB6E" "E"@"SEL$1")
73+
FULL(@"SEL$9E43CB6E" "R"@"SEL$1")
74+
LEADING(@"SEL$9E43CB6E" "D"@"SEL$2" "E"@"SEL$1" "R"@"SEL$1")
75+
USE_HASH(@"SEL$9E43CB6E" "E"@"SEL$1")
76+
USE_HASH(@"SEL$9E43CB6E" "R"@"SEL$1")
77+
END_OUTLINE_DATA
78+
*/
79+
80+
Predicate Information (identified by operation id):
81+
---------------------------------------------------
82+
83+
1 - access("R"."ID"="E"."ROLE_ID")
84+
2 - access("D"."ID"="E"."DEPT_ID")
85+
3 - filter(("D"."DNAME"='Department Name 1' OR "D"."DNAME"='Department Name 2'))
86+
4 - filter("E"."STAFFNO"<=10)
87+
88+
Column Projection Information (identified by operation id):
89+
-----------------------------------------------------------
90+
91+
1 - (#keys=1) "E"."ENAME"[VARCHAR2,100], "R"."RNAME"[VARCHAR2,100]
92+
2 - (#keys=1; rowset=256) "E"."ENAME"[VARCHAR2,100], "E"."ROLE_ID"[NUMBER,22]
93+
3 - (rowset=256) "D"."ID"[NUMBER,22]
94+
4 - (rowset=256) "E"."ENAME"[VARCHAR2,100], "E"."DEPT_ID"[NUMBER,22], "E"."ROLE_ID"[NUMBER,22]
95+
5 - (rowset=256) "R"."ID"[NUMBER,22], "R"."RNAME"[VARCHAR2,100]
96+
97+
98+
70 rows selected.
99+
100+
SQL> spool off
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
column ename format a20
2+
column rname format a20
3+
4+
select /*+ gather_plan_statistics */
5+
e.ename,r.rname
6+
from employees e
7+
join roles r on (r.id = e.role_id)
8+
join departments d on (d.id = e.dept_id)
9+
where e.staffno <= 10
10+
and d.dname in ('Department Name 1','Department Name 2');
11+
12+
--
13+
-- Show advanced plan with runtime statistics
14+
--
15+
@stats

0 commit comments

Comments
 (0)