home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a524 / 28.ddi / doc / explain.doc next >
Encoding:
Text File  |  1991-03-04  |  13.6 KB  |  403 lines

  1. ...
  2. ... $Header: explain.doc,v 6002700.1 89/05/23 00:39:22 rcs Exp $ explain.doc Copyr (c) 1989 Oracle
  3. ...
  4.  
  5.  
  6. NOTE: The following is subject to change and describes an unsupported
  7. feature of Oracle v6.0.25 and above.
  8.  
  9. EXPLAIN Facility
  10.  
  11. MOTIVATION
  12.  
  13.     There is a need for users to be able to determine the steps the system
  14. takes in performing various operations on a database. The EXPLAIN facility
  15. provides users with a convenient mechanism for getting this information.
  16. The facility stores this information in a standard database table that can
  17. be manipulated with standard SQL statements.
  18.  
  19.  
  20.  
  21. SYNTAX 
  22.  
  23.     The following syntax is based on the syntax used by DB2 for their
  24. EXPLAIN facility:
  25.  
  26.     EXPLAIN PLAN [SET STATEMENT_ID [=] <string literal>]
  27.     [INTO <table_name>]
  28.     FOR <sql_statement>
  29.  
  30. where
  31.  
  32.     STATEMENT_ID - a unique optional identifier for the statement;
  33.  
  34.     INTO - allows user to save the results of the analysis in
  35.       the specified table.  The table must conform to the format
  36.       for the table used to store the analysis ( see TABLE FORMATS
  37.       section for a description of the table format ).  If this 
  38.       clause is not specified, the system will then attempt to store
  39.       the information in a table named <user_id>.PLAN_TABLE .  If
  40.       the explicit or implicit table does not exist the EXPLAIN
  41.       command will fail.
  42.           
  43.     <sql statement> - an insert, delete, update, or query statement;
  44.       
  45.  
  46. TABLE FORMATS
  47.  
  48. Core Table Format
  49.  
  50. The core table used to represent the plan information consists of the
  51. following fields:
  52.  
  53. STATEMENT_ID - An identifier associated with the statement.  If not set by
  54.     the user, the identifier will be NULL.  Note that a user may 
  55.     identify a statement by the timestamp field.
  56.  
  57. TIMESTAMP - The date and time when the statement was analyzed.
  58.  
  59. REMARKS - Any comment the user wishes to associate with this step of
  60.       the analysis.
  61.  
  62. OPERATION - the name of the operation being performed.  The following table
  63.     provides a listing of the operations described by the facility.
  64.  
  65.        Operation    Description
  66.     ---------------------------------------------------------------
  67.     And-Equal    A retreival utilizing intersection of
  68.               rowids from index searches
  69.     Connect by    A retrieval that is based on a tree walk
  70.     Concatenation    A retrieval from a group of tables.  It is
  71.             essentially a UNION ALL operation of the
  72.             sources.  Used for OR operations.
  73.     Counting    A node that is used to count the number of 
  74.               rows returned from a table.  Used for queries
  75.               that use the ROWNUM meta-column.
  76.     Filter        A restriction of the rows returned from a table
  77.     First Row    A retrieval of only the first row
  78.     For Update      A retreval that is used for updating
  79.     Index        A retrieval from an index
  80.     Intersection    A retrieval of rows common to two tables
  81.     Merge Join    A join utilizing merge scans
  82.     Minus        A retrieval of rows in Source 1 table but not in
  83.               Source 2 table
  84.     Nested Loops    A join utilizing nested loops.  Each value in the
  85.               first subnode is looked up in the second subnode.
  86.               This is often used when one table in a join is 
  87.               indexed and the other is not.
  88.     Project        A retrieval of a subset of columns from a table
  89.     Remote        A retrieval from a database other than the current
  90.               database
  91.     Sequence    An operation involving a sequence table
  92.     Sort        A retrieval of rows ordered on some column or group
  93.               of columns
  94.     Table        A retrieval from a base table
  95.     Union        A retrieval of unique rows from two tables
  96.     View        A retrieval from a virtual table
  97.     -------------------------------------------------------------------
  98.  
  99. Note that the operation shown when counting the number of rows
  100. returned by a query (i.e. select count(*)) is SORT.  This is due to
  101. the way that COUNT is implemented internally.  The table will not
  102. really be sorted.
  103.  
  104.     
  105. OPTIONS - an option that modifies the operation, e.g., 
  106.     OUTER option on join operations, rationale for sorting, type
  107.     of index scan, type of filter, etc.  The following table
  108.     provides a list of the options for the operations that have
  109.     options.
  110.  
  111.     OPERATION    OPTIONS               DESCRIPTION
  112.         ---------------------------------------------------------------------
  113.         Index        UNIQUE KEY         Unique key lookup on index
  114.                      RANGE              Index range scan
  115.         Merge Join   OUTER              Join is an outer join
  116.         Nested Loops OUTER              Join is an outer join
  117.         Sort         DISTINCT           Sort is to produce distinct values
  118.                      GROUP BY           Sort is for grouping operation
  119.                      JOIN               Sort is for merge join
  120.                      ORDER BY           Sort is for order by
  121.         Table        BY ROWID           Table scan is by rowid
  122.                      FULL               Sequential table scan
  123.                      CLUSTER            Table scan by cluster key
  124.         ---------------------------------------------------------------------
  125.  
  126. OBJECT_NODE - the name of the node that owns the database object.
  127.  
  128. OBJECT_OWNER - the name of the schema the owns the database object.
  129.  
  130. OBJECT_NAME - the name of the database object.
  131.  
  132. OBJECT_TYPE - a modifier that provides descriptive information about
  133.     the database object, e.g., NON-UNIQUE for indexes, etc.
  134.  
  135. OBJECT_INSTANCE - a number corresponding to the ordinal position of the
  136.     object as it appears in the original query.  The numbering proceeds
  137.     from left to right, outer to inner with respect to the original 
  138.     query text.  Note that at this level, view expansion will result in
  139.     rather interesting object instance numbers.  We will be addressing this
  140.     issue fully in future releases.
  141.  
  142.  
  143. SEARCH_COLUMNS - the number of leading columns used when searching an
  144.     index.
  145.  
  146. ID - a number assigned to this operation in the tree.
  147.     Corresponds to a preorder traversal of the row source tree.
  148.  
  149. PARENT_ID - the number assigned to the previous operation that
  150.     recieves information from this operation.  This field combined with the
  151.     ID field allows users to do a treewalk of the specified
  152.     plan with the CONNECT BY statement.
  153.  
  154. POSITION - the position this database object occupies for the previous 
  155.     operation.
  156.  
  157. OTHER - other information that is specific to the row source that a user
  158.     may find useful.  For example, the select statement to a remote node, etc.
  159.  
  160. Sample Table Definition
  161.  
  162.  
  163. create table PLAN_TABLE (
  164.     statement_id     char(30),
  165.     timestamp        date,
  166.     remarks          char(80),
  167.     operation        char(30),
  168.     options            char(30),
  169.     object_node      char(30),
  170.     object_owner     char(30),
  171.     object_name      char(30),
  172.     object_instance numeric,
  173.     object_type     char(30),
  174.     search_columns  numeric,
  175.     id        numeric,
  176.     parent_id    numeric,
  177.     position    numeric
  178.     other           long);
  179.  
  180. An SQL script to create this table resides in file xplainpl.sql in the same
  181. directory containing the file catalog.sql.  This table must reside in the
  182. current schema unless you use the optional INTO clause of the EXPLAIN
  183. command.
  184.  
  185. EXAMPLES
  186.  
  187.     Suppose we issue the following statements:
  188.  
  189.     EXPLAIN PLAN 
  190.     SET STATEMENT_ID = 'query1'
  191.     INTO QUERY_PLANS
  192.     FOR SELECT * FROM T1,T2,T3 WHERE T1.F1 = T2.F1 AND T2.F2 = T3.F2;
  193.  
  194.     SELECT OPERATION, OPTIONS, OBJECT_NAME, ID, 
  195.       PARENT_ID, POSITION
  196.           FROM QUERY_PLANS 
  197.       WHERE STATEMENT_ID = 'query1'
  198.           ORDER BY ID;
  199.  
  200. The following output would be created:
  201.  
  202. OPERATION     OPTIONS  OBJECT_NAME      ID          PARENT_ID     POSITION
  203. --------------------------------------------------------------------------
  204. MERGE JOIN                          1
  205. MERGE JOIN                              2              1           1
  206. SORT          JOIN                      3              2           1
  207. TABLE ACCESS  FULL    T1                4              3           1
  208. SORT          JOIN                      5              2           2
  209. TABLE ACCESS  FULL    T2                6              5           1
  210. SORT          JOIN                      7              1           1
  211. TABLE ACCESS  FULL    T3                8              7           1
  212.  
  213. 8 RECORDS selected
  214.  
  215.  
  216.    Suppose that an index is created on field F1 on table T1 and the
  217. following statements are issued:
  218.  
  219.     EXPLAIN PLAN
  220.     SET STATEMENT_ID = 'query2'
  221.     INTO QUERY_PLANS
  222.     FOR SELECT * FROM T1 WHERE F1 > 1;
  223.  
  224.      
  225.      SELECT OPERATION, OPTIONS, OBJECT_NAME, OBJECT_TYPE, SEARCH_COLUMNS, 
  226.       ID, PREVIOUS_ID
  227.        FROM QUERY_PLANS WHERE
  228.       STATEMENT_ID='query2'
  229.           ORDER BY ID;
  230.  
  231. The following output is produced:
  232.  
  233. OPERATION   OPTIONS  OBJECT_NAME OBJECT_TYPE SEARCH_COLUMNS    ID    POSITION
  234. -----------------------------------------------------------------------------
  235. TABLE SCAN   BY ROWID T1                                       1
  236. INDEX SCAN   RANGE    IT1        NON-UNIQUE     1              2           1
  237.  
  238. 2 RECORDS selected
  239.  
  240.  
  241.     With the same conditions as before, suppose we issue the following
  242. which demonstrates an index only retrieval:
  243.  
  244.     EXPLAIN PLAN
  245.     SET STATEMENT_ID = 'query3'
  246.     INTO QUERY_PLANS
  247.     FOR SELECT F1 FROM T1 WHERE F1 > 1;
  248.  
  249.      SELECT OPERATION, OPTIONS, OBJECT_NAME, OBJECT_TYPE, SEARCH_COLUMNS,
  250.       ID
  251.       FROM QUERY_PLANS WHERE
  252.       STATEMENT_ID='query3';
  253.  
  254. The following output is produced: 
  255.  
  256. OPERATION   OPTIONS  OBJECT_NAME   OBJECT_TYPE  SEARCH_COLUMNS ID
  257. --------------------------------------------------------------------------
  258. INDEX SCAN   RANGE    IT1          NON-UNIQUE        1
  259.  
  260. 1 RECORDS selected
  261.  
  262.     The next example illustrates the output if a grouping operation is
  263. specified in the statement:
  264.  
  265.     EXPLAIN PLAN
  266.     SET STATEMENT_ID = 'query4'
  267.     INTO QUERY_PLANS
  268.     FOR SELECT AVG(F1),F2 FROM T1 GROUP BY F2;
  269.  
  270.     SELECT OPERATION, OPTIONS, OBJECT_NAME, ID,
  271.       PARENT_ID
  272.       FROM QUERY_PLANS WHERE STATEMENT_ID = 'query4'
  273.       ORDER BY ID;
  274.  
  275. OPERATION     OPTIONS    OBJECT_NAME            ID          PARENT_ID
  276. ---------------------------------------------------------------------
  277. SORT          GROUP BY                          1
  278. TABLE SCAN    FULL    T1                   2             1
  279.  
  280. 2 RECORDS selected
  281.  
  282.     The next example illustrates the ouptut if DISTINCT is specified in the
  283. statement:
  284.  
  285.     EXPLAIN PLAN
  286.     SET STATEMENT_ID = 'query5'
  287.     INTO QUERY_PLANS
  288.     FOR SELECT DISTINCT F1 FROM T1;
  289.  
  290.     SELECT OPERATION, OPTIONS, OBJECT_NAME, ID,
  291.       PREVIOUS_ID
  292.       FROM QUERY_PLANS WHERE STATEMENT_ID = 'query5'
  293.       ORDER BY ID;
  294.  
  295. OPERATION     OPTIONS    OBJECT_NAME           ID   PREVIOUS_ID
  296. --------------------------------------------------------------
  297. SORT          DISTINCT                         1
  298. TABLE SCAN    FULL    T1                     2         1
  299.  
  300. 2 RECORDS selected
  301.  
  302.     The next example illustrates the output if a subquery is specified in
  303. the statement:
  304.  
  305.     EXPLAIN PLAN
  306.     SET STATEMENT_ID = 'query6'
  307.     INTO QUERY_PLANS
  308.     FOR SELECT * FROM T1 WHERE F1 < ( SELECT F2 FROM T2 WHERE F1=3);
  309.  
  310.     SELECT OPERATION, OPTIONS, OBJECT_NAME, ID,
  311.       PARENT_ID, POSITION
  312.       FROM QUERY_PLANS WHERE STATEMENT_ID = 'query6'
  313.       ORDER BY ID;
  314.  
  315. OPERATION     OPTIONS  OBJECT_NAME        ID          PARENT_ID  POSITION
  316. -------------------------------------------------------------------------
  317. FILTER          OUT                       1
  318. TABLE SCAN      FULL  T1                  2            1          1
  319. TABLE SCAN      FULL  T2                  3            1          2
  320.  
  321. 3 RECORDS selected
  322.  
  323. The final example displays a complex query whose output is sent to the
  324. default plan table. ( It is assumed that this table has been created before
  325. issuing the statement.)
  326.  
  327.       EXPLAIN PLAN
  328.     SET STATEMENT_ID = 'query7'
  329.     FOR SELECT * FROM T1,T2 WHERE T1.F1 = T2.F1 UNION
  330.         SELECT * FROM T2,T3 WHERE T2.F1 = T3.F1;
  331.  
  332.     SELECT OPERATION, OPTIONS, OBJECT_NAME, ID, PARENT_ID, POSITION
  333.       FROM PLAN_TABLE WHERE STATEMENT_ID = 'query7'
  334.       ORDER BY ID;
  335.  
  336. The following output is produced:
  337.  
  338. OPERATION  OPTIONS     OBJECT_NAME     ID          PARENT_ID      POSITION
  339. --------------------------------------------------------------------------
  340. PROJECTION                             1
  341. UNION                                  2             1              1
  342. SORT        DISTINCT                   3             2              1
  343. NEST LOOP                              4             3              1
  344. TABLE SCAN  BY ROWID   T1              5             4              1
  345. INDEX SCAN  RANGE      IT1             6             5              1
  346. TABLE SCAN  FULL       T2              7             4              2
  347. SORT        DISTINCT                   8             2              2
  348. MERGE JOIN                             9             8              1
  349. SORT        JOIN                       10            9              1
  350. TABLE SCAN  FULL       T2              11            10             1
  351. SORT        JOIN                       12            9              2
  352. TABLE SCAN  FULL       T3              13            12             1
  353.  
  354. 13 RECORDS selected
  355.  
  356. The following example is based on the previous query.  It illustrates the
  357. use of the treewalking capability in Oracle's version of SQL.
  358.  
  359.         SELECT LPAD(' ',2*LEVEL)||OPERATION,OPTIONS, OBJECT_NAME
  360.     FROM PLAN_TABLE WHERE STATEMENT_ID='query7'
  361.     CONNECT BY PRIOR ID = PARENT_ID
  362.            and STATEMENT_ID = 'query7'
  363.      START WITH ID = 1
  364.            and STATEMENT_ID = 'query7'
  365.     ORDER BY ID;
  366.  
  367.  
  368. LPAD(' ',2*LEVEL)||OPERATION  
  369. ------------------------------------------------------------------------------
  370.                  OPTIONS   OBJECT_NAME 
  371. -------------------------------------
  372. PROJECTION                        
  373.  
  374.   UNION                           
  375.  
  376.     SORT
  377.                  DISTINCT
  378.       NEST LOOP                   
  379.  
  380.         TABLE SCAN
  381.                  BY ROWID      T1 
  382.               INDEX SCAN
  383.                  RANGE         IT1 
  384.         TABLE SCAN
  385.              FULL          T2    
  386.     SORT
  387.                  DISTINCT
  388.       MERGE JOIN                     
  389.  
  390.         SORT   
  391.                  JOIN
  392.           TABLE SCAN  
  393.                  FULL           T2  
  394.         SORT
  395.                  JOIN
  396.           TABLE SCAN 
  397.                   FULL          T3  
  398.  
  399. 13 RECORDS selected
  400.  
  401.  
  402.  
  403.