- Database Schemas
CREATE TABLE actor
(id INTEGER NOT NULL, name VARCHAR(35), PRIMARY KEY (id));
CREATE INDEX actor_name ON actor(name);
CREATE TABLE movie
(id INTEGER NOT NULL, title VARCHAR(70)
,yr DECIMAL(4), score FLOAT, votes INTEGER, director INTEGER
,PRIMARY KEY (id), FOREIGN KEY (director) REFERENCES actor(id)
);
CREATE INDEX movie_title ON movie(title);
CREATE INDEX movie_votes ON movie(votes);
CREATE TABLE casting
(movieid INTEGER NOT NULL, actorid INTEGER NOT NULL
,ord INTEGER, PRIMARY KEY (movieid, actorid)
,OREIGN KEY (movieid) REFERENCES movie(id)
, FOREIGN KEY (actorid) REFERENCES actor(id)
);
CREATE INDEX casting_movie ON casting(movieid);
CREATE INDEX casting_actor ON casting(actorid);
CREATE INDEX casting_ord ON casting(ord);
NOTE: TA account(S1G30) has these three tables with data.
Since these tables are quite large, you should NOT copy those in your own accounts.
You can refer to these tables either using prefixing owner account name or using synonyms. For example,
SELECT * FROM S1G30.movie;
or
CREATE SYNONYM movie FOR S1G30.movie;
SELECT * FROM movie;
TA executed the following ANALYZE statements so that Oracle uses cost-based optimization.
ANALYZE TABLE movie COMPUTE STATISTICS;
ANALYZE TABLE actor COMPUTE STATISTICS;
ANALYZE TABLE casting COMPUTE STATISTICS;
Examine indices info related to above tables.
(Use INDEX_NAME, TABLE_NAME, LEAF_BLOCKS,AVG_LEAF_BLOCKS_PER_KEY,
AVG_DATA_BLOCKS_PER_KEY, NUM_ROWS, DISTINCT_KEYS) columns in
ALL_INDEXES table.)
- Setup for using EXPLAIN PLAN.
The EXPLAIN PLAN statement displays execution plans chosen by the Oracle optimizer for SELECT, UPDATE, INSERT, and DELETE statements. A statement's execution plan is the sequence of operations Oracle performs to run the statement.
- Creating the PLAN_TABLE Output Table
ORACLE stores the execution plans of a given SQL into a table called PLAN_TABLE. Execute the following SQL script for it.
SQL> @$ORACLE_HOME/rdbms/admin/utlxplan
- Running EXPLAIN PLAN
The following SQL code template is used to populate PLAN_TABLE with
execution plan for a SQL statement. You need to edit the code template
to put specific value for <some-name> before running it.
EXPLAIN PLAN
SET STATEMENT_ID = '<some-name>'
FOR
<select statement to be analyzed>;
NOTE: PLAN_TABLE allows duplicate values in statement_id column.
This may result in spurious data for execution plans for queries
using an existing value of statement_id. You may consider
cleaning up old information using DELETE FROM PLAN_TABLE; before
asking for execution plan for a new query.
- Displaying PLAN_TABLE Output
Use the following SQL to view created execution plans
SELECT LPAD(' ', 2*LEVEL)||OPERATION||' '||OPTIONS||' '||OBJECT_NAME Query_Plan
FROM PLAN_TABLE
CONNECT BY PRIOR ID = PARENT_ID and STATEMENT_ID = '<some-name>'
START WITH ID=1 and STATEMENT_ID = '<some-name>'
ORDER BY ID;
or
SELECT id, parent_id, level, position, operation, options, object_name, cost, bytes
FROM PLAN_TABLE
CONNECT BY PRIOR ID = PARENT_ID and STATEMENT_ID = '<some-name>'
START WITH ID=1 and STATEMENT_ID = '<some-name>'
ORDER BY ID;
- NOTES:
1) You may save these statement via "SAVE <some-name>" command,
recall it via "@<some-name>" and edit it via "EDIT <some-name>".
2) You may use "spool <some-name>" command to start recording
a session and "spool off" to stop recording.
3) Additional Information on Explain Plan is available from
- Pages 114-116 (2nd Ed.) in the supplemantary book
- Range query processing stratagies are discussed in the text book ch 15.
- Oracle tutorials
from MIT (tuning, dwh, java) ... web series