相交并存在性能Oracle11g
Intersect and exists performance Oracle11g
我想知道 INTERSECT
或 EXISTS
在 Oracle 11g
中是否有更好的表现。考虑一下我有以下两个 tables.
Student_Master
STUDENT_ID NAME
---------- ------
STUD01 ALEX
STUD02 JAMES
STUD03 HANS
Student_Status
STUDENT_ID STATUS
---------- ------
STUD01 Fail
STUD02 Pass
STUD03 Pass
考虑到 table Student_Status 与 table 相比,以下哪个查询的记录数更多Student_Master.
SELECT STUDENT_ID FROM Student_Master
INTERSECT
SELECT STUDENT_ID FROM Student_Status
SELECT STUDENT_ID FROM Student_Master M
WHERE EXISTS
(SELECT STUDENT_ID FROM Student_Status S WHERE M.STUDENT_ID=S.STUDENT_ID)
快速测试会建议 EXISTS 选项...
SELECT STUDENT_ID FROM Student_Master INTERSECT SELECT STUDENT_ID FROM
Student_Status
Plan hash value: 416197223
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 6 (100)| |
| 1 | INTERSECTION | | | | | |
| 2 | SORT UNIQUE | | 3 | 36 | 3 (34)| 00:00:01 |
| 3 | TABLE ACCESS FULL| STUDENT_MASTER | 3 | 36 | 2 (0)| 00:00:01 |
| 4 | SORT UNIQUE | | 3 | 36 | 3 (34)| 00:00:01 |
| 5 | TABLE ACCESS FULL| STUDENT_STATUS | 3 | 36 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
-
SELECT STUDENT_ID FROM Student_Master M WHERE EXISTS (SELECT STUDENT_ID
FROM Student_Status S WHERE M.STUDENT_ID=S.STUDENT_ID)
Plan hash value: 361045672
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 4 (100)| |
|* 1 | HASH JOIN SEMI | | 3 | 72 | 4 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL| STUDENT_MASTER | 3 | 36 | 2 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| STUDENT_STATUS | 3 | 36 | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
带有 EXIST 运算符的第二种形式总是在 Student_Master table 上进行全扫描,所以我认为,使用相交会获得更好的性能。但是你需要在 master table.
上使用索引
有关 EXIST 的更多信息可在此处找到:Ask TOM - IN & EXISTS
我想知道 INTERSECT
或 EXISTS
在 Oracle 11g
中是否有更好的表现。考虑一下我有以下两个 tables.
Student_Master
STUDENT_ID NAME
---------- ------
STUD01 ALEX
STUD02 JAMES
STUD03 HANS
Student_Status
STUDENT_ID STATUS
---------- ------
STUD01 Fail
STUD02 Pass
STUD03 Pass
考虑到 table Student_Status 与 table 相比,以下哪个查询的记录数更多Student_Master.
SELECT STUDENT_ID FROM Student_Master
INTERSECT
SELECT STUDENT_ID FROM Student_Status
SELECT STUDENT_ID FROM Student_Master M
WHERE EXISTS
(SELECT STUDENT_ID FROM Student_Status S WHERE M.STUDENT_ID=S.STUDENT_ID)
快速测试会建议 EXISTS 选项...
SELECT STUDENT_ID FROM Student_Master INTERSECT SELECT STUDENT_ID FROM
Student_Status
Plan hash value: 416197223
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 6 (100)| |
| 1 | INTERSECTION | | | | | |
| 2 | SORT UNIQUE | | 3 | 36 | 3 (34)| 00:00:01 |
| 3 | TABLE ACCESS FULL| STUDENT_MASTER | 3 | 36 | 2 (0)| 00:00:01 |
| 4 | SORT UNIQUE | | 3 | 36 | 3 (34)| 00:00:01 |
| 5 | TABLE ACCESS FULL| STUDENT_STATUS | 3 | 36 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
-
SELECT STUDENT_ID FROM Student_Master M WHERE EXISTS (SELECT STUDENT_ID
FROM Student_Status S WHERE M.STUDENT_ID=S.STUDENT_ID)
Plan hash value: 361045672
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 4 (100)| |
|* 1 | HASH JOIN SEMI | | 3 | 72 | 4 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL| STUDENT_MASTER | 3 | 36 | 2 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| STUDENT_STATUS | 3 | 36 | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
带有 EXIST 运算符的第二种形式总是在 Student_Master table 上进行全扫描,所以我认为,使用相交会获得更好的性能。但是你需要在 master table.
上使用索引有关 EXIST 的更多信息可在此处找到:Ask TOM - IN & EXISTS