Oracle 如何处理给定的 SQL 语句

How Oracle processes the given SQL statement

您能否解释一下查询将如何执行?请逐步解释...

select
  max(salary),
  country_id
from (
  select
    salary,
    department_id,
    location_id,
    country_id
  from
    HR.EMPLOYEES
  natural join
    HR.DEPARTMENTS
  natural join
    HR.LOCATIONS
)
group by country_id;

理解SQL执行步骤的最好方法是生成解释计划并从最内层ID向最外层分析。

SQL> explain plan for
  2  select
  3    max(salary),
  4    country_id
  5  from (
  6    select
  7      salary,
  8      department_id,
  9      location_id,
 10      country_id
 11    from
 12      HR.EMPLOYEES
 13    natural join
 14      HR.DEPARTMENTS
 15    natural join
 16      HR.LOCATIONS
 17  )
 18  group by country_id;

Explained.

SQL>

让我们以可读的格式显示解释计划:

SQL> SELECT * FROM TABLE(dbms_xplan.display);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
Plan hash value: 1571404374

----------------------------------------------------------------------------------------------
| Id  | Operation                      | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT               |             |    11 |   297 |     8   (0)| 00:00:01 |
|   1 |  HASH GROUP BY                 |             |    11 |   297 |     8   (0)| 00:00:01 |
|*  2 |   HASH JOIN                    |             |    11 |   297 |     8   (0)| 00:00:01 |
|   3 |    MERGE JOIN                  |             |    11 |   176 |     5   (0)| 00:00:01 |
|   4 |     TABLE ACCESS BY INDEX ROWID| LOCATIONS   |    23 |   138 |     2   (0)| 00:00:01 |
|   5 |      INDEX FULL SCAN           | LOC_ID_PK   |    23 |       |     1   (0)| 00:00:01 |
|*  6 |     SORT JOIN                  |             |    11 |   110 |     3   (0)| 00:00:01 |
|*  7 |      TABLE ACCESS FULL         | DEPARTMENTS |    11 |   110 |     3   (0)| 00:00:01 |
|   8 |    TABLE ACCESS FULL           | EMPLOYEES   |   107 |  1177 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("EMPLOYEES"."DEPARTMENT_ID"="DEPARTMENTS"."DEPARTMENT_ID" AND
              "EMPLOYEES"."MANAGER_ID"="DEPARTMENTS"."MANAGER_ID")
   6 - access("DEPARTMENTS"."LOCATION_ID"="LOCATIONS"."LOCATION_ID")
       filter("DEPARTMENTS"."LOCATION_ID"="LOCATIONS"."LOCATION_ID")
   7 - filter("DEPARTMENTS"."MANAGER_ID" IS NOT NULL)

Note
-----
   - this is an adaptive plan

28 rows selected.

SQL>

查询转换器可以决定是否重写查询,以便优化器生成更好的执行计划。

来自documentation

Estimator

The estimator determines the overall cost of a given execution plan. The estimator generates three different types of measures to achieve this goal:

  • Selectivity

    This measure represents a fraction of rows from a row set. The selectivity is tied to a query predicate, such as last_name='Smith', or a combination of predicates.

  • Cardinality

    This measure represents the number of rows in a row set.

  • Cost

    This measure represents units of work or resource used. The query optimizer uses disk I/O, CPU usage, and memory usage as units of work.

If statistics are available, then the estimator uses them to compute the measures. The statistics improve the degree of accuracy of the measures.

我建议阅读我在上面提供的文档 link。