驱动表

ASKTOM

https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:192812348072


The 'driving' table is the table we will join FROM -- that is JOIN TO other tables.  For 
example, lets say you have the query:



select * from emp, dept where emp.deptno = dept.deptno;

In this case the driving table might be DEPT, we would fetch rows from DEPT in a full 
scan and then find the rows in EMP that match.  DEPT is the driving table.

The choice of a driving table made using many factors.  For example, in the above query 
if there was an index on DEPT(DEPTNO) but not EMP(DEPTNO), we would probably use EMP as 
the driving table -- we would fetch rows from EMP and then using the DEPT(DEPTNO) index 
-- find their matches in the DEPT table.  Table sizes, cardinality of column values, and 
other things can affect the choice of a driving table (eg: HINTS).

When using the Rule Based Optimizer -- the placement of tables in the FROM clause is 
relevant.  We process the from clause from the RIGHT to the LEFT -- we would tend to pick 
a driving table from the end of the FROM list.  There is a hint in the Cost Based 
Optimizer to have this happen as well.

When using CBO -- the order of tables is not relevant (unless you hint it to be).  We use 
the statistics and data dictionary to determine which table is best to be used as the 
driving table.



The Driving Table Location Problem
Oracle Tips by Burleson
 from: http://www.praetoriate.com/t_op_sql_driving_table.htm

It is a real problem in Oracle SQL that the driving table isreversed(颠倒) between the rule-based optimizer(最优化) and the cost-based optimizer. In the RBO, the driving table is the lasttable in the from clause(条款), while in the CBO the driving table is always determined by the CBO. In cases where theordered hint(暗示) is specified(规定的), Oracle will use the driving table as the first table in the from clause(条款). As you will recall(召回), the ordered hint(暗示) is very useful for reducing the parse(解析) time of large n-way table joins byspecifying(指定) the table join order.

This makes it very challenging for the SQL tuning(调整)professional who adds hints to SQL statements(声明) to specify the appropriate(适当的) driving table. Some DBAs use thenum_rows column of DBA_TABLES to get an idea of which table will be the best driving table, but the best way to determine the driving table is to look at the Boolean predicates(谓语) in thewhere clause.

select /*+ rule */
   emp.ename,
   emp.deptno,
   bonus.comm
from
   emp,
   bonus
where
   emp.ename = bonus.e(奖金)name
;

Note that the bonus table is the driving table because it appears last in the from clause(条款). Let’s explain this statement a(声明)nd observe the execution p(执行)lan.

OPERATION
----------------------------------------------------------------------
OPTIONS                        OBJECT_NAME                    POSITION
------------------------------ ---------------------------- ----------
 SELECT STATEMENT
  NESTED LOOPS
                                                                     1
    TABLE ACCESS
FULL                           BONUS
                                 1
    TABLE ACCESS
BY INDEX ROWID                 EMP                                   2
      INDEX
RANGE SCAN                     EMP_ENAME                             1

Now we reverse(颠倒) the table order, placing the bonus table first in the from clause.

select /*+ rule */
   emp.ename,
   emp.deptno,
   bonus.comm
from
   bonus,
   emp
where
   emp.ename = bonus.e(奖金)name
;

Now when we reexecute the explain plan utility(实用), we should see that the driving table has been reversed(颠倒).

OPERATION
----------------------------------------------------------------------
OPTIONS                        OBJECT_NAME                    POSITION
------------------------------ ---------------------------- ----------
 SELECT STATEMENT
  NESTED LOOPS
                                                                     1
    TABLE ACCESS
FULL                           EMP                                   1
    TABLE ACCESS
BY INDEX ROWID                 BONUS                                 2
      INDEX
RANGE SCAN                     BONUS_ENAME                           1

Here you see that the driving table has changed, and this is clear evidence(证据) that the only factor(因素) influencing the driving table with the RBO is the position of the table name in the where clause.

The Driving Table and Table Cardinality

Remember, the driving table should be the table that returns the smallest number of rows, and this is not always the table with the smallest number of rows. Hence(因此), you shouldevaluate(评价) each table independently and factor any filteringconstraints(约束) into the where clause.

For example, assume(承担) we have a customer table with 100,000 rows and an order table with 500,000 rows.

select
   customer_name
from
   customer,
   order,
where
   customer.cust_nbr = order.cust_nbr
and
   order_status = ‘backordered’;(延期交货)

At first blush(脸红), it might appear that the customer table should be the driving table. However, if there are only 50,000order rows that meet the order_status=’backordered’ criterion, then the order table should be made the driving table.

The driving table is important because it is retrieved(检索)first, and the rows from the second table are then merged(合并)into the result set from the first table. Therefore, it is essential that the second table return the least number of rows in terms of the where clause(条款). Note that the driving table should be the table that returns the least number of rows, not always the table with the smallest value for num_rows in the DBA_TABLES view.

With the rule-based optimizer(最优化), the indexing of tables and order of table name and Boolean expressions within the SQLstatement(声明) control the execution(执行) plan for the SQL. For the rule-based optimizer, consider the following query:

select /*+ rule */
   dname,
   sum(bonus.comm)
from
   emp,
   bonus,
   dept
where
   dept.deptno = emp.deptno
and
   emp.ename = bonus.e(奖金)name
group by
   dname;

Here we expect that a single row will be returned for each department, but hundreds of emp and bonus rows may bescanned(已扫描的) in order to compute the sum of allbonuses(奖金) by department. Hence(因此), we want the dept table to be the driving table for this query, and we have placed it last in the from clause.

OPERATION
----------------------------------------------------------------------
OPTIONS                        OBJECT_NAME                    POSITION
------------------------------ ---------------------------- ----------
 SELECT STATEMENT
  SORT
GROUP BY                                                             1
    NESTED LOOPS
                                                                     1
      NESTED LOOPS
                                                                     1
        TABLE ACCESS
FULL                           DEPT                                  1
        TABLE ACCESS
BY INDEX ROWID                 EMP                                   2
          INDEX
RANGE SCAN                     EMP_DEPTNO                            1
      TABLE ACCESS
BY INDEX ROWID                 BONUS                                 2
        INDEX
RANGE SCAN                     BONUS_ENAME                           1

As you can see from this example, the placement(布置) of the table names in the from clause(条款) is critical(鉴定的) to theexecution(执行) plan and the speed of the query.


This is an excerpt(摘录) from "Oracle High-Performance SQL Tuning" by Donald K. Burleson, published by Oracle Press.

你可能感兴趣的:(驱动表)