日期:2014-05-16  浏览次数:20501 次

从查询计划看Oracle连接查询性能误解
内连接:
SQL/86
select *
from products p, product_types pt
where p.product_type_id = pt.product_type_id;

SQL/92
select *
from products p
inner join product_types using(product_type_id);

左外连接:
SQL/86
select *
from products p, product_types pt
where p.product_type_id = pt.product_type_id(+);

SQL/92
select *
from products
left outer join product_types using(product_type_id);

右外连接:
SQL/86
select *
from products p, product_types pt
where p.product_type_id (+) = pt.product_type_id;

SQL/92
select *
from products p
right outer join product_types pt using(product_type_id);

全外连接:
SQL/86
无对应语法
SQL/92
select *
from products p
full outer join product_types pt using(product_type_id);

笛卡尔积:
SQL/86
select *
from products p, product_types pt

SQL/92
select *
from products p
cross join product_types


误解一:SQL/86语句比SQL/92快
以内连接为例,
SQL/86的Plan:
-------------------------------------------------------------
| Id  | Operation                    | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------
|   0 | SELECT STATEMENT             |                  |    11 |   605 |     6  (17)| 00:00:01 |
|   1 |  MERGE JOIN                  |                  |    11 |   605 |     6  (17)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| PRODUCT_TYPES    |     5 |    40 |     2   (0)| 00:00:01 |
|   3 |    INDEX FULL SCAN           | PRODUCT_TYPES_PK |     5 |       |     1   (0)| 00:00:01 |
|*  4 |   SORT JOIN                  |                  |    11 |   517 |     4  (25)| 00:00:01 |
|*  5 |    TABLE ACCESS FULL         | PRODUCTS         |    11 |   517 |     3   (0)| 00:00:01 |
-------------------------------------------------------------
SQL/92的Plan:
-------------------------------------------------------------
| Id  | Operation                    | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------
|   0 | SELECT STATEMENT&nbs