此脚本不依赖统计信息。

    在开发过程中,我们应该避免在 SQL 语句中调用自定义函数。我们可以通过以下 SQL 语句抓出 SQL 语句中调用了自定义函数的 SQL。

    1. select distinct sql_id, sql_text, module
    2. from V$SQL,
    3. (select object_name
    4. from DBA_OBJECTS O
    5. where owner = 'SCOTT'
    6. and object_type in ('FUNCTION', 'PACKAGE'))
    7. where (instr(upper(sql_text), object_name) > 0)
    8. and plsql_exec_time > 0
    9. and regexp_like(upper(sql_fulltext), '^[SELECT]')
    10. and parsing_schema_name = 'SCOTT';

    我们在 Scott 账户中创建如下函数。

    1. create or replace function f_getdname(v_deptno in number) return varchar2 as
    2. v_dname dept.dname%type;
    3. begin
    4. select dname into v_dname from dept where deptno = v_deptno;
    5. return v_dname;
    6. end f_getdname;

    然后我们在 Scott 账户中运行如下 SQL。

    1. select empno,sal,f_getdname(deptno) dname from emp;
    1. EMPNO SAL DNAME
    2. ---------- ---------- -------------------------
    3. 7369 800 RESEARCH
    4. 7499 1600 SALES
    5. 7521 1250 SALES
    6. 7566 2975 RESEARCH
    7. 7654 1250 SALES
    8. 7698 2850 SALES
    9. 7782 2450 ACCOUNTING
    10. 7788 3000 RESEARCH
    11. 7839 5000 ACCOUNTING
    12. 7844 1500 SALES
    13. 7876 1100 RESEARCH
    14. 7900 950 SALES
    15. 7902 3000 RESEARCH
    16. 7934 1300 ACCOUNTING

    我们通过脚本抓出刚执行过的 SQL 语句。

    1. select distinct sql_id, sql_text, module
    2. from V$SQL,
    3. (select object_name
    4. from DBA_OBJECTS O
    5. where owner = 'SCOTT'
    6. and object_type in ('FUNCTION', 'PACKAGE'))
    7. where (instr(upper(sql_text), object_name) > 0)
    8. and plsql_exec_time > 0
    9. and regexp_like(upper(sql_fulltext), '^[SELECT]')
    10. and parsing_schema_name = 'SCOTT';
    1. SQL_ID SQL_TEXT MODULE
    2. --------------- ------------------------------------------------------- ---------
    3. 2ck71xc69j49u select empno,sal,f_getdname(deptno) dname from emp SQL*Plus