home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / po7_win / db / rdbms71 / explain.doc < prev    next >
Text File  |  1995-01-06  |  14KB  |  401 lines

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