当前位置:Gxlcms > mysql > 如何快速得到真实的执行计划

如何快速得到真实的执行计划

时间:2021-07-01 10:21:17 帮助过:22人阅读

准备工作: create table zbdba as select * from dba_objects;create table zbdba1 as select * from dba_objects;create index zbdba_owner on zbdba(owner);create index zbdba1_owner on zbdba1(owner);exec dbms_stats.gather_table_stats(user, ZBDBA,

准备工作:
  1. create table zbdba as select * from dba_objects;
  2. create table zbdba1 as select * from dba_objects;
  3. create index zbdba_owner on zbdba(owner);
  4. create index zbdba1_owner on zbdba1(owner);
  5. exec dbms_stats.gather_table_stats(user, 'ZBDBA', method_opt => 'FOR ALL COLUMNS SIZE 1');
  6. exec dbms_stats.gather_table_stats(user, 'ZBDBA1', method_opt => 'FOR ALL COLUMNS SIZE 1');
通常我们对于执行时间很长的sql查看执行计划:
  1. explain plan for select zbdba.object_name from zbdba,zbdba1 where zbdba.owner=zbdba1.owner
  2. SCOTT@orcl11g>select plan_table_output from table(dbms_xplan.display());
  3. PLAN_TABLE_OUTPUT
  4. ------------------------------------------------------------------------------------------------------------------------------------
  5. Plan hash value: 1287183320
  6. ----------------------------------------------------------------------------------------------
  7. | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
  8. ----------------------------------------------------------------------------------------------
  9. | 0 | SELECT STATEMENT | | 207M| 7330M| | 1198 (55)| 00:00:15 |
  10. |* 1 | HASH JOIN | | 207M| 7330M| 1272K| 1198 (55)| 00:00:15 |
  11. | 2 | INDEX FAST FULL SCAN| ZBDBA1_OWNER | 72068 | 422K| | 48 (0)| 00:00:01 |
  12. | 3 | TABLE ACCESS FULL | ZBDBA | 72067 | 2181K| | 288 (1)| 00:00:04 |
  13. ----------------------------------------------------------------------------------------------
  14. Predicate Information (identified by operation id):
  15. ---------------------------------------------------
  16. 1 - access("ZBDBA"."OWNER"="ZBDBA1"."OWNER")
  17. 15 rows selected.

但是这种执行计划不一定是准确的。那我们怎么能才能快速的得到准确的执行计划呢?

真实的执行计划就是已经执行的sql

那么

  1. select zbdba.object_name from zbdba,zbdba1 where zbdba.owner=zbdba1.owner;
  2. 你不需要一直等,ctrl+c中断即可
  3. SCOTT@orcl11g>select sql_text,sql_id,hash_value,child_number from v$sql where sql_text like 'select zbdba.object_name from zbdba,zbdba1 where zbdba.owner=zbdba1.owner%';
  4. SQL_TEXT SQL_ID HASH_VALUE CHILD_NUMBER
  5. ------------------------------------------------------------ -------------------------- ---------- ------------
  6. select zbdba.object_name from zbdba,zbdba1 where zbdba.owner fr4g7ypwx5krq 2043857654 0
  7. =zbdba1.owner
  8. SCOTT@orcl11g>select * from table(dbms_xplan.display_cursor('fr4g7ypwx5krq',0,'ALLSTATS LAST'));
  9. PLAN_TABLE_OUTPUT
  10. ------------------------------------------------------------------------------------------------------------------------------------
  11. SQL_ID fr4g7ypwx5krq, child number 0
  12. -------------------------------------
  13. select zbdba.object_name from zbdba,zbdba1 where
  14. zbdba.owner=zbdba1.owner
  15. Plan hash value: 1287183320
  16. ----------------------------------------------------------------------------------
  17. | Id | Operation | Name | E-Rows | OMem | 1Mem | Used-Mem |
  18. ----------------------------------------------------------------------------------
  19. | 0 | SELECT STATEMENT | | | | | |
  20. |* 1 | HASH JOIN | | 207M| 3024K| 1862K| 7066K (0)|
  21. | 2 | INDEX FAST FULL SCAN| ZBDBA1_OWNER | 72068 | | | |
  22. | 3 | TABLE ACCESS FULL | ZBDBA | 72067 | | | |
  23. ----------------------------------------------------------------------------------
  24. Predicate Information (identified by operation id):
  25. ---------------------------------------------------
  26. 1 - access("ZBDBA"."OWNER"="ZBDBA1"."OWNER")
  27. Note
  28. -----
  29. - Warning: basic plan statistics not available. These are only collected when:
  30. * hint 'gather_plan_statistics' is used for the statement or
  31. * parameter 'statistics_level' is set to 'ALL', at session or system level
  32. 27 rows selected.
这样就快速从内存中到了真实的执行计划

人气教程排行