此脚本不依赖统计信息。

    在开发过程中,我们应该避免在同一个 SQL 语句中对同一个表多次访问。我们可以通过下面 SQL 抓出同一个 SQL 语句中对某个表进行多次扫描的 SQL。

    1. select a.parsing_schema_name schema,
    2. a.sql_id,
    3. a.sql_text,
    4. b.object_name,
    5. b.cnt
    6. from v$sql a,
    7. (select *
    8. from (select sql_id,
    9. child_number,
    10. object_owner,
    11. object_name,
    12. object_type,
    13. count(*) cnt
    14. from v$sql_plan
    15. where object_owner = 'SCOTT'
    16. group by sql_id,
    17. child_number,
    18. object_owner,
    19. object_name,
    20. object_type)
    21. where cnt >= 2) b
    22. where a.sql_id = b.sql_id
    23. and a.child_number = b.child_number;

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

    1. select ename,job,deptno from emp where sal>(select avg(sal) from emp);

    以上 SQL 访问了 emp 表两次,我们可以通过脚本将其抓出。

    1. select a.parsing_schema_name schema,
    2. a.sql_id,
    3. a.sql_text,
    4. b.object_name,
    5. b.cnt
    6. from v$sql a,
    7. (select *
    8. from (select sql_id,
    9. child_number,
    10. object_owner,
    11. object_name,
    12. object_type,
    13. count(*) cnt
    14. from v$sql_plan
    15. where object_owner = 'SCOTT'
    16. group by sql_id,
    17. child_number,
    18. object_owner,
    19. object_name,
    20. object_type)
    21. where cnt >= 2) b
    22. where a.sql_id = b.sql_id
    23. and a.child_number = b.child_number;
    1. SCHEMA SQL_ID SQL_TEXT OBJECT_NAME CNT
    2. --------------- --------------- ----------------------------- ------------ ----------
    3. SCOTT fdt0z70z43vgv select ename,job,deptno from EMP 2
    4. emp where sal>(select avg(sal)
    5. from emp)