Oracle优化——三种联结方法

NESTED LOOPS
条件:内表必须有索引,否则会造成NESTED TABLE SCAN。选取比例小。

SORT MERGE
条件:选取表中行的比例较大。内表没有索引。
缺点:需要对表排序,内存开销非常大。

HASH JOIN(先将一张表做成HASH表,再做NESTED LOOPS)
条件:必须是等值连接。
优势:同等条件下强于SORT MERGE。选取行的比例大时,优于NESTED LOOPS。
缺点:只能是等值连接。同样需要一定的内存开销。

SORT MERGE的执行计划
SH@ prod> select  max(amount_sold) from sales s join products p on s.prod_id < p.prod_id ;

MAX(AMOUNT_SOLD)
----------------
         1782.72


Execution Plan
----------------------------------------------------------
Plan hash value: 4223831697

--------------------------------------------------------------------------------------------------------------
| Id  | Operation              | Name        | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     | Pstart| Pstop |
--------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |             |     1 |    13 |       |  4088   (4)| 00:00:50 |       |       |
|   1 |  SORT AGGREGATE        |             |     1 |    13 |       |            |          |       |       |
|   2 |   MERGE JOIN           |             |    32M|   404M|       |  4088   (4)| 00:00:50 |       |       |
|   3 |    SORT JOIN           |             |    72 |   288 |       |     1   (0)| 00:00:01 |       |       |
|   4 |     INDEX FULL SCAN    | PRODUCTS_PK |    72 |   288 |       |     1   (0)| 00:00:01 |       |       |
|*  5 |    SORT JOIN           |             |   918K|  8075K|    35M|  3988   (1)| 00:00:48 |       |       |
|   6 |     PARTITION RANGE ALL|             |   918K|  8075K|       |   489   (2)| 00:00:06 |     1 |    28 |
|   7 |      TABLE ACCESS FULL | SALES       |   918K|  8075K|       |   489   (2)| 00:00:06 |     1 |    28 |
--------------------------------------------------------------------------------------------------------------

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

   5 - access(INTERNAL_FUNCTION("S"."PROD_ID")

你可能感兴趣的:(数据库技术,SQL)