试举出 3 个自然连接的例子,分别使用 nested loop、hash join、sort merge 来进行连接 (展示其执行计划)。提示:可以使用 hints
时间: 2024-03-03 09:47:05 浏览: 138
例子1:使用 nested loop 进行自然连接
```
SELECT *
FROM orders, customers
WHERE orders.customer_id = customers.customer_id;
```
执行计划:
```
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 150K| 23M| | 2276 (1)| 00:00:01 |
|* 1 | HASH JOIN | | 150K| 23M| 13M| 2276 (1)| 00:00:01 |
| 2 | TABLE ACCESS FULL | CUSTOMER | 150K| 5859K| | 32 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL | ORDERS | 150K| 10M| | 1206 (1)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("ORDERS"."CUSTOMER_ID"="CUSTOMER"."CUSTOMER_ID")
```
例子2:使用 hash join 进行自然连接
```
SELECT /*+ USE_HASH(customers orders) */ *
FROM orders, customers
WHERE orders.customer_id = customers.customer_id;
```
执行计划:
```
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 150K| 23M| | 2461 (1)| 00:00:01 |
| 1 | HASH JOIN | | 150K| 23M| 13M| 2461 (1)| 00:00:01 |
| 2 | TABLE ACCESS FULL| ORDERS | 150K| 10M| | 1206 (1)| 00:00:01 |
| 3 | TABLE ACCESS FULL| CUSTOMER | 150K| 5859K| | 32 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
```
例子3:使用 sort merge 进行自然连接
```
SELECT /*+ USE_MERGE(customers orders) */ *
FROM orders, customers
WHERE orders.customer_id = customers.customer_id;
```
执行计划:
```
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 150K| 23M| | 3826 (1)| 00:00:01 |
| 1 | MERGE JOIN | | 150K| 23M| | 3826 (1)| 00:00:01 |
| 2 | SORT JOIN | | 150K| 2929K| 2912K| 759 (1)| 00:00:01 |
| 3 | TABLE ACCESS | CUSTOMER | 150K| 2929K| | 32 (0)| 00:00:01 |
| 4 | SORT JOIN | | 150K| 10M| 8496K| 3066 (1)| 00:00:01 |
| 5 | TABLE ACCESS | ORDERS | 150K| 10M| | 1206 (1)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("ORDERS"."CUSTOMER_ID"="CUSTOMER"."CUSTOMER_ID")
```
注意,以上的执行计划仅供参考,实际的执行计划可能会因为系统配置、数据量等因素而有所不同。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)