上一篇文章中介绍了如何对Oracle中Struct和Array进行Explain Plan,
- EXPLAIN PLAN FOR
- SELECT * FROM TABLE(DEPT_ARRAY(DEPARTMENT_TYPE(
- 1, --DNO NUMBER (10),
- 'NAME', --NAME VARCHAR2 (50),
- 'LOCATION' --LOCATION VARCHAR2 (50)
- ))) T
- JOIN TABLE2 T2 ON T.DNO = T2.DNO;
- SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
运行出来的结果中会有这样的数据:
- PLAN_TABLE_OUTPUT
- 1 Plan hash value: 1748000095
- 2
- 3 ----------------------------------------------------------------------------------------------
- 4 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- 5 ----------------------------------------------------------------------------------------------
- 6 | 0 | SELECT STATEMENT | | 8168 | 16336 | 24 (0)| 00:00:01 |
- 7 | 1 | COLLECTION ITERATOR CONSTRUCTOR FETCH| | | | | |
- 8 ----------------------------------------------------------------------------------------------
optimizer默认Array中数据行数为8168, 但是由于情景不同,放在Array中数据的行数不尽相同。使用cardinality hint来让optimizer知道Array中数据的行数。
- EXPLAIN PLAN FOR
- SELECT /*+ cardinality(t 10 ) */ * FROM TABLE(DEPT_ARRAY(DEPARTMENT_TYPE(
- 1, --DNO NUMBER (10),
- 'NAME', --NAME VARCHAR2 (50),
- 'LOCATION' --LOCATION VARCHAR2 (50)
- ))) T
- JOIN TABLE2 T2 ON T.DNO = T2.DNO;
- SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
运行出来的结果:
- PLAN_TABLE_OUTPUT
- 1 Plan hash value: 1748000095
- 2
- 3 ----------------------------------------------------------------------------------------------
- 4 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- 5 ----------------------------------------------------------------------------------------------
- 6 | 0 | SELECT STATEMENT | | 10 | 20 | 24 (0)| 00:00:01 |
- 7 | 1 | COLLECTION ITERATOR CONSTRUCTOR FETCH| | | | | |
- 8 ----------------------------------------------------------------------------------------------
数字10为Array中数据的最大行数。
参考:
the cardinality hint when you are using PL/SQL collections in SQL