1

I want to test the efficiency of this query using explain plan for.

EXPLAIN PLAN FOR
(CREATE VIEW TOPTEAM AS  
    SELECT T1.Team TEAMS, COUNT(*) TIMES 
    FROM TeamsDivision T1
    WHERE (P1.Division = 1 AND
          (2 = (SELECT T2.Division
                FROM TeamsDivision T2
                WHERE (T2.Team = T1.Team AND T2.Season = (T1.Season + 1) )))
    )
    GROUP BY T1.Team     
    ORDER BY TIMES DESC;

SELECT TEAMS, TIMES 
FROM  TOPTEAM
WHERE ROWNUM < 6;

DROP VIEW TOPTEAM);

SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY());

The query works without the explain plan but when I use it I get this errors:

CREATE VIEW TOPTEAM AS
 *
ERROR at line 2:
ORA-00928: missing SELECT keyword


FROM  TOPTEAM
      *
ERROR at line2:
ORA-00942: table or view does not exist

I used explain plan on queries without create view and they worked. I don't know what is causing the error I have tried changing the parenthesis but I get the same errors. The only time it works is when I put explain plan before SELECT TEAMS, TIMES. Thank you!

Jamon
  • 13
  • 2

1 Answers1

3

The documentation of EXPLAIN PLAN says

The EXPLAIN PLAN statement displays execution plans that the optimizer chooses for SELECT, UPDATE, INSERT, and DELETE statements.

Source: SQL Tuning Guide

You can probably fish out the execution plan of the CREATE VIEW statement, but it will be uninteresting. Only the syntax of the query is checked, your access to the tables, and the query will be stored in USER_VIEWS.

I guess what you want to know is the performance of the query itself. You'd do this with

EXPLAIN PLAN FOR
SELECT TEAMS, TIMES 
  FROM TOPTEAM
 WHERE ROWNUM < 6;
wolφi
  • 8,091
  • 2
  • 35
  • 64
  • 1
    I'm happy to hear that, but it's not necessary or convention on stackoverflow. However, you have the option to accept one answer so that other people know it solved your question: https://stackoverflow.com/help/someone-answers – wolφi Apr 10 '21 at 13:16
  • 1
    The SQL Tuning Guide isn't completely accurate here - `EXPLAIN PLAN` can work with *some* DDL statements. For example, in [this answer](https://stackoverflow.com/a/30924156/409172) I use `EXPLAIN PLAN` to generate an estimate for how long it will take to build an index. (But it doesn't work for `CREATE VIEW`, and I agree that probably wouldn't help even if it did work.) – Jon Heller Apr 11 '21 at 01:57