通过FETCH NEXT子句

时间:2018-07-24 17:01:05

标签: sql oracle query-optimization

我有一个运行时间很长的Oracle Query,它使用了一堆:

WHERE EXISTS (SELECT NULL FROM Table WHERE TableColumn IN (...))

我不能只使用SELECT NULL,因为我只关心FETCH NEXT 1 ROW ONLY是否是{{1},而不是使用遍历整个表的TableColumn来查找条件。 }?

赞:

IN (...)

因此WHERE EXISTS (SELECT NULL FROM Table WHERE TableColumn IN (...) FETCH NEXT 1 ROW ONLY) 的求值速度更快。

编辑:

以下是未附加WHERE EXISTS子句的查询计划:

FETCH NEXT

下面是附加了------------------------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost | Time | ------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 75 | 521611 | | | 1 | SORT AGGREGATE | | 1 | 75 | | | | 2 | HASH JOIN | | 531266 | 39844950 | 521611 | | | 3 | TABLE ACCESS FULL | ACCT | 47574 | 523314 | 418 | | | 4 | HASH JOIN | | 531224 | 33998336 | 521185 | | | 5 | INDEX FAST FULL SCAN | PK_ACTVTYP | 454 | 2270 | 2 | | | 6 | HASH JOIN | | 531224 | 31342216 | 521177 | | | 7 | INDEX FULL SCAN | PK_ACTVCAT | 67 | 335 | 1 | | | 8 | HASH JOIN SEMI | | 531224 | 28686096 | 521169 | | | 9 | NESTED LOOPS SEMI | | 531224 | 28686096 | 521169 | | | 10 | STATISTICS COLLECTOR | | | | | | | 11 | HASH JOIN RIGHT SEMI | | 531224 | 25498752 | 112887 | | | 12 | TABLE ACCESS FULL | AMSACTVGRPEMPL | 2364 | 35460 | 10 | | | 13 | TABLE ACCESS FULL | ACTV | 12779986 | 421739538 | 112712 | | | 14 | INDEX RANGE SCAN | ACTVSUBACTV_DX2 | 163091724 | 978550344 | 251246 | | | 15 | INDEX FAST FULL SCAN | ACTVSUBACTV_DX2 | 163091724 | 978550344 | 251246 | | ------------------------------------------------------------------------------------------------ 子句的查询计划:

FETCH NEXT

据我所见,没有------------------------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost | Time | ------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 69 | 113148 | | | 1 | SORT AGGREGATE | | 1 | 69 | | | | 2 | FILTER | | | | | | | 3 | HASH JOIN | | 531221 | 36654249 | 113144 | | | 4 | TABLE ACCESS FULL | ACCT | 47574 | 523314 | 418 | | | 5 | HASH JOIN | | 531179 | 30808382 | 112718 | | | 6 | INDEX FAST FULL SCAN | PK_ACTVTYP | 454 | 2270 | 2 | | | 7 | HASH JOIN | | 531179 | 28152487 | 112710 | | | 8 | INDEX FULL SCAN | PK_ACTVCAT | 67 | 335 | 1 | | | 9 | HASH JOIN RIGHT SEMI | | 531179 | 25496592 | 112702 | | | 10 | TABLE ACCESS FULL | AMSACTVGRPEMPL | 2167 | 32505 | 10 | | | 11 | TABLE ACCESS FULL | ACTV | 12778893 | 421703469 | 112527 | | | 12 | VIEW | | 1 | 13 | 4 | | | 13 | WINDOW BUFFER PUSHED RANK | | 8 | 48 | 4 | | | 14 | INDEX RANGE SCAN | ACTVSUBACTV_DX2 | 8 | 48 | 4 | | ------------------------------------------------------------------------------------------------ 的情况似乎会增加FETCH NEXT的开销

编辑#2

添加TABLE ACCESS FULL而不是AND ROWNUM = 1

FETCH NEXT 1 ROW ONLY

1 个答案:

答案 0 :(得分:0)

pip --version是12c中的新功能,为避免性能问题导致添加 提示如下

FETCH NEXT

尝试一下并检查其查询计划

注意:我建议在ACCT表,ACTV表上添加索引,以提高其性能。